【问题标题】:Reverse for 'entrypage' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<title>[^/]+)$']未找到任何参数的“entrypage”反向。尝试了 1 种模式:['wiki/(?P<title>[^/]+)$']
【发布时间】:2022-01-17 17:29:04
【问题描述】:
VIEWS.PY
from django.shortcuts import render
from django.shortcuts import redirect
from django.urls import reverse
from django.http import HttpResponseRedirect
from django import forms
import markdown2

from . import util

class AddPageForm(forms.Form):
    title = forms.CharField(max_length=20)
    content = forms.CharField(widget=forms.Textarea(
        attrs={
            "class": "form-control",
            "placeholder": "Tell us more!"
        })
    )


def add_page(request):
    if request.method == "POST":
        form = AddPageForm(request.POST)
        entries = util.list_entries()
    
    if form.is_valid():
        title = form.cleaned_data['title']
        content = form.cleaned_data['content']
        util.save_entry(title, content)
        
        for entry in entries:
            if title.upper() == entry.upper():
                return render(request, "encyclopedia/errorpage.html")
            else:
                return HttpResponseRedirect(reverse('encyclopedia:entrypage'))
else:
    return render(request, "encyclopedia/addpage.html", {
        "form": AddPageForm()
    })

URLS.PY

app_name = "encyclopedia"

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:title>", views.entry_page, name="entrypage"),
    path("search", views.search, name="search"),
    path("add_page", views.add_page, name="addpage"),
]

ADDPAGE.HTML

        <form action="{% url 'encyclopedia:addpage' %}" method="post">
        {% csrf_token %}
        {{ form }}
        <input type="submit" value="Submit" class="btn btn-secondary">
    </form>

布局.HTML

                <div>
                <a href="{% url 'encyclopedia:addpage' %}">Create New Page</a>
            </div>
            <div>

我已尝试更新此网址和视图,但我不断收到错误响应

    path("add_page/<str:title>", views.add_page, name="addpage"),
    def add_page(request, title):

请告知此错误响应可能来自何处,因为上述编辑是我在其他一些 stackoverflow 响应中看到的以清除错误,但这对我不起作用。

谢谢

【问题讨论】:

  • 向我们展示 entry_page 视图
  • 谢谢,我收到了下面的回复,并且已经能够解决错误。谢谢,danke,obrigado

标签: django django-views django-forms django-templates


【解决方案1】:

当你重定向到entrypage时,你需要指定标题,所以:

from django.shortcuts import redirect

def add_page(request):
    if request.method == "POST":
        form = AddPageForm(request.POST)
        entries = util.list_entries()
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']
            util.save_entry(title, content)
        
            for entry in entries:
                if title.upper() == entry.upper():
                    return render(request, "encyclopedia/errorpage.html")
            #                                   specify title &downarrow;
            return redirect('encyclopedia:entrypage', title=title)
    # …

我也强烈建议使用数据库,并且不要从实用程序中获取所有条目:数据库经过优化以有效搜索,而访问文件列表需要线性时间来搜索.如果你定义了一个数据库模型,你可以添加db_index=True [Django-doc]来建立一个索引,可以极大地促进搜索。

【讨论】:

  • 感谢您的回复。我正在从 util 获取条目,因为这是一项任务,并且 CS50W 已经为我们提供了它以供我们使用。我试过了,它奏效了。好的,我很兴奋,非常感谢你。您能否解释一下为什么这不适用于 HttpResponseRedirect(反向)。此外,我们是否应该在使用重定向时始终指定标题。
  • 当 Django 使用 reverse 方法尝试使用您在视图中提供的参数获取您的 url 时,在您的情况下,您添加 title 参数但您忘记指定它在您的 redirect 中,Django 找不到任何与您的反向匹配匹配的 url,请参见此处:docs.djangoproject.com/en/4.0/ref/urlresolvers/…
  • @Chuks: redirect 是一个“快捷方式”,它调用reverse,然后将结果包装在HttpResponseRedirect 中。因此它更优雅一些。
  • @Chuks:我最近写了一篇关于两者区别的文章:django-antipatterns.com/difference-between/…
猜你喜欢
  • 2021-02-05
  • 2020-11-26
  • 2021-12-12
  • 2020-05-13
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多