关于django url 为什么会被覆盖?
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')),
和交易 with_Time 的网址是
url(r'^$', 'dealwith_Time.views.current_datetime'),
url(r'^/12$', 'dealwith_Time.views.hour_ahead'),
并显示我的交易 with_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
它 worked.and 响应时间。但是当我访问 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
,因此它将匹配第一个条目和显示时间。
如果您要包含其他 urls 文件,您通常希望在没有 $
的情况下使用正则表达式,因此它将匹配 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
中被删除。
这是我的文件树
____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')),
和交易 with_Time 的网址是
url(r'^$', 'dealwith_Time.views.current_datetime'),
url(r'^/12$', 'dealwith_Time.views.hour_ahead'),
并显示我的交易 with_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
它 worked.and 响应时间。但是当我访问 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
,因此它将匹配第一个条目和显示时间。
如果您要包含其他 urls 文件,您通常希望在没有 $
的情况下使用正则表达式,因此它将匹配 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
中被删除。