Django:渲染时出现 NoReverseMatch 错误

发布于 2024-11-17 18:54:09 字数 2186 浏览 2 评论 0原文

我似乎收到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-24 18:54:09

client.pknote.pk 是空值,因此它们与正则表达式不匹配。

The client.pk and note.pk are empty values, so they don't match the regex.

别闹i 2024-11-24 18:54:09

(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 like url(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 from django.conf.urls.defaults

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文