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]+)/$']

在添加上下文的EDIT之后['group_id']=createview上的None得到了这个:

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%}可以处理这种情况。

当组Id不存在时,我很确定它是我的url 'ipaswdb:grouplocation_add group_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("") 

假设一旦页面加载,group_id就会出现,如果它是从UpdateView加载的,那么我是否需要HttpResponseRedirect以某种方式转到UpdateView?如何一步到位地获取从创建视图保存的模型并将其传递到更新视图?

1) 所以我猜有两件事:当它在if-else块中时,为什么会出现错误。

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

因为在else语句中group_id为none。因此,您应该将该字段指定为none。

相关内容

最新更新