0

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 
2
  • What does the get_absolute_url method on Post look like? Commented Jan 1, 2016 at 21:54
  • Looking at the code, you haven't defined any such method, so it can't possibly work. If you look at the generated HTML you will undoubtedly see that the href is blank. That's why clicking on the link just takes you back to the same page. Commented Jan 1, 2016 at 22:03

2 Answers 2

3

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]) 
Sign up to request clarification or add additional context in comments.

2 Comments

It goes to the link but it doesn't load the template.
Please use url(r'^blog/(?P<slug>.*)/$', views.page, name='post') and let me know if it works. Would you share your urls.py? Maybe it is caught by other view.
2

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') ] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.