I have such views:
def finish_creation(request, pre_id):
pre_post = PrePost.objects.get(pk = pre_id)
if pre_id == pre_post.id:
this = Post.objects.create(
author = pre_post.author,
title = pre_post.title,
text = pre_post.text
)
this.pswd = some_password
this.save()
pre_post.delete()
return HttpResponseRedirect(this.get_absolute_url())
Explanation: unauthorized user (author) can write posts on site, but first creates some temporary pre_post in database and sends email to author with link in it to activate (transfer temporary pre_post to permanent post). After follow a link creates Post from PrePost elements, PrePost deletes from database and user automatically redirects to his publicated post.
But there is error: Pre_Post matching query does not exist.
But everything else is OK:
1) Post creates perfectly,
2) Pre_post deletes perfectly,
and only return HttpResponseRedirect(this.get_absolute_url()) doesn’t work…
However, error page refers on first line in views pre_post = PrePost.objects.get(pk = pre_id)
How could it be?
Thanks.
EDITED:
But when I didn’t delete pre_post everything worked fine as expected (except deletion of pre_post). What’s wrong with that delete()?
This is really bad design to begin with. You shouldn’t be moving data from one table to another to publish it. Simply add a field to the one model. For example:
Then, you can write a custom manager to pull only approved posts:
Finally, in your views and templates, just use
Post.objects.approved(), instead ofPost.objects.all(). So only approved posts will ever be shown on site.You can then only show the
approvedfield in your forms when the user is a superuser, or whatever other designation you want, and only they will be able to mark it as approved.You could also do this many other ways, such as a “Published” status that only superusers could set, etc., but this is the general idea.
EDIT The problem with your current code is that there is always a possibility that an object won’t exist. You must account for that possibility by always wrapping calls to
getintry...exceptblocks: