
[英]Page not found at /about/. The current path, about/, did not match any of these. Why can't django follow my URL path?
[英]About django url why will be overwrite?
这是我的文件树
____mysite
|____db.sqlite3
|____dealwith_Time
| |______init__.py
| |______init__.pyc
| |____admin.py
| |____admin.pyc
| |____migrations
| | |______init__.py
| | |______init__.pyc
| |____models.py
| |____models.pyc
| |____tests.py
| |____urls.py
| |____urls.pyc
| |____views.py
| |____views.pyc
|____manage.py
|____mysite
| |______init__.py
| |______init__.pyc
| |____settings.py
| |____settings.pyc
| |____urls.py
| |____urls.pyc
| |____wsgi.py
| |____wsgi.pyc
关于根urls.py文件是关于
url(r'^dealwith_Time/$',include('dealwith_Time.urls')),
url(r'^dealwith_Time/12$',include('dealwith_Time.urls')),
并处理_Time的网址是
url(r'^$', 'dealwith_Time.views.current_datetime'),
url(r'^/12$', 'dealwith_Time.views.hour_ahead'),
并展示我对_Time的看法
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body> It is now %s.</body></html>" %now
return HttpResponse(html)
def hour_ahead(request):
return HttpResponse("victory")
问题是当我访问localhost:8000/dealwith_time
dealwith_time时它起作用并响应时间。但是当我访问localhost:8000/dealwith_time/12
它仍在响应时间!并使用视图的current_time函数而不是使用hour_ahead
函数并打印"victory"
...为什么我如此困惑,请帮助我..
你必须改变
url(r'^dealwith_Time/$',include('dealwith_Time.urls')),
对于
url(r'^dealwith_Time/',include('dealwith_Time.urls')),
$符号将覆盖dealwith_Time/12
,并将覆盖前斜杠符号之后的所有内容。
看一下正则表达式 。
您在root
的urls.py
的url末尾有$
符号。 这意味着url必须完全匹配(不仅仅是开头)。 因此,在您的示例中,URL与root
的urls.py
第二个条目匹配,并且空字符串被传递给with_Time
的urls.py
因此它将与第一个条目和显示时间匹配。
如果要包含其他url文件,通常希望不使用$
来使用regex,那么它将与url的开头匹配,其余的将传递给包含的urls文件。
要更正您的示例,请将其用于root
的urls.py
:
url(r'^dealwith_Time/',include('dealwith_Time.urls')),
请注意,我已经删除了$
,所以/dealwith_time/12
和/dealwith_time/
都将匹配,第一种情况下的12
将被传递到下一个级别。
并将其用于with_Time
的urls.py
:
url(r'^$', 'dealwith_Time.views.current_datetime'),
url(r'^12$', 'dealwith_Time.views.hour_ahead'),
请注意,我已经删除了/
在第二行,因为它将被删除在root的urls.py
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.