Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django and get_absolute_url

I have simple "post" model which represents an entry for my blog:

class Post(models.Model):
    title = models.CharField('title', max_length=200)
    slug = models.SlugField('slug', unique_for_date='creation_time')
    creation_time = models.DateTimeField('creation time', auto_now_add=True)
    content = models.TextField('content')

    @permalink
    def get_absolute_url(self):
        return ('devblog_post_url', (), {
            'year': self.creation_time.year,
            'month': self.creation_time.month,
            'day': self.creation_time.day,
            'slug': self.slug})            

At the index of my blog, I want to paginate these Posts with this view:

def index_view(request):
    published_posts = Post.objects.all()    
    paginator = Paginator(published_posts, 10)
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        post_list = paginator.page(page)
    except (EmptyPage, InvalidPage):
        post_list = paginator.page(paginator.num_pages)

    return render_to_response('devblog_index.html', {"post_list": post_list})

And now here is the problem, I get blank urls once I call the get_absolute_url method for a post with devblog_index.html template:

{% for post in post_list.object_list %}
    <a href="/{{ post.get_absolute_url }}/">{{ post.title }}</a><br />
{% endfor %}

EDIT:

Here is my urls.py for my application:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    url(r'^$',
        view='devblog.views.index_view',
        name='devblog_index'
    ),
    url(r'^(?P<year>\d{4})/(?P<month>\w{1,2)/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
        view='devblog.views.post_view', name='devblog_post_url'
    )
)

at the main urls.py, I simple include it as (r'^blog/', include('devblog.urls'))

Where can be the problem for this blank string url ?

Regards

like image 346
Hellnar Avatar asked Jan 22 '11 11:01

Hellnar


People also ask

What does Get_absolute_url do in Django?

Define a get_absolute_url() method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.

How can I get full url in Django?

Use handy request. build_absolute_uri() method on request, pass it the relative url and it'll give you full one. By default, the absolute URL for request. get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.

What is __ str __ in Django?

The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.

What is absolute url in Django?

Django absolute provide 2 template tags: absolute: acts like url but provide a full URL based on incoming request.


1 Answers

Change (?P<month>\w{1,2) to (?P<month>\d{1,2}).

like image 51
sunn0 Avatar answered Sep 25 '22 15:09

sunn0