javascript - 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 中的上下文)
解决方案
因为在您的 else 语句中 group_id 没有。因此,您应该将该字段指定为无。
推荐阅读
- c# - 如何围绕某个点旋转但停在某个角度?
- scala - Scala - 实例化类时处理空字符串
- node.js - 如何在 ejs 模板中显示嵌套数组值?
- c# - 无法解析符号“HasRequired”实体框架核心
- javascript - 如何在长数字上轻松移动逗号
- python - 替换双引号外的逗号 - Python - 正则表达式
- asdf-vm - 如何将 asdf 与 haskell 一起使用
- system - “at”在分布式系统中是什么意思?
- connection - 如何在 2 台设备(如 torrent)之间建立 p2p 连接
- reactjs - 反应倒数计时器没有很好地减少,它不一致地改变数字