Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:如何检查客人或页面所有者是否进入页面_Python_Django - Fatal编程技术网

Python Django:如何检查客人或页面所有者是否进入页面

Python Django:如何检查客人或页面所有者是否进入页面,python,django,Python,Django,我在做一个社交网络,我为所有用户提供了一个配置文件模板,如何检查进入页面的来宾或页面所有者,{%if profile.user.username==None%}试图在html中这样编写,但效果很奇怪。对于用户,我使用配置文件模型 class Profile(models.Model): first_name = models.CharField(max_length=200, blank=True) last_name = models.CharField(max_leng

我在做一个社交网络,我为所有用户提供了一个配置文件模板,如何检查进入页面的来宾或页面所有者,{%if profile.user.username==None%}试图在html中这样编写,但效果很奇怪。对于用户,我使用配置文件模型

  class Profile(models.Model):
    first_name = models.CharField(max_length=200, blank=True)
    last_name  = models.CharField(max_length=200, blank=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    email = models.EmailField(max_length=150, blank=True)
    country = models.CharField(max_length=100, blank=True)
    avatar = models.ImageField(default = 'avatar.svg', upload_to = 'avatars/%Y/%m/%d', blank=True)
    friends = models.ManyToManyField(User, blank=True, related_name='friends')
    update = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.user}-{self.created}"
    
    @receiver(post_save, sender=User)
    def update_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)
        instance.profile.save()
试试这个

{% if profile.user == request.user %}
   # i am user
{% else %}
   # i am guest
{% endif %}

or

{% if profile.user.username == request.user.username %}
   # i am user
{% else %}
   # i am guest
{% endif %}

尝试{%if profile.user.username==request.user.username%}。request.user提供当前登录用户。工作出色,谢谢