python - django 中的 URL 模式匹配

标签 python django urlconf

当我尝试访问 http://127.0.0.7:8000/edit-paragraph/6/edit/ 时出现找不到页面,出现以下错误:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^login/$
^login/home/
^logout/$
^edit-section/(?P<s_id>\d+)/edit/
^edit-section/(?P<s_id>\d+)
^edit-paragraph/(?P<p_id>\d+)/edit/
The current URL, edit-paragraph/6/, didn't match any of these.

在我的 urls.py 中,我有:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^login/$', 'mysite.auth.views.login_user'),
    (r'^login/home/', 'mysite.auth.views.logged_in'),
    (r'^logout/$', 'mysite.auth.views.logout_user'),
    (r'^edit-section/(?P<s_id>\d+)/edit/', 'mysite.auth.views.edit_section_form'),
    (r'^edit-section/(?P<s_id>\d+)', 'mysite.auth.views.edit_section'),
    (r'^edit-paragraph/(?P<p_id>\d+)/edit/', 'mysite.auth.views.edit_paragraph')

)

对我来说,它看起来很像 url http://127.0.0.7:8000/edit-paragraph/6/edit/应该匹配我的 URLConf 的最后一行。关于我做错了什么的任何想法?它能够匹配相似的 url:

r'^edit-section/(?P<s_id>\d+)/edit/'

提前致谢!


编辑:

原来是重定向。现在我有了这个,浏览器给出了找不到页面的错误:

@login_required
def edit_paragraph(request, p_id):
p = get_object_or_404(Paragraph, id=p_id)

if request.method == 'POST':
    form = ParagraphForm(request.POST)
    if form.is_valid():
        p.title = request.POST['title']
        p.description = request.POST['description']
        p.save()
        return HttpResponse('foo')
    else:
        form = ParagraphForm({ 'title': p.title, 'description': p.description })
        return render_to_response('auth/edit-paragraph.html', { 'form': form }, context_instance=RequestContext(request))

return HttpResponseRedirect('/edit-paragraph/'+p_id+'/edit/')

编辑 - 已解决:

这是我为避免无限循环以及发生的任何其他问题而提出的修复方法:

@login_required
def edit_paragraph(request, p_id):
    p = get_object_or_404(Paragraph, id=p_id)
    form = ParagraphForm({ 'title': p.title, 'description': p.description })

    if request.method == 'POST':
        form = ParagraphForm(request.POST)
        if form.is_valid():
            p.title = request.POST['title']
            p.description = request.POST['description']
            p.save()
            return HttpResponseRedirect('/login/home')

    return render_to_response('auth/edit-paragraph.html', { 'form': form }, context_instance=RequestContext(request))

最佳答案

一些事情:

  1. 如果您被重定向,请确保您确实被重定向,这不仅仅是因为您的浏览器的 URL 自动完成功能太有用了。
    如果发生重定向,您的网络开发人员工具(firebug、网络检查器等)将记录重定向。
    许多浏览器在尾部 / 周围有奇怪的行为。您可以通过在浏览器之前未访问过的端口(即 manage.py runserver 8001)上运行您的开发服务器来检查这一点,然后重试。

  2. 如果您在 urls.py(或您的 View 函数)中或之前的某处遇到异常,可能实际上并未评估 url 模式,因此 django 认为您有a 404。在你的例子中,这似乎没有发生,因为 django 告诉你它正在检查哪些 URL。但是,您可以尝试在控制台中导入 settings.pyurls.pyviews.py 文件,然后尝试运行您的 View 直接运行并确保你没有得到异常。

关于python - django 中的 URL 模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7195340/

相关文章:

c# - 如何将我的代码从 C# 转换为 Python?

Django:/logout 切换语言

Python 3 电子邮件正文编码

python - setuptools "eager_resources"到可执行目录

Django:获取最新的不同对象

django - 使用 Django 的本地开发服务器时,本地站点未在 VirtualBox 中显示?

python - django - 可选 url 参数的正则表达式

python - 探戈与 Django : URLconf defined in tango_with_django_project. 网址。找不到网页

python - 具有 "gcloud functions deploy"的 GCP 云函数失败,--source 作为文件而不是目录