问题描述
我正在使用django 1.6文档教程1 - 6.学习django
本轮是我的第四次尝试,以前的3次尝试是成功的,我每次尝试了解更多。
我现在在教程3中,以创建视图。
根据文档,在创建视图后,我需要将其映射到一个URL。
所以我按照文档在poll目录中添加一个urls.py。
然后我按照文档添加include()到mysite / urls.py
i可以这样称为有意义的视图中的索引视图。
现在如果我回到本地主机:8000,我得到一个错误页面,
所以我的问题是
1)为什么?
2)如何收回我的本地主机:8080索引页面与民意调查索引页面共存?
非常感谢大家的时间。对于这么简单的问题,我是新的,很抱歉。
谢谢。
更新:
这是我的urls.py
<$ p $来自django.conf.urls导入模式的p>
包含来自django.contrib的url
import admin
admin.autodiscover()
urlpatterns = patterns('',
#示例:
#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
从django.conf.urls导入模式,url
从投票导入视图
urlpatterns = patterns('',
url(r'^ $',views.index,name ='index')
)
错误msg是:
code>使用mysite.urls中定义的URLconf,Django按照以下顺序尝试了这些URL模式:
^ admin /
^ polls /
T他当前的网址,没有匹配任何这些。
http:// localhost :创建项目(mysite)时将显示8000
索引页面。创建应用程序并将其包含在settings.py中的已安装应用程序中后,您将不再查看项目的索引页面,而应用程序的管理页面(民意调查)将在 http:/ / localhost:8000 / admin
如果您在polls / urls.py
$中创建了以下模式的任何视图b$ b
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):
返回render_to_response('polls / index.html',context_instance = RequestContext(request))
urls.py
urlpatterns = patterns('',
url(r'^ index / $'你的项目/ urls.py中的$ bal
urlpatterns = patterns('',
url(r'^ polls /',include('polls.urls ')),
url(r'^ admin /',include(admin.site.urls)),
)
现在,您可以在 http:// localhost:8000 / polls / index
中查看您的模板index.html
i am learning django by using django 1.6 documents tutorial 1 - 6. this round is my 4th try and, previous 3 try was successful and i understand more on every try.
i am in tutorial 3 now, to create views.
according to the documents, after i created a view, i need to map it to a URL. so i follow the documents to add a urls.py in the polls directory. and then i follow the document to add include() to mysite/urls.py i am able to so called wired an index view into the polls view.
now if i go back to localhost:8000, i get an error page, so my question is 1) WHY? 2) how to get back my localhost:8080 index page co-exist with the polls index page?
thank you very much everyone for your time. i am new and sorry for so simple question.thank you.
updated:this is my 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')),
)
this is my polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
the error msg is:
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.
解决方案 http://localhost:8000
index page will be displayed when you created a project (mysite). After creating an application and including it in installed apps in settings.py, you will no longer view the index page of the project, rather your admin page of the application (polls) will be visible at http://localhost:8000/admin
And if you had created any views with the following patterns in polls/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^blog/$', views.blog, name='blog'),
)
You need to visit http://localhost:8000/polls
for your index page and http://localhost:8000/polls/blogs
for your poll blogs page
Editing my answer for step by step approach:
create index.html at polls/templates/polls directory.
in polls/views.py
from django.shortcuts import render_to_response
def pollindex(request):
return render_to_response('polls/index.html', context_instance=RequestContext(request))
in polls/urls.py
urlpatterns = patterns('',
url(r'^index/$', views.pollsindex, name='index'),
)
in your project/urls.py
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And now you can view your template index.html at http://localhost:8000/polls/index
这篇关于django tutorial 3 indexpage丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!