【问题标题】:django tutorial 3 indexpage missingdjango 教程 3 索引页丢失
【发布时间】:2014-06-26 16:49:45
【问题描述】:

我正在使用 django 1.6 文档教程 1 - 6 学习 django。 这一轮是我的第 4 次尝试,前 3 次尝试成功,每次尝试我都了解更多。

我现在在教程 3 中,创建视图。

根据文档,创建视图后,我需要将其映射到 URL。 所以我按照文档在 polls 目录中添加了一个 urls.py 。 然后我按照文档将 include() 添加到 mysite/urls.py 我可以将一个索引视图连接到投票视图中。

现在如果我回到 localhost:8000,我会得到一个错误页面, 所以我的问题是 1)为什么? 2) 如何找回我的 localhost:8080 索引页与投票索引页共存?

非常感谢大家的宝贵时间。我是新手,很抱歉这么简单的问题。 谢谢。

更新: 这是我的 urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls')),
)

这是我的投票/urls.py

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)

错误信息是:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , didn't match any of these.

【问题讨论】:

  • 发布您的 URLs.py。没有代码,我们无法为您提供帮助。
  • 您必须发布您的回溯/异常以及相关代码。您的描述不能像实际的错误消息和您的实际代码那样具有描述性。
  • 感谢 rnevius 和 yuji,请查看我更新的 urls.py 粘贴代码和错误消息。谢谢。
  • ps,如果我注释掉 url(r'^polls/', include('polls.urls')),我可以显示 localhost:8000 没有错误,错误发生在我之后添加 url(r'^polls/', include('polls.urls')),

标签: django django-models django-views


【解决方案1】:

http://localhost:8000 索引页面将在您创建项目(mysite)时显示。创建应用程序并将其包含在 settings.py 中已安装的应用程序中后,您将不再查看项目的索引页面,而是在 http://localhost:8000/admin 可见您的应用程序管理页面(民意调查)

如果您在 polls/urls.py 中创建了具有以下模式的任何视图

urlpatterns = patterns('',
       url(r'^$', views.index, name='index'),
       url(r'^blog/$', views.blog, name='blog'),
)

您需要访问http://localhost:8000/polls 访问您的索引页面和http://localhost:8000/polls/blogs 访问您的投票博客页面

逐步编辑我的答案:

在 polls/templates/polls 目录中创建 index.html。

在 polls/views.py 中

from django.shortcuts import render_to_response
def pollindex(request):
     return render_to_response('polls/index.html', context_instance=RequestContext(request))

在 polls/urls.py 中

urlpatterns = patterns('',
      url(r'^index/$', views.pollsindex, name='index'),
)

在你的项目/urls.py中

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

现在您可以在http://localhost:8000/polls/index查看您的模板 index.html

【讨论】:

  • 嗨,Ria,我认为我的概念仍然很旧,但请耐心等待,我希望我的客人访问 localhost:8000/index.html 并通过 localhost:8000/polls/index 访问投票页面。 html,我应该如何配置 urls.py 文件?
  • 我认为您对模板及其视图感到困惑。 index.html 应该是 polls/templates/polls/ 目录下的模板。并且这个模板文件需要在视图中获取,在url中要提到。查看我的编辑以获取更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2013-07-18
  • 2011-04-07
  • 2021-02-28
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多