【问题标题】:NoReverseMatch: Reverse for 'edit' with keyword arguments '{'title': ''}' not foundNoReverseMatch:使用关键字参数'{'title':''}'反转'edit'
【发布时间】:2021-07-22 23:04:40
【问题描述】:

这是我在previous question 中询问的同一问题的延伸。

我现在遇到的问题是,在解决了 trans 视图功能的问题并期望程序运行没有问题之后,我收到以下错误

NoReverseMatch at /wiki/Python/edit

Reverse for 'edit' with keyword arguments '{'title': ''}' not found.

考虑到我 AM 在 html 中传递标题参数并在 urls.py 中设置编辑视图函数和 urlpath 以期望标题,我这辈子都无法理解论据。

当我仔细查看回溯时,它会突出显示编辑视图功能中的这段代码。

return render(request, "encyclopedia/edit.html", {
    "form": EntryForm(initial={
        "title": title,
        "content": entry
    })
})

但我不明白这里的问题是什么?

这是我正在进行的项目的最后一部分,因此我将非常感谢任何和所有帮助。

下面是相关代码。

HTML

entry.html

<!-- The edit button in the entry's page -->
<form action="{% url 'wiki:trans' %}" method="POST">
    {% csrf_token %}
    <input type=hidden value={{title}} name="title">
    <input type=submit value="Edit">
</form>

edit.html

<!-- the edit page -->
{% block body %} 
<h1>Edit Entry</h1>
<br>
<form action="{% url 'wiki:edit' title=title %}" method="POST">
    {% csrf_token %}
    {{form}}
    <input type="submit" value="Save" id="save">
</form>
{% endblock %}

view.py

class EntryForm(forms.Form):
    title = forms.CharField(label="Title")
    content = forms.CharField(widget=forms.Textarea)

def trans(request):
    title = request.POST.get("title")
    return redirect("wiki:edit", title=title)

def edit(request, title):
    if request.method == "GET":
        entry = util.get_entry(title)

        return render(request, "encyclopedia/edit.html", {
            "form": EntryForm(initial={
                "title": title,
                "content": entry
            })
        })
    else:
        form = EntryForm(request.POST)

        if form.is_valid():
            title = form.cleaned_data["title"]
            content = form.cleaned_data["content"]

        util.save_entry(title, content)
        return redirect("wiki:title", title=title)

urls.py

app_name = "wiki"
urlpatterns = [
    path("", views.index, name="index"),
    path("search", views.search, name="search"),
    path("new", views.new, name="new"),
    path("trans", views.trans, name="trans"),
    path("<str:title>/edit", views.edit, name="edit"),
    path("random", views.rand, name="random"),
    path("<str:title>", views.title, name="title")
]

【问题讨论】:

标签: python django


【解决方案1】:
def edit(request, title):
    if request.method == "GET":
        entry = util.get_entry(title)

        return render(request, "encyclopedia/edit.html", {
            "form": EntryForm(initial={
                "title": title,
                "content": entry
            })
        })

render() 调用没有为title 传递值,因此模板尝试使用空白值,这是不允许的。

【讨论】:

  • 我认为您可以使用默认值稍微更改参数,它会修复错误...看看我的答案。
  • 但是为什么不传值呢?当我从 trans 函数重定向时,我将标题传递给了编辑函数。现在我只是尝试使用render() 将其传递给表单。传递头衔对于手头的任务至关重要。
  • @sunflower.saturn 传递给模板的唯一项目是form。我看到表单有一个名为 title 的字段,但模板希望标题本身是一个单独的元素,而不是表单的一部分。
【解决方案2】:

您传递给title=title dict 的title 变量为空,这意味着标题变量没有数据。要修复它,请确保标题具有值或将其更改为 url 模式中的可选参数。

您可以通过以下方式修复它:

def edit(request, title=None):

那么title如果不通过就会取一个默认值。

【讨论】:

  • 传递标题绝对是至关重要的,所以我不能用None 代替它。我不明白为什么标题是空的。
  • 然后你必须更新你的问题,因为你知道标题是空的,这就是整个问题,尝试以不同的方式构造你的问题,以便你可以找出变量为空的方式.
猜你喜欢
  • 2021-09-28
  • 2021-11-15
  • 2016-05-01
  • 2012-12-11
  • 1970-01-01
  • 2017-10-01
  • 2013-08-06
  • 2013-12-22
  • 2019-10-03
相关资源
最近更新 更多