gpt4 book ai didi

django - 帮助 django 基于日期的通用 View

转载 作者:行者123 更新时间:2023-12-02 02:24:34 25 4
gpt4 key购买 nike

我第一次使用基于日期的通用 View 。我已经使存档页面正常工作,但由于某种原因,当我使用 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 %}

新闻对象 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)

新闻模型

class NewsForm(forms.ModelForm):
class Meta:
model = News
fields = ('title', 'image', 'comments',)

最佳答案

刚刚意识到我必须定义一个 View 并传递年、月、日和 slug,并重写 url。

查看

from django.views.generic import list_detail

def news_detail_view(request, year, month, day, slug):
response = list_detail.object_detail( \
queryset = News.objects.all(), \
slug = slug, slug_field = 'slug')
return response

url.py

from news.views import news_detail_view

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-]+)/$',\
news_detail_view, 'blog_news_detail'),
)

关于django - 帮助 django 基于日期的通用 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5680130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com