免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 18738 | 回复: 12

django 404问题 [复制链接]

论坛徽章:
0
发表于 2013-08-07 15:21 |显示全部楼层
在学习django,按照tutorial一直弄,直到https://docs.djangoproject.com/en/1.1/intro/tutorial03/

就悲剧了,详细错误信息

就是当访问127.0.0.1:8000/polls的时候抛出异常
ViewDoesNotExist at /polls
Could not import polls.urls. View does not exist in module polls.


前前后后检查了很多次,google也未果,只好求助大家了

论坛徽章:
0
发表于 2013-08-07 16:26 |显示全部楼层
贴你的urls.py代码上来。

论坛徽章:
0
发表于 2013-08-07 17:22 |显示全部楼层
本帖最后由 isohybrid 于 2013-08-07 18:08 编辑

polls/urls.py
from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    # ex: /polls/
    url(r'^s', views.index, name='index'),
    # ex: /polls.5/
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
    # ex: /polls.5/results/
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
    # ex: /polls.5/vote/
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
    )


mysite/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'^polls/', 'polls.urls'),
    url(r'^admin/', include(admin.site.urls)),
    )

论坛徽章:
0
发表于 2013-08-08 17:35 |显示全部楼层
本帖最后由 isohybrid 于 2013-08-08 17:50 编辑

HI,开始django用的是开发版1.7后来换成稳定版1.5的,照着重做了一次,还是卡在这额

Write your first view
Let’s write the first view. Open the file polls/views.py and put the following Python code in it:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the poll index."
This is the simplest view possible in Django. To call the view, we need to map it to a URL - and for this we need a URLconf.

To create a URLconf in the polls directory, create a file called urls.py. Your app directory should now look like:

polls/
    __init__.py
    admin.py
    models.py
    tests.py
    urls.py
    views.py
In the polls/urls.py file include the following code:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)
The next step is to point the root URLconf at the polls.urls module. In mysite/urls.py insert an include(), leaving you with:

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

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)
You have now wired an index view into the URLconf. Go to http://localhost:8000/polls/ in your browser, and you should see the text “Hello, world. You’re at the poll index.”, which you defined in the index view.


就这几步,搞不懂哪里出错了

127.0.0.1/admin         可以访问
127.0.0.1/polls            404

Page not found (404)
Request Method:        GET
Request URL:        http://127.0.0.1:8000/polls
Using the URLconf defined in Lili.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

论坛徽章:
0
发表于 2013-08-11 00:09 |显示全部楼层
你的这个url引入不对把:url(r'^polls/', 'polls.urls'),   建议你先按照django的来,然后在自己修改。

论坛徽章:
0
发表于 2013-08-12 22:06 |显示全部楼层
我把官方的tutorial实现了下,放到github上,欢迎参考:https://github.com/the5fire/django_tutorial_demo

论坛徽章:
0
发表于 2013-08-13 16:18 |显示全部楼层
已解决,十分感谢LS的同志

论坛徽章:
0
发表于 2013-11-12 09:36 |显示全部楼层
哎,晕死了,我也是遇到了这个问题,为什么需要include('polls.urls',namespace="polls").更可气的是官方的文档里居然没有提及。走了很长时间弯路。

论坛徽章:
0
发表于 2013-11-12 22:13 |显示全部楼层
回复 8# fengkongxue


    官方文档没有提及,只能说你没看仔细。反正include的用法我就是看官方文档学的

论坛徽章:
0
发表于 2013-11-12 22:17 |显示全部楼层
话说,最后问题是如何解决的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP