Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Django显示用户配置文件?_Python_Django_Django Templates_Django Views - Fatal编程技术网

Python 如何使用Django显示用户配置文件?

Python 如何使用Django显示用户配置文件?,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我是django的新手,目前正在尝试建立一个允许用户登录和查看其他用户档案的网站。到目前为止,我已经成功地让用户登录,但我不知道如何查看其他人的个人资料 每个配置文件都使用用户用户名为其配置文件创建url。当前,如果我作为一个用户登录并将URL更改为另一个用户配置文件URL,它仍会显示当前用户配置文件。我想要一个类似pinterest的东西,任何人无论是否登录都可以查看个人资料 任何帮助都将不胜感激 看法 项目url from django.conf.urls import patterns,

我是django的新手,目前正在尝试建立一个允许用户登录和查看其他用户档案的网站。到目前为止,我已经成功地让用户登录,但我不知道如何查看其他人的个人资料

每个配置文件都使用用户用户名为其配置文件创建url。当前,如果我作为一个用户登录并将URL更改为另一个用户配置文件URL,它仍会显示当前用户配置文件。我想要一个类似pinterest的东西,任何人无论是否登录都可以查看个人资料

任何帮助都将不胜感激

看法

项目url

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from registration.backends.simple.views import RegistrationView

class MyRegistrationView(RegistrationView):  #redirects to home page after registration
    def get_success_url(self,request, user):
        return '/register_profile'

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'howdidu_project.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('howdidu.urls')),
    url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'), #redirects to home page after registration
    (r'^accounts/', include('registration.backends.simple.urls')),
    url(r'^(?P<username>\w+)/', include('howdidu.urls')), #do i need this?
)

# media
if settings.DEBUG:
    urlpatterns += patterns(
        'django.views.static',
        (r'^media/(?P<path>.*)',
        'serve',
        {'document_root': settings.MEDIA_ROOT}), )
从django.conf.url导入模式,包括,url
从django.contrib导入管理
从django.conf导入设置
从registration.backends.simple.views导入注册视图
类MyRegistrationView(RegistrationView):#注册后重定向到主页
def get_success_url(自我、请求、用户):
返回“/register\u profile”
urlpatterns=模式(“”,
#示例:
#url(r'^$','howdidu_project.views.home',name='home'),
#url(r“^blog/”,包括('blog.url'),
url(r“^admin/”,包括(admin.site.url)),
url(r“”,包括('howdidu.url'),
url(r“^accounts/register/$”,MyRegistrationView.as_view(),name='registration'u register'),注册后重定向到主页
(r“^accounts/”,包括('registration.backends.simple.url'),
url(r'^(?P\w+/),include('howdidu.url'),#我需要这个吗?
)
#媒体
如果设置为.DEBUG:
urlpatterns+=模式(
“django.views.static”,
(r“^media/(?P.*)”,
"发球",,
{'document\u root':settings.MEDIA\u root}),)
应用程序url

from django.conf.urls import patterns, url
from howdidu import views

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        url(r'^register_profile/$', views.register_profile, name='register_profile'),
        url(r'^(?P<username>\w+)/$', views.profile_page, name='user_profile'),
        )
来自django.conf.url导入模式,url
从howdidu导入视图
urlpatterns=模式(“”,
url(r'^$',views.index,name='index'),
url(r“^register\u profile/$”,views.register\u profile,name='register\u profile'),
url(r'^(?P\w+/$),views.profile\u页面,name='user\u profile'),
)
模板

{% extends 'howdidu/base.html' %}

{% load staticfiles %}

{% block title %}{{ user.username }}{% endblock %}

{% block body_block %}
        {% if user.is_authenticated %}
        <h1>{{ user.username }} welcome to your profile page</h1>
        <img src="{{ user.userprofile.profile_picture.url }}" width = "150" height = "150"  />
        <h2>{{ user.userprofile.first_name }}</h2>
        <h2>{{ user.userprofile.second_name }}</h2>
        <h2>{{ user.userprofile.user_country }}</h2>
        {% endif %}

{% endblock %}
{%extends'howdidu/base.html%}
{%load staticfiles%}
{%block title%}{{user.username}{%endblock%}
{%block body_block%}
{%if user.u经过身份验证%}
{{user.username}}欢迎来到您的个人资料页面
{{user.userprofile.first_name}
{{user.userprofile.second_name}
{{user.userprofile.user_country}
{%endif%}
{%endblock%}
  • 在配置文件夹
    project\u name/url.py
    中注册应用程序的URL: 例如:
  • 从django.conf.url导入包括,url
    从django.contrib导入管理
    从django.conf导入设置
    URL模式=[
    url(r“^user/”,包括(“.url”),
    url(r“^admin/”,包括(admin.site.url)),
    ]
    
  • /url.py
    中添加新路由。 例如:
  • 从。导入视图

    urlpatterns = [
        url(r'profile/(?P<username>[a-zA-Z0-9]+)$', views.get_user_profile),
    ]
    
  • /templates//user_profile.html
    中创建模板文件,以显示
    用户
    对象:
  • {{user.username}
    {{user.email}
    {{user.first_name}
    {{user.last_name}
    

    替换为应用程序的名称,这样应该很好。

    谢谢您的回复!除了这里和那里的名字不同之外,我的代码几乎完全一样。我似乎仍然有同样的问题。例如,如果我的网站是www.example.com,我的用户名是oliver。当我进入www.example.com/oliver时,我可以在登录时进入我的用户配置文件ok。如果我尝试访问另一个用户配置文件(如www.example.com/katie),我仍然会登录,但我似乎仍然会获得oliver的用户配置文件。我需要做什么来确保其他用户的数据保留在每个用户配置文件的每个url上?我是否需要以某种方式更改我的视图?在您的视图中,您必须通过url
    profile/(?P[a-zA-Z0-9]+)$
    检索用户名的数据。请将您当前的视图添加到您的问题中我刚刚上传了我的代码:-)希望现在您的代码很好,应该可以工作了。也添加你的模板,请上传模板,目前非常基本!
    urlpatterns = [
        url(r'profile/(?P<username>[a-zA-Z0-9]+)$', views.get_user_profile),
    ]