我尝试显示基于PK的用户配置文件。所以,我创建了一个名称为“Profile”的菜单。用户单击菜单后,用户可以看到该配置文件。Django,使用库检索用户配置文件没有调用模型的用户
我尝试这样做:
def view_profile(request, pk):
profile = get_object_or_404(User, pk=pk)
return render(request, 'girl/base.html', {'profile':profile})
而且也是这样:
def view_profile(request, pk):
profile = User.objects.filter(User, pk=pk)
return render(request, 'base.html', {'profile':profile})
在base.html文件:
{% if user.is_authenticated %}
<a href='{% url "profile" pk=profile.pk%}' class="prof"><span class="pro">Profile</span></a>
{% endif %}
而且在模板中,profile.html:
{% extends 'base.html' %}
{% block content %}
{% if user.is_authenticated %}
<h2><a href="{% url 'profile' pk=profile.pk %}">{{ profile.username }}</a></h2>
{% endif %}
{% endblock %}
urls.py
url(r'^cat/(?P<pk>\d+)/profile/$', views.view_profile, name='profile')
的轮廓一直没有露面,我得到了以下错误:
Reverse for 'profile' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['cat/(?P<pk>\\d+)/profile/$']
错误是一样的。 –
''User.objects.filter(pk = pk)在浏览器中,并确保你有pk,url的格式是正确的。 –
@Scarlettstone检查您的profile.html的源代码''User.objects.filter(pk = pk)' –