帮助 django 基于日期的通用视图

发布于 2024-11-01 06:13:27 字数 3095 浏览 0 评论 0原文

我第一次使用基于日期的通用视图。我已经使存档页面正常工作,但由于某种原因,当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-11-08 06:13:27

刚刚意识到我必须定义一个视图并传递年、月、日和 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'),
     )

Just realised I had to define a view and pass the year, month, day and slug, and rewrite the url.

view

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'),
     )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文