view raw
jackson makale jackson makale - 8 months ago 36
Python Question

Errors with django urls

I'm getting an error 404 when i click the link on django, i have spent so much time trying to see what i'm doing wrong but no luck.

here is my index.html

<!DOCTYPE html>
<title>Page Title</title>


{% for obj in object_list %}
{% url "detail" %}
<a href = '{{obj.get_absolute_url}}'> {{obj.title}}</a><br/>
{{obj.content}} <br/>
{{obj.timestamp}} <br/>
{{obj.updated}} <br/>
{{}} <br/>

{% endfor %}

This is my

from django.conf.urls import url
from django.contrib import admin

from .views import (

urlpatterns = [
url(r'^$', post_list),
url(r'^create/$', post_create),
url(r'^detail/(?P<id>\d+)/$', post_detail, name='detail'),
url(r'^update/$', post_update),
url(r'^delete/$', post_delete),


This is my

from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404

from .models import Post

# Create your views here.

def post_create(request):
return HttpResponse("<h1>Create</h1>")

def post_detail(request, id): # retreive
instance = get_object_or_404(Post, id=id)
context = {
"title": instance.title,
"instance": instance,
return render(request, "post_detail.html", context)

def post_list(request): # list of posts
queryset = Post.objects.all
context = {
"title": "List",
"object_list": queryset,
return render(request, "index.html", context)

def post_update(request):
return HttpResponse("<h1>Update</h1>")

def post_delete(request):
return HttpResponse("<h1>Delete</h1>")

and this is my

from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Post(models.Model):
title = models.CharField(max_length = 120)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add = False )
timestamp = models.DateTimeField(auto_now=False, auto_now_add = True )

def __unicode__(self):
return self.title

def __str__(self):
return self.title

def get_absolute_url(self):
return "/posts/%s" %(


Your get_absolute_url method returns URLs in the form /posts/<id> but your urlconf is expecting /posts/detail/<id>.

Instead of hard-coding a URL like that in the method, you should use the reverse functionality:

from django.urls import reverse
def get_absolute_url(self):
    return reverse('detail', kwargs={'id':})