我的Jinja如果我的HTML中的条件不起作用

时间:2019-02-18 13:36:54

标签: html django jinja2

在我的html中,我有一个复选框。我注册并勾选了复选框。我可以从管理面板中看到该复选框为true。但是我的HTML中的“ if”条件不起作用。当复选框条件为True时,它应该仅显示数据库中的一项,否则应显示几项。

我试图在views.py中编写另一个if条件,但是我的代码仍然无法正常工作。

我的views.py

def address(request, pk):
    user = User.objects.all()
    city = Post.objects.get(id=pk).city
    address = Post.objects.filter(city=city)
    email = Post.objects.all()

    context = {
        'user': user,
        'address': address,
        'city': city,
    }

    return render(request, 'users/address.html', context)

我的models.py:

class User(AbstractUser):
    first_name = models.CharField(verbose_name="First name", max_length=255)
    last_name = models.CharField(verbose_name="First name", max_length=255)
    country = models.CharField(verbose_name="Country name", max_length=255)
    city = models.CharField(verbose_name="City name", max_length=255)
    email = models.EmailField(verbose_name="Email", max_length=255)
    access_challenge = models.BooleanField(default=False)

    def __str__(self):
        return self.username

class Post(models.Model):
    title = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    address = models.CharField(max_length=255)
    email = models.EmailField(max_length=255)
    phone = models.CharField(max_length=255)
    website = models.URLField(max_length=255)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('users:blog')

我的html:

{% extends 'shared/base.html' %}
{% load staticfiles %}

{% block content %}

<div class="content-section p-5 mt-5 pl-4">
<h1 class="text-center text-uppercase">{{ city }}</h1>

{% for store in address %}

    {% if user.access_challenge %}


   {{ store.email }}


   {% else %}

<table class="table table-hover text-left col-sm-12 mt-4" style="table-layout: fixed; word-wrap: break-word;">
       <tbody>
         <tr>
           <th style="width: 100px;"> No: </th>
           <td> {{store.pk}}</td>
         </tr>
         <tr>
           <th style="width: 100px;"> Name: </th>
           <td> {{store.title}}</td>
         </tr>

        <tr>
          <th style="width: 100px;"> Address: </th>
          <td> {{store.address}}</td>
        </tr>
        <tr>
          <th style="width: 100px;"> Email: </th>
          <td> {{store.email}}</td>
        </tr>
        <tr>
          <th style="width: 100px;"> Phone: </th>
          <td> {{store.phone}}</td>
        </tr>
        <tr>
          <th style="width: 100px;">Website: </th>
          <td> {{store.website}}</td>
        </tr>
      {% endif %}

    </tbody>
</table>


{% endfor %}
{% endblock %}

</div>

如果用户选中“访问挑战区域”复选框,我只想显示他的电子邮件地址,并隐藏他的城市名称和地址。

1 个答案:

答案 0 :(得分:0)

尝试

{% for store in address %}

{% if store.author.access_challenge %}
   # rest code

当您在此处使用用户外键时