【问题标题】:Django urls recognition errorDjango urls识别错误
【发布时间】:2012-09-28 19:08:05
【问题描述】:

我有项目myproject 和下面列出的应用程序myapp。当我尝试使用 url /add 时出现此错误:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/add/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^$
The current URL, add/, didn't match any of these.

设置配置:

ROOT_URLCONF = 'myproject.urls'

我的项目/urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', include('myapp.urls')),
)

myapp/urls.py

from django.conf.urls import patterns, include, url

urlpatterns = patterns('myapp.views',
    url(r'^$', 'main'),
    url(r'^add/', 'foo'),
)

myapp/views.py

from django.shortcuts import render

def main(request):
    return render(request, "main.html", {'test': 1})

def foo(request):
    return render(request, "add.html")

模板/main.html

{{ test }}

模板/add.html

{{ test }}

怎么了?

【问题讨论】:

    标签: django django-urls


    【解决方案1】:

    这一行:

    url(r'^$', include('myapp.urls')),
    

    表示只有 完全为空的 URL 将被传递给 myapp 包含。你的意思是:

    url(r'^', include('myapp.urls')),
    

    【讨论】:

    • 或者,r'^add/,然后相应地调整myapp/urls.py
    猜你喜欢
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2018-08-17
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多