Skip to content

Instantly share code, notes, and snippets.

@makthrow
Last active August 29, 2015 14:14
Show Gist options
  • Save makthrow/a5ce6797fcb333a3d59b to your computer and use it in GitHub Desktop.
Save makthrow/a5ce6797fcb333a3d59b to your computer and use it in GitHub Desktop.
# realpython.com course 2 chapter 27.14.7 homework
# django redirect to url of newly added blogpost
def add_post(request):
context = RequestContext(request)
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
form.save(commit=True)
single_post = Post.objects.all().order_by('-created_at')[:1][0]
single_post.url = encode_url(single_post.title)
# this works
#return redirect('/blog/' + single_post.url)
# this works also
return redirect(post, post_url = single_post.url)
else:
print form.errors
else:
form = PostForm()
return render_to_response('blog/add_post.html', {'form': form}, context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment