在Django表单中设置Action属性的正确方法是什么

在Django表单中设置Action属性的正确方法是什么,django,Django,我在Django系统中有一个html表单,我找不到在表单中实现Action属性的正确方法 html <form action="stages" method="post"> 单击“提交”时出现的错误是: RuntimeError at /duo/2/stages/stages You called this URL via POST, but the URL doesn't end in a slash and you have APPEND

我在Django系统中有一个html表单,我找不到在表单中实现Action属性的正确方法

html

<form action="stages" method="post">
单击“提交”时出现的错误是:

RuntimeError at /duo/2/stages/stages
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set...
我尝试了其他各种URL,但都给出了错误消息

[编辑200828] 附加斜杠消息具有误导性。这只是我从html中得到的信息

<form action="stages" method="post">

如果我将html更改为

<form action={% url 'stages' partner_pk %} method="post">


我明白了

NoReverseMatch at/duo/2/stages/'


您还没有设置APPEND_SLASH选项,所以所有路由都应该有/after,但它仍然不起作用,因为您正在追加相对路径(请注意URL中的两个阶段)

考虑到您在同一路线上发布,您可以省略操作属性

<form   method="post">

或者使用类似的东西(因为HTML5标准不允许空操作)


此外,您还可以使用像这样的完整路径

<form action="{% url 'stages' partner_pk %}"  method="post">

本例中的问题是相反的(url)

我将views.py中的post方法更改为

def post(self, request, partner_pk):
    """Render the POST request."""
    stages_selected = app.update_stages(request)
    if app.context['use_stages'] and not stages_selected:
        messages.error(request, 'You have not selected any stages.')
        return render(request, f'{partner_pk}', app.context)
    else:
        return HttpResponseRedirect(f'/duo/{partner_pk}')

而且很有效

谢谢。但是所有这些解决方案都会在/duo/2/stages/处出现错误
NoReverseMatch。您需要检查此设置。谢谢,但这似乎并不相关-请参阅我更新的问题
<form   method="post">
<form  action='?' method="post">
<form action="{% url 'stages' partner_pk %}"  method="post">
def post(self, request, partner_pk):
    """Render the POST request."""
    stages_selected = app.update_stages(request)
    if app.context['use_stages'] and not stages_selected:
        messages.error(request, 'You have not selected any stages.')
        return render(request, f'{partner_pk}', app.context)
    else:
        return HttpResponseRedirect(f'/duo/{partner_pk}')