Django - 反向不工作,我不知道为什么

时间:2016-08-01 04:19:43

标签: django

我之前使用过reverse并且它已经很好了。但是现在,我不知道它为什么不起作用。这可能是因为我尝试reverse基于类的视图,而我之前从未做过

这是我的代码 - 我在哪里搞砸了?

views.py:

class DocumentRequestDetail(NavMixin, TitleMixin, SelectedBrokerage, DetailView):
   model = DocumentRequest
   mod_nav = 'brokerage_nav.html'
   context_object_name = 'doc'
   subtitle = 'Document Request Details'


def MarkDocRequestComplete(request, bpk, pk):                                                    
   d = DocumentRequest.objects.get(pk= pk)                                                        
   d.is_complete = True                                                                            
   d.save()
   return reverse('doc_request_detail', args=[bpk, pk])  #<--- the offending line

urls.py

from django.conf.urls import include, url
  from django.views.generic.list import ListView
  from brokerage.views import *

  urlpatterns = [
    url(r'^(?P<pk>[0-9]+)/detail/$', BrokerageDetail.as_view(), name="brokerage_detail"),
    url(r'^(?P<pk>[0-9]+)/edit/$', BrokerageEdit.as_view(), name="brokerage_edit"),

    url(r'^(?P<bpk>[0-9]+)/doc-request/all/$', DocumentRequestList.as_view(), name="doc_request_list"),
    url(r'^(?P<bpk>[0-9]+)/doc-request/(?P<pk>[0-9]+)/$', DocumentRequestDetail.as_view(), name="doc_request_detail"),

    url(r'^(?P<bpk>[0-9]+)/mark-doc-request-complete/(?P<pk>[0-9]+)/$', MarkDocRequestComplete, name="mark_doc_request_complete"),
  ]

调用MarkDocRequestComplete的HTML链接:

<a href="{% url 'brokerage:mark_doc_request_complete' brokerage.pk doc.pk %}" class='btn btn-lg btn-wide btn-success'>
<i class='ti-check'></i>
Mark Complete
</a>
单击HTML链接时出现

错误:

Reverse for 'doc_request_detail' with arguments '(u'1', u'19')' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我不明白......我做错了什么?

修改我应该说,在错误中,119bpkpk的正确值。

编辑2 已添加完整的URLs.py

2 个答案:

答案 0 :(得分:0)

您的网址标记不起作用的原因是,当您的网址中没有设置名称空间时,您通过向其添加AppState.addEventListener('change', state => { if (state === 'active') { // do this } else if (state === 'background') { // do that } else if (state === 'inactive') { // do that other thing } }); 来命名空间。有关如何正确设置名称空间的信息,请参阅Drag and drop items in RecyclerView with GridLayoutManager

正确的标签应为:

brokerage:

答案 1 :(得分:-1)

你必须改变:

return reverse('doc_request_detail', args=[bpk, pk])

return reverse('doc_request_detail', kwargs={'bpk': bpk, 'pk': pk})

您还必须从以下位置更改模板标记:

<a href="{% url 'brokerage:mark_doc_request_complete' brokerage.pk doc.pk %}" class='btn btn-lg btn-wide btn-success'>

为:

<a href="{% url 'brokerage:mark_doc_request_complete' bpk=brokerage.pk pk=doc.pk %}" class='btn btn-lg btn-wide btn-success'>

这些更改的原因是您的正则表达式组中有名称。