如何在Django中设置索引页面,如 http://127.0.0.1:8000/

0 投票
1 回答
3971 浏览
提问于 2025-04-18 02:20

我创建了一个小项目,结构大致是这样的..

|-mysite
|-polls
|---static
|-----polls
|-------css
|---------images
|-------images
|-------js
|---templates
|-----polls
|-templates
|---admin
在这个项目中,"polls"是我的应用,现在我可以通过这个网址访问它 http://127.0.0.1:8000/polls/
在我的主文件夹,也就是"mysite"文件夹里的urls.py文件中,我写了这样的代码..

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

现在在"polls"文件夹里的urls.py文件中的代码是..

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

现在我想让页面像主网站地址那样显示,比如.. http://127.0.0.1:8000/
我该怎么做呢?
谢谢。

1 个回答

1

你能把这里的内容改成这样吗

url(r'^polls/',include('polls.urls',namespace="polls")),   

变成

url(r'^', include('polls.urls', namespace="polls")),

原因是这样的:Django的URL使用正则表达式。在你的例子中,你是在告诉Django,从这个网址开始捕捉你的投票应用,也就是localhost:8000/polls/。

想了解更多,可以查看这个链接: https://docs.djangoproject.com/en/dev/topics/http/urls/

即使在你项目的根目录下的urls.py文件里,你也有这个:

# Examples:
    # url(r'^$', 'Toolkit.views.home', name='home'), #this will match url of the type localhost:8000
    # url(r'^blog/', include('blog.urls')), # this will match url of the type localhost:8000/blog/

注意看哦!

撰写回答