Django:渲染时出现 NoReverseMatch 错误
我似乎收到了 Caught NoReverseMatch 错误。我不太确定是什么导致了这个问题。查看完整的错误。
Caught NoReverseMatch while rendering: Reverse for 'mmc.views.edit_note' with arguments '(1L, '')' and keyword arguments '{}' not found.
在我的 get_client 页面上。我有一个指向编辑注释页面的链接。我假设问题可能出在我的模板中。我认为 note.pk 是问题所在。
<a href="{% url mmc.views.edit_notes client.pk note.pk %}"> Edit Note</a>
这里还有一些可能有帮助的更多信息。 urls.py
(r'^clients/(?P<client_id>\d+)/$', views.get_client),
(r'^clients/notes/(?P<client_id>\d+)(?P<note_id>\d+)$', views.edit_notes),
views.py
@login_required
def edit_notes(request, client_id = 0, note_id = 0):
client = None
note = None
try:
client = models.Client.objects.get(pk = client_id)
note = models.Note.objects.get(pk = note_id)
except:
return HttpResponseNotFound()
if request.method == 'POST':
form = forms.NoteForm(request.POST, instance=note)
if form.is_valid():
note = form.save(commit=False)
note.user = request.user
note.client = client
note.save(True)
request.user.message_set.create(message = "Note is successfully added.")
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
else:
form = forms.NoteForm(instance=note)
return render_to_response('note_form.html', {'form':form, 'client':client, 'note':note}, context_instance = RequestContext(request))
*编辑:* 似乎已经纠正了大部分内容以下是我所做的一些更改。
模板
{% for note in notes %}
<a href="{% url mmc.views.edit_note client.pk note.pk %}" onclick="return showAddAnotherPopup(this);"> Edit Note</a>
{% endfor%}
urls.py
(r'^clients/notes/(?P<client_id>\d+)/(?P<note_id>\d+)/$', views.edit_note)
现在唯一的问题是它显示单个客户的每个编辑表单注释的所有链接。我只想要最新笔记的链接,并且只想要最新笔记。有可能的办法吗?
I seem to be getting a Caught NoReverseMatch error. I am not so sure what is causing the problem. Have a look at the full error.
Caught NoReverseMatch while rendering: Reverse for 'mmc.views.edit_note' with arguments '(1L, '')' and keyword arguments '{}' not found.
On my get_client page. I have a link to the edit note page. I am assuming the problem might be here in my template. I think the note.pk is the problem.
<a href="{% url mmc.views.edit_notes client.pk note.pk %}"> Edit Note</a>
Here is also some more information which could help.
urls.py
(r'^clients/(?P<client_id>\d+)/
views.py
@login_required
def edit_notes(request, client_id = 0, note_id = 0):
client = None
note = None
try:
client = models.Client.objects.get(pk = client_id)
note = models.Note.objects.get(pk = note_id)
except:
return HttpResponseNotFound()
if request.method == 'POST':
form = forms.NoteForm(request.POST, instance=note)
if form.is_valid():
note = form.save(commit=False)
note.user = request.user
note.client = client
note.save(True)
request.user.message_set.create(message = "Note is successfully added.")
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
else:
form = forms.NoteForm(instance=note)
return render_to_response('note_form.html', {'form':form, 'client':client, 'note':note}, context_instance = RequestContext(request))
*EDIT: * Seem to have corrected most of it Here are some changes I have made.
Template
{% for note in notes %}
<a href="{% url mmc.views.edit_note client.pk note.pk %}" onclick="return showAddAnotherPopup(this);"> Edit Note</a>
{% endfor%}
urls.py
(r'^clients/notes/(?P<client_id>\d+)/(?P<note_id>\d+)/
Now the only problem is it displays all of the links to each edit form notes for an individual client. I only want the link for the latest note and only the latest note. Is there a possible way?
, views.get_client),
(r'^clients/notes/(?P<client_id>\d+)(?P<note_id>\d+)
views.py
*EDIT: * Seem to have corrected most of it Here are some changes I have made.
Template
urls.py
Now the only problem is it displays all of the links to each edit form notes for an individual client. I only want the link for the latest note and only the latest note. Is there a possible way?
, views.edit_notes),
views.py
*EDIT: * Seem to have corrected most of it Here are some changes I have made.
Template
urls.py
Now the only problem is it displays all of the links to each edit form notes for an individual client. I only want the link for the latest note and only the latest note. Is there a possible way?
, views.edit_note)
Now the only problem is it displays all of the links to each edit form notes for an individual client. I only want the link for the latest note and only the latest note. Is there a possible way?
, views.get_client), (r'^clients/notes/(?P<client_id>\d+)(?P<note_id>\d+)views.py
*EDIT: * Seem to have corrected most of it Here are some changes I have made.
Template
urls.py
Now the only problem is it displays all of the links to each edit form notes for an individual client. I only want the link for the latest note and only the latest note. Is there a possible way?
, views.edit_notes),views.py
*EDIT: * Seem to have corrected most of it Here are some changes I have made.
Template
urls.py
Now the only problem is it displays all of the links to each edit form notes for an individual client. I only want the link for the latest note and only the latest note. Is there a possible way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
client.pk
和note.pk
是空值,因此它们与正则表达式不匹配。The
client.pk
andnote.pk
are empty values, so they don't match the regex.(r'^clients/(?P\d+)/$',views.get_client)
应该类似于url(r'^clients/(?P; \d+)/$',views.get_client, name='MY_URL_NAME')
然后使用{% url MY_URL_NAME client.pk 调用%}
并从
django.conf.urls.defaults
导入url
(r'^clients/(?P<client_id>\d+)/$', views.get_client)
should be something likeurl(r'^clients/(?P<client_id>\d+)/$', views.get_client, name='MY_URL_NAME')
then called with{% url MY_URL_NAME client.pk %}
and import
url
fromdjango.conf.urls.defaults