未找到反向匹配项
NoReverseMatch found
我使用的是 Django 1.7 & python 3.4
我正在尝试在我的网站上实施关注和取消关注用户,但我被卡住了。
urls.py
url(r'^user/', include('myuserprofile.urls'),),
我的用户配置文件。urls.py
urlpatterns = patterns('',
url(r'^(?P<slug>[^/]+)/$', 'myuserprofile.views.profile', name='profile'),
url(r'^(?P<slug>[^/]+)/follow/$', 'myuserprofile.views.follow', name='follow'),
url(r'^(?P<slug>[^/]+)/unfollow/$', 'myuserprofile.views.unfollow', name='unfollow'),
views.py
@login_required
def follow(request):
myuser = request.user.id
if request.method == 'POST':
to_user = MyUser.objects.get(id=request.POST['to_user'])
rel, created = Relationship.objects.get_or_create(
from_user=myuser.myuserprofile,
to_user=to_user,
defaults={'status': 'following'}
)
else:
return HttpResponseRedirect(reverse('/'))
if not created:
rel.status = 'following'
rel.save()
模板部分是这样的:
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' %}{% else %}{% url 'follow' %}{% endif %}" method="POST">
'follow' 的反转,未找到参数“()”和关键字参数“{}”。尝试了 1 种模式:['user/(?P[^/]+)/follow/$']
您需要使用namespaced URL。
在您的情况下,URL unfollow
应引用为 <app_name>:unfollow
.
URL namespaces allow you to uniquely reverse named URL patterns even
if different applications use the same URL names. It’s a good practice
for third-party apps to always use namespaced URLs (as we did in the
tutorial). Similarly, it also allows you to reverse URLs if multiple
instances of an application are deployed. In other words, since
multiple instances of a single application will share named URLs,
namespaces provide a way to tell these named URLs apart
看看这个Here
你必须使用 URL namespaces
这是我的
polls/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
...
)
所以我们使用了
{% url 'polls:index' %}
您应该将用户的用户名添加到 follow/unfollow:
{% url 'follow' user_to_follow.username %}
将 urls.py
更改为:
urlpatterns = patterns('myuserprofile.views',
url(r'^(?P<username>[^/]+)/$', 'profile', name='profile'),
url(r'^(?P<username>[^/]+)/follow/$', 'follow', name='follow'),
url(r'^(?P<username>[^/]+)/unfollow/$', 'unfollow', name='unfollow'),
)
并且视图的签名应该接受 username
参数:
@login_required
def follow(request, username):
myuser = request.user.id
if request.method == 'POST':
to_user = MyUser.objects.get(username=username)
...
您缺少模板中的别名。
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' %}{% else %}{% url 'follow' %}{% endif %}" method="POST">
应该是
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' user.username %}{% else %}{% url 'follow' user.username %}{% endif %}" method="POST">
我使用的是 Django 1.7 & python 3.4 我正在尝试在我的网站上实施关注和取消关注用户,但我被卡住了。 urls.py
url(r'^user/', include('myuserprofile.urls'),),
我的用户配置文件。urls.py
urlpatterns = patterns('',
url(r'^(?P<slug>[^/]+)/$', 'myuserprofile.views.profile', name='profile'),
url(r'^(?P<slug>[^/]+)/follow/$', 'myuserprofile.views.follow', name='follow'),
url(r'^(?P<slug>[^/]+)/unfollow/$', 'myuserprofile.views.unfollow', name='unfollow'),
views.py
@login_required
def follow(request):
myuser = request.user.id
if request.method == 'POST':
to_user = MyUser.objects.get(id=request.POST['to_user'])
rel, created = Relationship.objects.get_or_create(
from_user=myuser.myuserprofile,
to_user=to_user,
defaults={'status': 'following'}
)
else:
return HttpResponseRedirect(reverse('/'))
if not created:
rel.status = 'following'
rel.save()
模板部分是这样的:
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' %}{% else %}{% url 'follow' %}{% endif %}" method="POST">
'follow' 的反转,未找到参数“()”和关键字参数“{}”。尝试了 1 种模式:['user/(?P[^/]+)/follow/$']
您需要使用namespaced URL。
在您的情况下,URL unfollow
应引用为 <app_name>:unfollow
.
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It’s a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed. In other words, since multiple instances of a single application will share named URLs, namespaces provide a way to tell these named URLs apart
看看这个Here
你必须使用 URL namespaces
这是我的
polls/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
...
)
所以我们使用了
{% url 'polls:index' %}
您应该将用户的用户名添加到 follow/unfollow:
{% url 'follow' user_to_follow.username %}
将 urls.py
更改为:
urlpatterns = patterns('myuserprofile.views',
url(r'^(?P<username>[^/]+)/$', 'profile', name='profile'),
url(r'^(?P<username>[^/]+)/follow/$', 'follow', name='follow'),
url(r'^(?P<username>[^/]+)/unfollow/$', 'unfollow', name='unfollow'),
)
并且视图的签名应该接受 username
参数:
@login_required
def follow(request, username):
myuser = request.user.id
if request.method == 'POST':
to_user = MyUser.objects.get(username=username)
...
您缺少模板中的别名。
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' %}{% else %}{% url 'follow' %}{% endif %}" method="POST">
应该是
<form action="{% if relationship.status == 'F' %}{% url 'unfollow' user.username %}{% else %}{% url 'follow' user.username %}{% endif %}" method="POST">