首页 > 解决方案 > NoReverseMatch at /blah/blah/add 虽然我认为我的模板 if/else/endif 可以解决错误

问题描述

我不断收到错误:

NoReverseMatch for 'grouplocation_add' with arguments '('',)' not found. 1 pattern(s) tried: [u'ipaswdb/grouplocations/add/(?P<pk>[0-9]+)/$']

编辑添加 context['group_id']=None on createview 后得到这个:

NoReverseMatch: Reverse for 'grouplocation_add' with arguments '(None,)' not found. 1 pattern(s) tried: [u'ipaswdb/grouplocations/add/(?P<pk>[0-9]+)/$']

图案:

url(r'^grouplocations/add/(?P<pk>[0-9]+)/$', GroupLocationCreateView.as_view(), name='grouplocation_add'),

同样,如果它是 group_id 存在的更新,它可以工作......如果它是创建新的,但我认为我的 {% if ... %} 和 {% else %} 会处理这种情况。

我很确定url 'ipaswdb:grouplocation_add group_id当组 ID 不存在时是我的(如果在发生更新时调用它,但是创建一个新组,还没有 ID。这个按钮是为了做更多的事情已经创建的组。我的 If else 试图说“直到表单被提交,因此模型被保存,因此对模型进行更新视图....”不要启用让你搞砸的按钮模型的其他部分,直到它被保存一次。

  {% if group_id %}
    <td><a href=""><button disabled type="button" id="group_add_location" class="btn btn-primary">Add A Group 
    Location</button></a> </td>
  {% else %}
    <td><a href="{% url 'ipaswdb:grouplocation_add' group_id %}"><button type="button" id="group_add_location" class="btn btn-primary">Add A Group 
  {% endif %}

鉴于错误显然我的方法是不同的。我还想保存并返回到更新视图而不是创建视图。这是否意味着在我的

 class GroupCreateView(CreateView):
         ..do tons of stuf
      def get_context_data(self, **kwargs): 
        ..do more stuf 
         context['group_id'] = None #added this
      def post(self, request, *args, **kwargs):
         if self.request.POST.has_key('submit'):
            ..DO LOTS OF STUFF..
         return HttpResponseRedirect("") 

鉴于页面加载后,如果从 UpdateView 加载该 group_id 存在,那么我是否需要 HttpResponseRedirect 以某种方式转到 UpdateView?我怎样才能从创建视图中获取保存的模型,并一步将其传递到更新视图?

1)所以我猜两件事:为什么它在 if else 块中时会给出该错误。

2)一旦我修复了该错误,我如何通过有效地从创建视图(具有 HttpResponseRedirect 的末端)转身并使用新创建的模型调用 UpdateView 来使 if else 块工作,从而具有 group_id (通过进入 UpdateView 中的上下文)

标签: javascriptdjango

解决方案


因为在您的 else 语句中 group_id 没有。因此,您应该将该字段指定为无。


推荐阅读