Home > Net >  Check if logged in user is the author of a post | Django | If-else doesn't work
Check if logged in user is the author of a post | Django | If-else doesn't work

Time:12-11

I want to check if the logged in user is the author of a post in my Forum. I have written some code to figure that out:

<div >
            user: {{ user }} <!--Output: Admin-->
            author: {{ post.author }} <!--Output: Admin-->
            {% if user == post.author %}
              <form action="DELETE">
                {% csrf_token %}
                <button type="submit"  name="post-id" value="{{ post.id }}">Delete</button>
              </form>
              <a href=""><button >Edit</button></a>
            {% endif %}
          </div>

They both output the same but the statement returns false! Why?

Models.py

class Post(models.Model):
    vote_count = models.IntegerField(default=0)
    id = models.BigAutoField(primary_key=True)
    created_at = models.DateField(default=date.today)
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    tags = models.CharField(max_length=200)
    author = models.CharField(max_length=100, default="none")

    def __str__(self):
        return str(self.id)   ' '   self.title

I tried different ways to get the Current user and the author. Doesn't work to. (I think you might say that I should use ForeignKey instead of ´CharField´, when using that I get this Error:

ERROR: Column forum_app_post.author_id does not exist.
LINE 1: ...app_post". "description", "forum_app_post". "tags", "forum_app...
                                                             ^
HINT: Perhaps the intention was to refer to the "forum_app_post.author" column.

)

CodePudding user response:

The author field cannot be a CharField because it represents the user. You need to set author field as foreignkey.

You need to update your model like this:

from django.contrib.auth.models import User

class Post(models.Model):
    vote_count = models.IntegerField(default=0)
    id = models.BigAutoField(primary_key=True)
    created_at = models.DateField(default=date.today)
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    tags = models.CharField(max_length=200)
    author = models.ForeignKey(User,on_delete= models.CASCADE, verbose_name='Post Author')

def __str__(self):
    return str(self.id)   ' '   self.title

If you want to check the logged in user from all the registered posts, you should get all the posts first.

def get_all_posts(request):
    posts = Post.objects.filter.all()

    context = {
    "posts" : posts,
    }
    return render(request,"my_page.html",context)

Then in the html page :

<div >
        {% if posts %}
           {% for post in posts %}
              {% if request.user == post.author %}
                <!--do what you want here--> 
              {% else %}
              {% endif %}
           {% endfor %}

        {% else %}
            <div >You have no registered post yet!</div>
        <!-- /.container-fluid -->
        </div>
        {% endif %}
      </div>

I also recommend using django-taggit for tags.

  • Related