我下面的教程可在Django添加好友:Building Friend Networks如何Django的request.GET中獲取的用戶名
我得到一個404頁面,因爲我已經在我的代碼設置它提出一個HTTP404如果用戶名是在不request.GET中發現:
views.py:
def friend_add(request):
if 'username' in request.GET:
friend = get_object_or_404(User, username=request.GET['username'])
friendship = Friendship(from_friend=request.user, to_friend=friend)
friendship.save()
return HttpResponseRedirect('/friends/%s' % request.user.username)
else:
raise Http404
urls.py:
url(r'^friend/add/(?P<username>\w+)$', photo.views.friends_request),
url(r'^friends/(?P<username>\w+)$', photo.views.friends),
url(r'^request_sent/$', photo.views.friend_add),
我使用的模板標籤像這樣從教程中所示:
{% ifequal user.username abuild.user %}
<a href="/builds/friends/{{ user.username }}">view your friends</a>
{% else %}
{% if is_friend %}
<a href="/builds/friends/{{ abuild.user }}">
{{ abuild.user }} is a friend of yours</a>
{% else %}
<a href="/builds/request_sent/">
add {{ abuild.user}} to your friends</a>
{% endif %}
- <a href="/builds/friends/{{ abuild.user }}">
view {{abuild.user}}'s friends</a>
{% endifequal %}
如何確保我得到的用戶名,當我點擊進入/request_sent/
一個鏈接?
abuild.user
是誰我想添加的用戶,而user.username
是登錄的人。
你可以將它添加到查詢字符串在您的模板中,或者使用帶有GET操作的表單。 – Evert