Monday, July 18, 2016

AttributeError: 'Post' object has no attribute 'get_absolute_url'


Below is the code of views.py. I am getting error when calling HttpResponseRedirect in update and create method. So please let me know how can i solve this ?

from
django.shortcuts import render, get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect from .forms import PostForm from . models import Post def post_create(request): #create Method form = PostForm(request.POST or None) if form.is_valid(): instance = form.save(commit=False) instance.save() return HttpResponseRedirect(instance.get_absolute_url()) context = { "form" : form, } return render(request, "post_form.html", context) def post_list(request): #list method queryset = Post.objects.all() context = { "object_list": queryset, "title": "List" } return render(request, "index.html", context) def post_update(request, id=None): #update Method instance = get_object_or_404(Post, id=id) form = PostForm(request.POST or None, instance=instance) if form.is_valid(): instance = form.save(commit=False) instance.save() return HttpResponseRedirect(instance.get_absolute_url()) #i am getting error here context = { "title": instance.title, "instance": instance, "form": form, } return render(request, "post_form.html", context)

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5486d915-3589-43c8-8d39-943b299f1a20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment