在导致 NoReverseMatch 错误的 HTML 表单中调用视图作为操作属性的值

Calling a view as a value for action's attribute in HTML forms that leads to a NoReverseMatch error

我正在使用 django 创建一个 Web 应用程序,现在我想添加一个修改条目的视图(我的 Web 应用程序是一个百科全书,所以该视图让我们可以编辑页面)。

我们可以通过点击此 html 页面的 link 来访问编辑页面:

{% extends "encyclopedia/layout.html" %}

{% block title %}
    {{ title }}
{% endblock %}

{% block body %}
    {{ html | safe }}
    {% if exists %}
        <br><br><br><br>
            <a href="{{ address }}">Edit encyclopedia</a>
    {% endif %}
{% endblock %}

所以 django 会经历这个 url

urlpatterns = [
    ...
    ...
    ...
    path("<str:title>/edit", views.edit, name="edit"),
]    

那么,这个 url 应该把我们带到这个视图:

def edit(request, title):
    if request.method == "POST":
        form = NewForm(request.POST)
        if form.is_valid():
            with open(f"entries/{title}.md", "w") as file:
                file.write(form.cleaned_data["content"])
                return redirect(reverse("encyclopedia:display"))
        else:
            return render(request, "encyclopedia/edit.html",{
                'form' : NewForm(),
                'message' : """<div class="alert alert-danger" role="alert">
                            Your entries are empty.
                            </div>"""
            })
    markup = util.get_entry(title)[0]
    print(request.)
    return render(request, "encyclopedia/edit.html",{
        'form'  : NewForm(),
        'title' : title,
        'markup': markup,
    })

这是我的 html 文件:

{% extends "encyclopedia/layout.html" %}

{% block title %}
    Edit
{% endblock %}

{% block body %}
    <h1>New Encyclopedia</h1>
    <p>Our websites' encyclopedias are written in a langage names Markdow<br>
    You may have additionnal informations about this language <a href="https://docs.github.com/en/free-pro-team@latest/github/writing-on-github/basic-writing-and-formatting-syntax">here</a>.</p>
    <form action="{% url 'encyclopedia:edit' %}" method="POST" style="display: block; text-align: center; padding: 20px;">
        {% csrf_token %}
        <p style="margin: 15px;">{{ title }}</p>
        <textarea rows='10' placeholder="Encyclopedia's content" name="content" style="width: 90%; margin: 15px;">{{ markup }}</textarea>
        <input type="submit" value="Edit" style="width:15%">
    </form>
{% endblock %}

但我的问题是,当我 运行 我的应用程序转到编辑页面时,我收到一个 NoReverseMatch 错误,就像这个:

NoReverseMatch at /wiki/Django/edit Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P[^/]+)/edit$']

我认为这个问题 link 是因为我在表单中调用编辑视图时没有给出标题参数,但我不知道该怎么做。

如果有人能帮助我那就太棒了,我做了很多研究,但无法真正理解如何解决这个问题...

我终于找到了错误的根源,它可能对谁有帮助,是我忘记添加 'title' 变量(我认为这是必须的),就像这样:

else:
      return render(request, "encyclopedia/edit.html",{
          'form'   : NewForm(),
          'message': """<div class="alert alert-danger" role="alert">
                      Your entries are empty.
                      </div>""",
          'title'  : title,
      })

is_valid() 方法并不真正对应我想如何处理数据,我已经用这个函数替换了它:

undecodables = ["è", "à", "é"]

def own_validation(string):
    try:
        valid = False
        for i in string:
            if i.isalnum : valid = True
            assert i not in undecodables 
    except AssertionError:
        return False
    else:
        return valid

我不知道所有不可解码的字符,所以列表将在以后完成。

感谢@iklinac的帮助!