1

I am working in a blog application using django. A user can add posts and edit their own posts. However, I am having trouble with retrieving user's posts. Here is a similar question, but it does not work with my application: Django edit form based on add form? Any help is truly appreciate

Here is my code:

@login_required
def edit(request, id):
    if id:
        post = get_object_or_404(BlogPost, id=id)
        if post.author != request.user:
            return render(request, "403.html")
        else:
            post = BlogPost(author=request.user)

    if request.method == "POST":
        form = AddPost(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            messages.add_message(request, messages.SUCCESS,
                                 'You have succesfully updated your post')
            return redirect('homepage')
    else:
        form = AddPost(instance=post)
    return render(request, 'blog/update.html', {'form': form})

Here is my model:

class BlogPost(models.Model):
    title = models.CharField(
        max_length=100, null=False, blank=False, unique=True)
    content = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    author = models.CharField(max_length=50).......
Community
  • 1
  • 1
Alexander
  • 599
  • 2
  • 14
  • 25
  • Your title implies that Django is editing your users' posts. Or is that a prepended tag, which should be removed? In any case, please edit to ask a more specific question in the title. – isherwood Apr 10 '15 at 14:55
  • [HERE](http://tutorial.djangogirls.org/en/django_start_project/README.html) there is a useful tutorial. It starts from the bases then it adds more difficulties. I remember that there is something like yours.. you have to search for. – Trix Apr 10 '15 at 14:55
  • I'm not sure what your actual question is. You say you are "having trouble" - what does that mean? What is actually wrong? – Daniel Roseman Apr 10 '15 at 14:57
  • And I don't know if it's your problem (because I don't know what your problem actually is), but your first `else` clause looks like it should be one indent to the left: ie under `if id`, not `if post.author...`. – Daniel Roseman Apr 10 '15 at 14:59
  • @DanielRoseman Even If I a user is log in and try to edit a post that belongs to that particular user, it returns 403.html. – Alexander Apr 10 '15 at 14:59
  • OK, fair enough. Can you post the BlogPost model? – Daniel Roseman Apr 10 '15 at 15:00
  • @DanielRoseman I have added my model – Alexander Apr 10 '15 at 15:04

1 Answers1

0

Your author field is a CharField. I'm not sure what you're putting into that field, or how you're setting it, but it will never equal request.user, because that is an instance of your User model.

Normally the author field would be a ForeignKey to User, so that the comparison would be true for those posts authored by the current user. If you are putting the username into the author field, you could compare with that: if post.author != request.user.username - but I would really not recommend doing that, as you are breaking normalization for no good reason.

(Also note my other comment: your first else clause looks like it should be one indent to the left: ie under if id, not if post.author....)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895