Thursday, July 25, 2013

How to get the right url of models in generic views?


#urls.py
from .models import Entry
from django.conf.urls import patterns, url
from django.views.generic.dates import (ArchiveIndexView,
                                        YearArchiveView,
                                        MonthArchiveView,
                                        DayArchiveView,
                                        DateDetailView,
                                        )

urlpatterns = patterns('',
    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
        DateDetailView.as_view(model=Entry,
            date_field='pub_date'
            ),
        ),

    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$',
        DayArchiveView.as_view(model=Entry,
            date_field='pub_date',
            template_name='blog/entry_archive.html',
            ),
        ),

    url(r'^(?P<year>\d{4})/(?P<month>\w{3})/$',
        MonthArchiveView.as_view(model=Entry,
            queryset=Entry.objects.order_by('-pub_date'),
            context_object_name='latest',
            date_field='pub_date',
            template_name='blog/entry_archive.html',
            ),
        ),

    url(r'^(?P<year>\d{4})/$',
        YearArchiveView.as_view(model=Entry,
            queryset=Entry.objects.order_by('-pub_date'),
            context_object_name='latest',
            date_field='pub_date',
            template_name='blog/entry_archive.html',
            make_object_list=True,
            ),
        ),

    url(r'^$',
        ArchiveIndexView.as_view(model=Entry,
            date_field='pub_date',
            ),
        ),
)

I'm having a problem getting the right urls of model instances in generic class based views in Django 1.5.

I have the following code:


#models.py
import datetime

from django.contrib.auth.models import User
from django.db import models

from taggit.managers import TaggableManager
from markdown import markdown

class Category(models.Model):
    title = models.CharField(max_length=250, help_text="Maximum 250 characters.")
    slug = models.SlugField(unique=True,
        help_text="Sugested value will be generated from title. Must be unique")
    description = models.TextField()


    class Meta:
        ordering = ['-title']
        verbose_name_plural = 'Categories'

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return 'categories/%s/' % self.slug


class Entry(models.Model):

    LIVE_STATUS = 1
    DRAFT_STATUS = 2
    HIDDEN_STATUS = 3
    STATUS_CHOICES = (
            (LIVE_STATUS, 'Live'),
            (DRAFT_STATUS, 'Draft'),
            (HIDDEN_STATUS, 'Hidden'),
    )

    status = models.IntegerField(choices=STATUS_CHOICES, default=LIVE_STATUS)
    title = models.CharField(max_length=250)
    slug = models.SlugField(unique_for_date='pub_date')
    excerpt = models.TextField(blank=True)
    excerpt_html = models.TextField(editable=False, blank=True)
    body = models.TextField()
    body_html = models.TextField(editable=False, blank=True)
    pub_date = models.DateTimeField(default=datetime.datetime.now())
    author = models.ForeignKey(User)
    enable_comments = models.BooleanField(default=True)
    categories = models.ManyToManyField(Category)
    tags = TaggableManager()

    class Meta:
        verbose_name_plural = 'Entries'
        ordering = ['-pub_date']

    def __unicode__(self):
        return self.title

    def save(self, force_insert=False, force_update=False):
        self.body_html = markdown(self.body)
        if self.excerpt:
            self.excerpt_html = markdown(self.excerpt)
        super(Entry, self).save(force_insert, force_update)

    @models.permalink
    def get_absolute_url(self):
        return ('entry', (),{
            'year': self.pub_date.strftime('%Y'),
            'month': self.pub_date.strftime('%b'),
            'day': self.pub_date.strftime('%d'),
            'slug': self.slug,
            }
        )

When I manually type an url in the browser the archives or entries are shown. 
So when giving in "localhost://news/2013/jul/24/entry-name" the entry is shown. 
And when giving in "localhost://news/2013/" a list is presented. 
So the urls are valid but in the list-views (archives) the links that are printed on the screen are not linked to the the right url. 
They are linked to the url of the present page.
Can someone help me ?

Many thanks in advance!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment