Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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
Django中的If语句不显示输出_Django_If Statement - Fatal编程技术网

Django中的If语句不显示输出

Django中的If语句不显示输出,django,if-statement,Django,If Statement,我想在request.user编写的注释旁边显示一个delete按钮,这样只有他们才能删除这些注释,但由于某些原因,我的if语句无法正常工作。这是我的模板: {% for post in posts %} ... {% if post.comment_set.all %} {% for comment in post.comment_set.all %} <img src="{{ comment.user.image }}&

我想在request.user编写的注释旁边显示一个delete按钮,这样只有他们才能删除这些注释,但由于某些原因,我的if语句无法正常工作。这是我的模板:

{% for post in posts %}
    ...
    {% if post.comment_set.all %}
        {% for comment in post.comment_set.all %}
            <img src="{{ comment.user.image }}">
            <p>{{ comment.body }}</p>
       
            {% if comment.user == request.user %}
                # delete icon with modal
            {% endif %}

        {% endfor %}
    {% endif %}
{% endfor %}

奇怪的是,在页面的后面,我使用类似的代码在博客帖子旁边显示delete,使用
{%if post.author.user==request.user%}
。基于此,我尝试将多余的if标记更改为
comment.author.user
comment.post.author
comment.post.author.user
和其他变体,但迄今为止没有任何效果。我做错了什么?

请求对象是否包含在上下文中? 您可能需要将请求添加到settings.py中的context processors块 包括这个

'django.template.context_processors.request',
这样,request.user将在模板变量中可用

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [BASE_DIR + '/templates'],
            'OPTIONS': {
                'debug': DEBUG,
                'context_processors': [
                    'django.contrib.auth.context_processors.auth',
                    'django.template.context_processors.debug',
                    'django.template.context_processors.media',
                    'django.template.context_processors.static',
                    'django.template.context_processors.request',
                ],
            },
        },
    ]

谢谢你的回复。是的,my settings.py已经包含这行代码。问题似乎不在于请求。用户,因为我以前能够在没有发出的情况下输出它ahh-ok。您是否尝试过
{%if comment.user.user==request.user%}
?这一定是我没有尝试过的唯一组合之一,而且成功了!太棒了,非常感谢你!
user=models.ForeignKey(Profile,on_delete=models.CASCADE)
中的
user
可能更好地写成
Profile=models.ForeignKey(Profile,on_delete=models.CASCADE)
。允许{%if comment.profile.user==request.user%}Ahh我明白了。现在它有意义了。太棒了,再次感谢,这真的很有帮助!
TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [BASE_DIR + '/templates'],
            'OPTIONS': {
                'debug': DEBUG,
                'context_processors': [
                    'django.contrib.auth.context_processors.auth',
                    'django.template.context_processors.debug',
                    'django.template.context_processors.media',
                    'django.template.context_processors.static',
                    'django.template.context_processors.request',
                ],
            },
        },
    ]