如何使用 django 基于日期的通用视图
我一直在尝试 django 基于日期的通用视图,但没有成功。文档没有给出示例,所以我还没有很好地理解它们。这个教程展示了如何为具有<的模型使用通用视图代码>slug字段。
该文档说
Either object_id or (slug and slug_field) is required.
但我还不明白如何实际做到这一点。我的 models.py 包含
class CompanyActivity(models.Model):
company = models.CharField(max_length=300)
desc = models.TextField()
text = models.TextField()
date = models.DateTimeField()
activity_type = models.CharField(max_length=600)
并且我的 urls.py 包含
info_dict = {
'queryset': CompanyActivity.objects.all(),
'date_field': 'date',
}
urlpatterns+= patterns('django.views.generic.date_based',
url(r'^activity/(?P<year>d{4})/(?P<object_id>\d+)/$','object_detail', dict(info_dict,object_id='id',template_name='activity/detail.html')),
)
我知道 urls.py 中有问题我不知道如何传递 object_id,我打算做的是我想要显示特定年份的活动页面。同一个公司同一年可以有多个活动页面。
当我查找此页面时,
http://127.0.0.1:8000/activity/2011/1
它说找不到。请帮忙
I have been trying to django date based generic views
but have not been successful. The documentation does not give an example, so I have not understood them well. This tutorial here shows how to use generic views for a model that has a slug field
.
The documentation says
Either object_id or (slug and slug_field) is required.
But I have not understood how actually to do that. My models.py contains
class CompanyActivity(models.Model):
company = models.CharField(max_length=300)
desc = models.TextField()
text = models.TextField()
date = models.DateTimeField()
activity_type = models.CharField(max_length=600)
and my urls.py contains
info_dict = {
'queryset': CompanyActivity.objects.all(),
'date_field': 'date',
}
urlpatterns+= patterns('django.views.generic.date_based',
url(r'^activity/(?P<year>d{4})/(?P<object_id>\d+)/
I know there is something wrong in the urls.py
I do not know how to pass the object_id, what I intend to do is that I want to display the activity page for a particular year. And multiple activity pages can be there for the same company for the same year.
When I look for this page
http://127.0.0.1:8000/activity/2011/1
It says not found. Please help
,'object_detail', dict(info_dict,object_id='id',template_name='activity/detail.html')),
)
I know there is something wrong in the urls.py
I do not know how to pass the object_id, what I intend to do is that I want to display the activity page for a particular year. And multiple activity pages can be there for the same company for the same year.
When I look for this page
It says not found. Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于某种原因,您似乎正在查看
object_detail
的信息。顾名思义,这是特定对象的详细信息(基于日期),因此与您的问题(即如何获取特定年份的活动列表)无关。毫不奇怪,您正在寻找的功能是archive_year
:也就是说,您根本不应该真正使用旧的基于函数的视图 - 它们已被基于类的视图所取代,这就是为什么它们只能通过“基于函数的通用视图(已弃用)”链接进行访问。请参阅有关新样式的文档。
For some reason, you seem to be looking at the information for
object_detail
. As the name implies, that's for the detail of a particular object (based on the date), so is not relevant to your problem which is how to get the list of activities for a particular year. Again not surprisingly, the function you're looking for isarchive_year
:That said, you shouldn't really be using the old function-based views at all - they've been replaced by class-based ones, which is why they're only accessible via the link that says "Function-based generic views (Deprecated)". See the documentation on the new style.