Django urls.py正则表达式

时间:2014-04-28 18:40:38

标签: python regex django

我完成了Django初学者教程,我正在尝试完成[The Django Book:Chapter 7 Forms] [1]。我在使用文档中的方法将URLSconf链接到search_forms.html页面时遇到问题。很可能是因为我使用单独的'main'和'app'urls.py文件

任何人都可以帮助我使用正确的URLconf格式吗?我想访问http://127.0.0.1:8000/polls/search-form/

时看到search_forms.html页面

我有一个位于/src/project/urls.py

的主urls.py文件
from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),   
)

我还有一个位于/src/polls/urls.py的民意调查urls.py您可以看到我尝试了一些解决方案。

urlpatterns = patterns('',
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),


    #this one will bring me to the vote page when i type search-form in the browser
    #http://127.0.0.1:8000/polls/2/search-form/
    #url(r'^(?P<poll_id>\d+)/search-form/$', views.vote, name='search-form'),


    #url(r'^/search-form/$', views.search_form, name='search_form'),

    url(r'^search-form/$', views.search_form),

    #url(r'^(?P<poll_id>\d+)/search-form/$', views.search_form, name='search-form'),

    #url(r'^search-form/$', views.search_form),
    #url(r'^$', views.search_form, name='search-form/$'),  
   # url(r'^(?P<poll_id>\d+)/detail_dropdown/$', views.vote, name='detail_dropdown'),
)

我在/src/polls/templates/polls/search_form.html上有一个search_form.html页面,其中包含与教程相同的代码

我还在src / polls / views.py设置了search_form视图,代码如下

def search_form(request):
    return render(request, 'search_form.html')

非常感谢任何帮助。感谢。

编辑:浏览器错误消息

谢谢@Ailistar

我已经使用未注释的url尝试了它,这是错误消息。

TemplateDoesNotExist at /polls/search-form/

search_form.html

Request Method:     GET
Request URL:    http://127.0.0.1:8000/polls/search-form/
Django Version:     1.6.2
Exception Type:     TemplateDoesNotExist
Exception Value:    

search_form.html

Exception Location:     /Library/Python/2.7/site-packages/django/template/loader.py in find_template, line 131
Python Executable:  /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version:     2.7.6
Python Path:    

['/Users/brendan/Dropbox/workspace/bias_experiment/src',
 '/Library/Python/2.7/site-packages/distribute-0.7.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/setuptools-2.2-py2.7.egg',
 '/Library/Python/2.7/site-packages/pip-1.5.4-py2.7.egg',
 '/Library/Python/2.7/site-packages/yolk-0.4.3-py2.7.egg',
 '/Library/Python/2.7/site-packages/virtualenv-1.11.4-py2.7.egg',
 '/Library/Python/2.7/site-packages/virtualenvwrapper-4.2-py2.7.egg',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
 '/Library/Python/2.7/site-packages']

Server time:    Mon, 28 Apr 2014 20:36:43 +0100

  [1]: http://www.djangobook.com/en/2.0/chapter07.html

1 个答案:

答案 0 :(得分:1)

您问题中未注释的网址应该有效。命名它是一种很好的做法。

url(r'^search-form/$', views.search_form, name="search_form"),

错误消息说明了问题所在。

TemplateDoesNotExist at /polls/search-form/

search_form.html

您已将模板放入“polls / search_form.html”,但在您的视图中,您使用的是“search_form.html”模板。

要解决此问题,您可以将视图更改为

return render(request, 'polls/search_form.html')

或者您可以将模板移动到父目录/src/polls/templates/

如果上面的网址格式不起作用,请更新您的问题,说明在浏览器中转到/polls/search-form/时出现的错误。