[英]Check if logged in user is the author of a post | Django | If-else doesn't work
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 class="right-section-posts">
user: {{ user }} <!--Output: Admin-->
author: {{ post.author }} <!--Output: Admin-->
{% if user == post.author %}
<form action="DELETE">
{% csrf_token %}
<button type="submit" class="delete-btn" name="post-id" value="{{ post.id }}">Delete</button>
</form>
<a href=""><button class="edit-btn">Edit</button></a>
{% endif %}
</div>
They both output the same but the statement returns false?他们都 output 相同但语句返回 false? Why?
为什么?
Models.py模型.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: (我想你可能会说我应该使用
ForeignKey
而不是“CharField”,当使用它时我得到这个错误:
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.
) )
The author
field cannot be a CharField because it represents the user. author
字段不能是 CharField,因为它代表用户。 You need to set author
field as foreignkey.您需要将
author
字段设置为外键。
You need to update your model like this:您需要像这样更新您的 model:
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:然后在html页面中:
<div class="right-section-posts">
{% if posts %}
{% for post in posts %}
{% if request.user == post.author %}
<!--do what you want here-->
{% else %}
{% endif %}
{% endfor %}
{% else %}
<div class="alert alert-info">You have no registered post yet!</div>
<!-- /.container-fluid -->
</div>
{% endif %}
</div>
I also recommend using django-taggit for tags.我还建议使用 django-taggit 作为标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.