有关django同用视图的问题?

2012-09-27 19:39:18 +08:00
 budong
最近看到django通用视图部分(http://djangobook.py3k.cn/2.0/chapter11/),做到这一步;

说明我的project名--generic,book-app是generic/generic/book

#model.py,之前例子中Publisher的定义(generic/generic/book/model.py
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()

def __unicode__(self):
return self.name

#urls.py 在generic/generic/urls.py
from django.conf.urls.defaults import *
from django.views.generic import list_detail
from mysite.books.models import Publisher
publisher_info = {
'queryset': Publisher.objects.all(),
'template_name': 'publisher_list_page.html',
}
urlpatterns = patterns('',
url(r'^publishers/$', list_detail.object_list, publisher_info),
)


模板在generic/templates下,generic/templatespublisher_list_page.html
publisher_list_page.html
<h2>Publishers</h2>
<ul>
{% for publisher in object_list %}
<li>{{ publisher.name }}</li>
{% endfor %}
</ul>

浏览器请求ocalhost:8000/publishers,但是页面一直显示:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/publishers
Using the URLconf defined in generic.urls, Django tried these URL patterns, in this order:
1.^about/$
2.^publishers/$
The current URL, publishers, 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.
3706 次点击
所在节点    问与答
6 条回复
ivanlw
2012-09-28 10:18:15 +08:00
什么版本的django?我看了我的url.py里面下urlpatterns元祖的元素都是(),(),().....而不是url(),url(),url().......

debug页面都告诉你连这个url都识别不出来了……
budong
2012-09-28 16:23:44 +08:00
@ivanlw
1.4版本的,我也很郁闷为什么不能识别这个url。。。照着教程来的,不知到那个地方错了。
budong
2012-09-29 09:14:32 +08:00
问题已解决:访问时像这样 http://localhost:8000/publishers/在后面加一个/就行了。
但是为什么会是这样,我还在思考中,如果你知道请告诉我。
kojp
2012-09-29 09:23:42 +08:00
上面没看完。
但只看这条的话。
^publishers/$
你尝试改成
^publishers$

应该不能达成你第一个要求了。有时间的话,建议把正则的相关的几本书给他啃掉。
不一定要会写得多牛逼,但至少要知道各处修饰符的意义。
shen0956
2012-09-29 10:42:02 +08:00
^publishers/$
建议改成
^publishers/?$
就是有无/都可以访问
你要多了解下正则表达式啊....
Danfi
2012-09-29 16:17:39 +08:00
可能和这个有关吧
https://docs.djangoproject.com/en/dev/ref/settings/#append-slash
CommonMiddleware 没启用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/48870

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX