我希望此代码显示用户的朋友列表,要添加的朋友列表,在编写代码并测试后,我收到此错误:
TypeError at /userpage/
user_page() takes exactly 2 arguments (1 given)
Request Method: GET
Request URL: http://127.0.0.1:8000/userpage/
Django Version: 1.3.1
Exception Type: TypeError
Exception Value: user_page() takes exactly 2 arguments (1 given)
Exception Location: C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response, line 111
Python Executable: C:\Python27\python.exe
Python Version: 2.7.2
以下是我的代码: 的观点:
def user_page(request, username):
user=get_object_or_404(User, username=username)
if request.user.is_authenticated():
is_friend=Friendship.objects.filter(
from_friend=request.user,
to_friend=user
)
else:
is_friend=False
return render_to_response('user_page.html', context_instance=RequestContext(request))
模板:
{% extends "base.html" %}
{% block content %}
{% ifequal user.username username %}
<a href="/friends/{{ username }}/">view your friends</a>
{% else %}
{% if is_friend %}
<a href="/friends/{{ user.username }}/">
{{ username }} is a friend of yours</a>
{% else %}
<a href="/friend/add/?username={{ username }}">
add {{ username }} to your friends</a>
{% endif %}
- <a href="/friends/{{ username }}/">
view {{username }}'s friends</a>
{% endifequal %}
{% endblock %}
请帮帮我!
答案 0 :(得分:1)
错误消息是user_page() takes exactly 2 arguments (1 given)
,它告诉您问题是什么 - 可能是您的urlconf希望在URL中/userpage/
之后有一个用户名,但是当您不在时调用user_page()
但没有第二个参数。