Python “Django教程”;写一些实际有用的观点;

Python “Django教程”;写一些实际有用的观点;,python,regex,django,Python,Regex,Django,我一直在做Django教程。我所说的部分是“编写实际做某事的视图”(第3部分) 我正在尝试使用它提供给您的index.html模板,但是我一直收到一个404错误,上面说 Request Method: GET Request URL: http://127.0.0.1:8000/polls/index.html Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^p

我一直在做Django教程。我所说的部分是“编写实际做某事的视图”(第3部分)

我正在尝试使用它提供给您的index.html模板,但是我一直收到一个404错误,上面说

Request Method: GET
Request URL: http://127.0.0.1:8000/polls/index.html

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<question_id>\d+)/$ [name='detail']
^polls/ ^(?P<question_id>\d+)/results/$ [name='results']
^polls/ ^(?P<question_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/index.html, didn't match any of these.
请求方法:获取
请求URL:http://127.0.0.1:8000/polls/index.html
Django使用mysite.URL中定义的URLconf,按以下顺序尝试了这些URL模式:
^投票/^$[name='index']
^轮询/^(?P\d+)/$[name='detail']
^轮询/^(?P\d+)/results/$[name='results']
^投票/^(?P\d+)/vote/$[name='vote']
^管理员/
当前URL polls/index.html与其中任何一个都不匹配。
我不知道其中一个正则表达式是不是错了?我已经和它混在一起有一段时间了,我没有运气让它工作

我可以去投票。但是/polls/index.html不起作用

任何帮助都将不胜感激


我使用的Django版本是1.7.4,Django视图函数或类使用您定义的模板,因此您不必在URL中指定它。
urls.py
文件与您定义的正则表达式相匹配,以向视图发送请求


如果您确实想使用该URL,则必须在
URL.py
中定义
^polls/index.html$
,并将其指向您的视图。

Django视图函数或类使用您定义的模板,这样您就不必在URL中指定它。
urls.py
文件与您定义的正则表达式相匹配,以向视图发送请求


如果您真的想使用该URL,您必须在
URL.py
中定义
^polls/index.html$
,并将其指向您的视图。

根据您的要求,听起来您基本上想要在
URL.py
中的
urlpatterns
中定义的URL上输出一个静态html文件

我强烈建议您查看基于类的视图。

从现有内容到呈现
polls/index.html
的最快方法如下:

# some_app/urls.py
from django.conf.urls import patterns
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^polls/index.html', TemplateView.as_view(template_name="index.html")),
)
但我相信您会希望将内容传递给模板,这样基于类的视图将是您所需要的。因此,在增加上下文的情况下,上述方法的替代方法是:

# some_app/views.py
from django.views.generic import TemplateView

class Index(TemplateView):
    template_name = "index.html"

    def get_context_data(self, **kwargs):
        context = super(Index, self).get_context_data(**kwargs)
        context['foo'] = 'bar'
        return context
显然,将
{{foo}}
添加到
index.html
会向用户输出bar。您需要将
url.py
更新为

# some_app/urls.py
from django.conf.urls import patterns
from .views import Index

urlpatterns = patterns(
    '',
    (r'^polls/index.html', Index.as_view()),
)

根据您的要求,听起来您基本上希望在
urlpatterns
中的
urls.py
中定义的URL上输出一个静态html文件

我强烈建议您查看基于类的视图。

从现有内容到呈现
polls/index.html
的最快方法如下:

# some_app/urls.py
from django.conf.urls import patterns
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^polls/index.html', TemplateView.as_view(template_name="index.html")),
)
但我相信您会希望将内容传递给模板,这样基于类的视图将是您所需要的。因此,在增加上下文的情况下,上述方法的替代方法是:

# some_app/views.py
from django.views.generic import TemplateView

class Index(TemplateView):
    template_name = "index.html"

    def get_context_data(self, **kwargs):
        context = super(Index, self).get_context_data(**kwargs)
        context['foo'] = 'bar'
        return context
显然,将
{{foo}}
添加到
index.html
会向用户输出bar。您需要将
url.py
更新为

# some_app/urls.py
from django.conf.urls import patterns
from .views import Index

urlpatterns = patterns(
    '',
    (r'^polls/index.html', Index.as_view()),
)

我是否将其添加到/mysite/polls/url.py文件或/mysite/mysite/url.py文件中?如果您已经将任何匹配的
polls
发送到您的
polls/url.py
,那么您需要在
mysite/polls/url.py
教程中定义
mysite
的问题,如果我记得的话,这就是
mysite
是项目和应用程序。您可以在
mysite
dir中设置
settings.py
wsgi.py
,然后执行
python manage.py startapp myapp
,然后在其中创建
models.py
views.py
url.py
。将
myapp
添加到
INSTALLED\u APPS
中,然后将
/polls
URL放入
myapp
中,我是否将其添加到/mysite/polls/urls.py文件或/mysite/mysite/urls.py中?如果您已经将任何匹配的
polls
URL发送到
polls/urls.py中,然后你需要在你的
mysite/polls/url.py中定义
^index.html$
,如果我记得的话,教程中的
mysite
的问题是
mysite
是项目和应用程序。您可以在
mysite
dir中设置
settings.py
wsgi.py
,然后执行
python manage.py startapp myapp
,然后在其中创建
models.py
views.py
url.py
。将
myapp
添加到
INSTALLED\u APPS
中,然后将您的
/polls/
URL放入
myapp