Sebastian
Sebastian

Reputation: 573

get_absolute_url doesn't load the page

So I am trying to code a blog and have a special page for every article and I have the blog.html (main page of the blog) that has the "Read more" button.

<a class = "Read" href="{{ post.get_absolute_url }}">Read more...</a>

And I have the another file post.html which is the base template for every article page. The Post model has the slug field and the urls.py is like this:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.front, name='front'),
    url(r'^blog/', views.blog, name='blog'),
    url(r'^contact/', views.contact, name='contact'),
    url(r'^blog/(?P<slug>[^\.]+)', views.page, name='post')
]

And the page view is like this:

def page(request, slug):
    return render_to_response('home/post.html', {
        'post': get_object_or_404(Post, slug=slug)
    })

The problem is when I press read more nothing happens but I look at the terminal window and the server takes it as a request and return 200 which means success but the page doesn't load.

Edit: The Post model:

class Post(models.Model):
    title = models.CharField(max_length=100, default='')
    text = models.TextField(default='')
    slug = models.SlugField(default=uuid.uuid1, unique=True)
    status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='w')
    description = models.TextField(default='', max_length=300)
    creation_date = models.DateTimeField(auto_now_add=True, editable=False)

     def __unicode__(self):
       return self.title

     def edit_text(self, text):
         self.text = text

     class Meta:
          get_latest_by = 'creation_date

Upvotes: 0

Views: 625

Answers (2)

kia
kia

Reputation: 828

That's the problem. Please try to close the url patterns by $

urlpatterns = [
    url(r'^$', views.front, name='front'),
    url(r'^blog/$', views.blog, name='blog'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^blog/((?P<slug>.*)/$', views.page, name='post')
]

Upvotes: 2

kia
kia

Reputation: 828

Have you defined get_absolute_url for Post Model?

from django.core.urlresolvers import reverse
from django.db import models

class Post(models.Model):
   #fields

   def get_absolute_url(self):
      return reverse('post', args=[self.slug])

Upvotes: 3

Related Questions