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

标签 python django django-templates django-views

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

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

如有任何帮助,我们将不胜感激!

查看

from django.http import HttpResponse
from django.shortcuts import render
from howdidu.forms import UserProfileForm
from howdidu.models import UserProfile
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User

def index(request):
    context_dict = {'boldmessage': "I am bold font from the context"}
    return render(request, 'howdidu/index.html', context_dict)

#user profile form
@login_required
def register_profile(request):
    profile = UserProfile.objects.get(user=request.user)
    if request.method == 'POST':
        form = UserProfileForm(request.POST, request.FILES, instance=profile)
        if form.is_valid():
            form.save()
            return index(request)
        else:
            print form.errors
    else:
        form = UserProfileForm()
    return render(request, 'howdidu/register_profile.html', {'form': form})

#profile page using user name as url
@login_required
def profile_page(request, username):
    user = get_object_or_404(User, username=username)
    return render(request, 'howdidu/profile.html', {'profile_user': user})

项目地址

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}), )

应用程序网址

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'),
        )

模板

{% 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 %}

最佳答案

  1. 在配置文件夹中注册您的应用程序的 url project_name/urls.py : 例如:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings 

urlpatterns = [
    url(r'^user/', include('<app_name>.urls')),
    url(r'^admin/', include(admin.site.urls)),
]
  1. 在您的 <app_name>/urls.py 中添加一条新路线. 例如:

从。导入 View

urlpatterns = [
    url(r'profile/(?P<username>[a-zA-Z0-9]+)$', views.get_user_profile),
]
  1. <app_name>/views.py 中添加 View 那需要username (用户的用户名)检索其信息并将其发送到模板中 例如:
from django.shortcuts import render

def get_user_profile(request, username):
    user = User.objects.get(username=username)
    return render(request, '<app_name>/user_profile.html', {"user":user})
  1. <app_name>/templates/<app_name>/user_profile.html 中创建模板文件显示user对象:
{{ user.username }} 
{{ user.email }}
{{ user.first_name }} 
{{ user.last_name }} 

替换<app_name>以您的应用名称命名,它应该不错。

关于python - 如何使用 Django 显示用户配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724344/

相关文章:

django - 使用 'inspectdb' 得到错误 'IndexError: list index out of range'(可能是因为没有一些 FK)。请建议解决方法

html - 边框不延伸过去的 float 元素?

django - 如何在 django 模板中的字典中获取第一个值?

python - '类型错误: __init__() got multiple values for argument' with Python 3 using super() function

python - 如何将从用户获取的多个值分配给存储在Python字典中的单个键?

python - 使用元数据创建文件名

python - Durationfield django

python - django 身份验证 View 的模板不存在

html - Django : Select option in template

python - 了解 SVR scikit-learn 收敛所需的迭代次数