【问题标题】:Form action doesn't work | Django表单操作不起作用 |姜戈
【发布时间】:2014-12-07 23:11:32
【问题描述】:

您好,我制作了一些 ModelForm。现在当我进入时,我得到了这个错误。

RuntimeError
Exception Value:    

You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/comp/new/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

这是什么原因造成的?可能表单操作与表单 URL comp/new 完全匹配,这与我的视图匹配以呈现和处理数据。但是出了点问题……

我的网址

url(r'^comp/new/$', 'company.views.add_company', name='add_company'),

我的表单操作

<form id="category_form" method="post" action="/comp/new/">

【问题讨论】:

  • 最后需要一个 /。您的表单操作显然没有,就像您在帖子中所说的那样。 /comp/new/ - 错误信息会告诉你究竟出了什么问题。
  • 我试图在上面加上 /comp/new/ 这样的斜线,但没有用。请求网址:127.0.0.1:8000/comp/new 不变。
  • 向我们展示您的表单代码 - 它的 HTML。
  • 您也可以发表您的观点吗?并将您的表单模板更改为:&lt;form id="category_form" method="post" action="{% url 'add_company' %}"&gt;
  • 它现在可以工作了。这是动作的反斜杠。浏览器不会重新加载新的 html。谢谢

标签: django forms exception


【解决方案1】:

您可能正在使用reverse,因此请检查您的 urls.py,缺少的 / 应该在那里

【讨论】:

    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 2020-11-14
    • 2017-10-15
    • 2017-09-20
    • 1970-01-01
    • 2012-10-23
    • 2017-10-06
    • 1970-01-01
    相关资源
    最近更新 更多