【问题标题】:What's wrong with my django URL?我的 django URL 有什么问题?
【发布时间】:2015-09-14 01:12:09
【问题描述】:

来自 urls.py 的行:

url(r'^(?P<customer_profile_id>\d+)/case/(?P<account_type>\w+)/$', view='case', name='case'),

来自 html 的行:

<form method="POST" action="{% url 'case/cash/' %}" id="create_collection_case" target="_blank">

错误:

NoReverseMatch at /admin/customers/1/
Reverse for 'case/cash/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

我只有一个 html 表单 -> 发布到 django url -> 加载 django 视图。加载页面时出现上述错误。如果您需要其他任何问题来解决问题,请告诉我!

【问题讨论】:

    标签: python html django django-admin django-views


    【解决方案1】:

    指定url的名称(case),而不是url的一部分:

    <form method="POST" action="{% url 'case' %}" id="create_collection_case" target="_blank">
    

    【讨论】:

      【解决方案2】:

      如果您希望 django 确定路线,您的正则表达式需要与表单操作完美匹配。请注意\d+ 代表整数,我更喜欢将文本/字符串与([-\w+]+) 匹配。

      为了获得最佳实践,我建议您将 URL 更改为(始终最好将整数查询值放在末尾)

      url(r'^case/(?P<account_type>[-\w+]+)/(?P<customer_profile_id>\d+)/$', view='case', name='case'),
      

      你的表单操作应该这样做,其中 profile_id 是一个整数

      {% url 'case/cash/profile_id/' %}
      #e.g. http://127.0.0.1:8000/case/cash/1/
      

      【讨论】:

        猜你喜欢
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        相关资源
        最近更新 更多