帮助 django 基于日期的通用视图
我第一次使用基于日期的通用视图。我已经使存档页面正常工作,但由于某种原因,当我使用 get_absolute_url 方法创建指向详细信息页面的链接时,存档页面只是重新加载。 任何见解都会有帮助,谢谢。
这是我的 url.py
blog_info_dict = {
'queryset': News.objects.all(), \
'date_field': 'date_created'
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^news/$', 'archive_index', alumni_info_dict, 'blog_news_archive_index'),
(r'^news/(?P<year>\d{4})/$', 'archive_year', \
blog_info_dict, 'blog_news_archive_year'),
(r'^news/(?P<year>\d{4})/(?P<month>\w{3})/$', 'archive_month', \
blog_info_dict, 'blog_news_archive_month'),
(r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', \
'archive_day', blog_info_dict, 'blog_news_archive_day'),
(r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-w]+)/$'\
, 'object_detail', blog_info_dict,'blog_news_detail'),
)
models.py
class News(models.Model):
title = models.CharField('News Title', max_length=60)
slug = models.SlugField(editable=False)
image = models.ImageField('Image', null=True, blank=True, upload_to='uploaded_images/')
comments = models.TextField('News Content', null=True, blank=True)
date_created = models.DateTimeField('Date Posted', auto_now=True)
created_by = models.ForeignKey(User)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(News, self).save(*args, **kwargs)
@models.permalink
def get_absolute_url(self):
return('blog_news_detail', (), { \
'year': self.date_created.strftime("%Y"),
'month': self.date_created.strftime("%b").lower(),
'day': self.date_created.strftime("%d"),
'slug': self.slug })
news_archive.html 模板
{% extends "index.html" %}
{% block content %}
{% for news in latest %}
<p><a href="{{ news.get_absolute_url }}">{{ news.title }}</a> </p>
<p>{{ news.comments }}</p>
{% endfor %}
{% endblock %}
news_detail.html
{% extends "news_archive.html" %}
{% block content %}
<p>{{ object.title }}</p>
{% endblock %}
新闻对象视图
def news_view(request):
if request.method == 'POST':
form = NewsForm(request.POST, request.FILES)
if form.is_valid():
news_instance = News( \
title = form.cleaned_data['title'],
image = form.cleaned_data['image'],
comments = form.cleaned_data['comments'],
created_by = request.user)
news_instance.save()
else:
form = NewsForm()
variables = RequestContext(request, {'form': form})
return render_to_response('news_page.html', variables)
新闻模型表单
class NewsForm(forms.ModelForm):
class Meta:
model = News
fields = ('title', 'image', 'comments',)
I am using date based generic views for the first time. I have gotten the archive page to work, but for some reason when I create a link to the detail page using get_absolute_url method, the archive page just reloads.
Any insight will be helpful, thanks.
here's my url.py
blog_info_dict = {
'queryset': News.objects.all(), \
'date_field': 'date_created'
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^news/
models.py
class News(models.Model):
title = models.CharField('News Title', max_length=60)
slug = models.SlugField(editable=False)
image = models.ImageField('Image', null=True, blank=True, upload_to='uploaded_images/')
comments = models.TextField('News Content', null=True, blank=True)
date_created = models.DateTimeField('Date Posted', auto_now=True)
created_by = models.ForeignKey(User)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(News, self).save(*args, **kwargs)
@models.permalink
def get_absolute_url(self):
return('blog_news_detail', (), { \
'year': self.date_created.strftime("%Y"),
'month': self.date_created.strftime("%b").lower(),
'day': self.date_created.strftime("%d"),
'slug': self.slug })
news_archive.html template
{% extends "index.html" %}
{% block content %}
{% for news in latest %}
<p><a href="{{ news.get_absolute_url }}">{{ news.title }}</a> </p>
<p>{{ news.comments }}</p>
{% endfor %}
{% endblock %}
news_detail.html
{% extends "news_archive.html" %}
{% block content %}
<p>{{ object.title }}</p>
{% endblock %}
News object view
def news_view(request):
if request.method == 'POST':
form = NewsForm(request.POST, request.FILES)
if form.is_valid():
news_instance = News( \
title = form.cleaned_data['title'],
image = form.cleaned_data['image'],
comments = form.cleaned_data['comments'],
created_by = request.user)
news_instance.save()
else:
form = NewsForm()
variables = RequestContext(request, {'form': form})
return render_to_response('news_page.html', variables)
news modelform
class NewsForm(forms.ModelForm):
class Meta:
model = News
fields = ('title', 'image', 'comments',)
, 'archive_index', alumni_info_dict, 'blog_news_archive_index'),
(r'^news/(?P<year>\d{4})/
models.py
news_archive.html template
news_detail.html
News object view
news modelform
, 'archive_year', \
blog_info_dict, 'blog_news_archive_year'),
(r'^news/(?P<year>\d{4})/(?P<month>\w{3})/
models.py
news_archive.html template
news_detail.html
News object view
news modelform
, 'archive_month', \
blog_info_dict, 'blog_news_archive_month'),
(r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/
models.py
news_archive.html template
news_detail.html
News object view
news modelform
, \
'archive_day', blog_info_dict, 'blog_news_archive_day'),
(r'^news/(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-w]+)/
models.py
news_archive.html template
news_detail.html
News object view
news modelform
\
, 'object_detail', blog_info_dict,'blog_news_detail'),
)
models.py
news_archive.html template
news_detail.html
News object view
news modelform
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚意识到我必须定义一个视图并传递年、月、日和 slug,并重写 url。
查看
url.py
Just realised I had to define a view and pass the year, month, day and slug, and rewrite the url.
view
url.py