重定向错误 - NoReverseMatch:使用关键字参数 '{'title': 'Python'}' 反转 'edit' [英] redirect errot - NoReverseMatch: Reverse for 'edit' with keyword arguments '{'title': 'Python'}' not found

查看:23
本文介绍了重定向错误 - NoReverseMatch:使用关键字参数 '{'title': 'Python'}' 反转 'edit'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款允许用户创建、显示和编辑条目的应用.现在我正在研究编辑功能.我想要做的是有一个编辑按钮(这实际上是一个通过隐藏输入发送数据的表单),将条目的标题发送到名为 trans 的视图,其唯一目的是重定向到编辑视图,我这样做的原因这样,当我在编辑视图上工作时,我可以简化过程,如果请求方法是 GET,它会显示带有表单的页面,用户可以在其中编辑条目,如果它发布,则编辑视图可以接收更改并保存它们,而不必担心从条目页面重定向.

问题在于每次单击编辑按钮时都会出现错误:

NoReverseMatch at/wiki/trans使用关键字参数 '{'title': 'Python'}' 反转 'edit' 未找到.

我一遍又一遍地检查了 urls.py 中的任何拼写错误或问题,或者任何命名问题,但我就是找不到错误.这令人沮丧,因为我认为这将是项目中最简单的部分.

以下是相关代码.如果有人指出我做错了什么,我将不胜感激.提前致谢.

HTML

<form action="{% url 'wiki:trans' %}";方法=POST">{% csrf_token %}<输入类型=隐藏值={{title}} name=title"><输入类型=提交值=编辑"></表单>

views.py

class EntryForm(forms.Form):title = forms.CharField(label=标题")content = forms.CharField(widget=forms.Textarea)定义传输(请求):title = request.POST.get("title")返回重定向(维基:编辑",标题=标题)定义编辑(请求,标题):如果 request.method == GET":entry = util.get_entry(title)返回渲染(请求,百科全书/edit.html",{表单":EntryForm({内容":条目,标题":标题})})别的:form = EntryForm(request.POST)如果 form.is_valid():title = form.cleaned_data[title"]content = form.cleaned_data[内容"]util.save_entry(标题,内容)返回重定向(维基:标题",标题=标题)

urls.py

app_name = wiki"网址模式 = [路径(",views.index,名称=索引"),路径(搜索",views.search,名称=搜索"),路径(新",views.new,名称=新"),路径(反",views.trans,名称=反"),路径(编辑",views.edit,名称=编辑"),路径(随机",views.rand,名称=随机"),路径(

解决方案

尝试在每个 url 的末尾添加一个 /,如下所示:

urls.py:

app_name = wiki"网址模式 = [路径(",views.index,名称=索引"),路径(搜索/",views.search,名称=搜索"),路径(新/",views.new,名称=新"),路径(trans/",views.trans,名称=trans"),路径(编辑/",views.edit,名称=编辑"),路径(随机/",views.rand,名称=随机"),路径(<str:title>/",views.title,name=title")]

还可以尝试使用 reverse 并在 views.py 上将标题作为参数传递:

 return redirect(reverse('wiki:title', args=[title]))

I'm working on an app that allows the user to create, show and edit an entry. right now i'm working on the editing function. what i'm trying to do is have an edit button (that's actually a form that sends data via hidden inputs) that sends the title of the entry to a view called trans whose sole purpose is redirect to the edit view, the reason i did this is so that when i'm working on the edit view, i can simplify the process where if the request method is GET it shows the page with the form where the user can edit the entry and if it's post the edit view can receive the changes and save them without worrying about the redirecting from the entry's page.

The problem lies in the fact that everytime i click the edit button i get the error:

NoReverseMatch at /wiki/trans
Reverse for 'edit' with keyword arguments '{'title': 'Python'}' not found.

I have checked over and over for any misspellings or issues in urls.py or any problems with the naming but i just can't find the bug. and it's frustrating because i thought that this would be the easiest part of the project.

Below is the relevant code. i would be extremely grateful for anyone who points out what i'm doing wrong. Thank you in Advance.

HTML

<div id="edit">
    <form action="{% url 'wiki:trans' %}" method="POST">
        {% csrf_token %}
        <input type=hidden value={{title}} name="title">
        <input type=submit value="Edit">
    </form>
</div>

views.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({
                "content": entry,
                "title": title
            })
        })
    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("edit", views.edit, name="edit"),
    path("random", views.rand, name="random"),
    path("<str:title>", views.title, name="title")
]

解决方案

Try adding a / at the end of each url, like this:

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("edit/", views.edit, name="edit"),
    path("random/", views.rand, name="random"),
    path("<str:title>/", views.title, name="title")
]

Also try using reverse and passing in the title as an arg on your views.py:

    return redirect(reverse('wiki:title', args=[title]))

这篇关于重定向错误 - NoReverseMatch:使用关键字参数 '{'title': 'Python'}' 反转 'edit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆