最奇怪的是,我刚刚从程序的工作部分复制了以下代码,只是切换了名称,所有基础都相同。
错误:
NoReverseMatch at /profile/1/
Reverse for 'follow_user_add' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['profile/follow/']
ulrs.py:
url(r'^profile/', include('basics.urls')),
基础/ urls.py:
url(r'^follow/', 'basics.views.add_user_follower', name='follow_user_add'),
url(r'^(?P<pk>\d+)/', UserDetailView.as_view(), name='users_detail'),
url(r'^', UserListView.as_view(), name='users_list'),
views.py:
class UserDetailView(DetailView):
model = User
template_name = 'basics/profile_detail.html'
context_object_name = 'user_object'
def get_context_data(self, **kwargs):
context = super(UserDetailView, self).get_context_data(**kwargs)
context["member_list"] = Member.objects.filter(user=self.object)
return context
def add_user_follower(request, pk ):
if request.method == 'POST':
form = TempForm(request.POST or None)
if form.is_valid():
...
return HttpResponseRedirect(followed_user.get_url())
else:
form = TempForm() # An unbound form
return render(request, 'basics/profile_detail.html', {
'form': form,
})
和模板:
<form action="{% url 'follow_user_add' user_object.id %}" method="post">{% csrf_token %}
<input type="submit" value="follow" />
</form>
答案 0 :(得分:2)
我刚想出你的错误,你给了这样的网址,
url(r'^follow/', 'basics.views.add_user_follower', name='follow_user_add'),
但是在这样的模板中,
action="{% url 'follow_user_add' user_object.id %}"
所以上面的网址就像这样/profile/follow/1/
匹配。但是您的网址/profile/follow/
。
在这样的观点中,
def add_user_follower(request, pk ):
你得到了这个错误,
Reverse for 'follow_user_add' with arguments '(1,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['profile/follow/']
我的解决方案是你将网址与pk匹配,但是你没有那个问题。所以你需要这样做,
url(r'^follow/(?P<pk>\d+)/$', 'basics.views.add_user_follower', name='follow_user_add'),
注意:你基本上错过了(?P<pk>\d+)
pattren。希望这会对你有所帮助。
更新:以这种方式更改网址,
url(r'^follow/(?P<pk>\d+)/$', 'basics.views.add_user_follower', name='follow_user_add'),
url(r'^(?P<pk>\d+)/$', UserDetailView.as_view(), name='users_detail'),
url(r'^$', UserListView.as_view(), name='users_list'),