工作,我有一個看起來像這樣在我的urls.py網址:Django的URL不是括號
url(r'^ex/(?P<example>[\w ]+)$', views.example, name = 'example')
我的一個變量是這樣的:/ex/(something)/
。我找到了一個沒有找到URL的404頁面,我知道它與括號有關。我如何解決這個問題而不改變變量?
工作,我有一個看起來像這樣在我的urls.py網址:Django的URL不是括號
url(r'^ex/(?P<example>[\w ]+)$', views.example, name = 'example')
我的一個變量是這樣的:/ex/(something)/
。我找到了一個沒有找到URL的404頁面,我知道它與括號有關。我如何解決這個問題而不改變變量?
添加(
和)
的字符類爲您example
組。
url(r'^ex/(?P<example>[\w()]+)$', views.example, name='example'),
你必須檢查你的觀點
def example(request, example):
# your logic view
return render(request, 'example.html', {'example': example})
如果使用get_object_or404
和型號不存在的Django返回404碼
和URL應該是這樣的:
url(r'^ex/(?P<example>[\w]+)$', views.example, name = 'example')
或
url(r'^ex/(?P<example>[a-zA-Z]+)$', views.example, name = 'example')
在url中,Django使用正則表達式來創建你的url。另外,如果你使用圓括號,你必須逃避它。
謝謝!這也工作=) – ALUW