我嘗試顯示基於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)' –