【发布时间】:2018-02-13 06:02:40
【问题描述】:
所以我有 manytomany 字段。在模板中我想验证当前用户是否存在于 manytomany 字段中并分别显示输出
模型.py
class Business(models.Model):
businessname=models.CharField(max_length=230,null=True)
creator==models.ForeignKey(User,unique=True)
users=models.ManyToManyField(Users,null=True,related-name="shared")
在我的 veiw.py 中
def Display(request):
BusinessObjects = Business.objects.all()
template="template.html"
return render(request,template,context={'objects':BusinessObjects})
模板.html
<div class="allobjects">
{% for obj in objects %}
{{obj.businessname}}
<!-- verify if the current login user exist in the many to many field-->
{% for currentuser in obj.users %}
{% if user.username in currentuser.username %}
<p> You are sharing this object</p>
{% else %}
<p> You are not sharing this object
{% endif %}
{% endfor %}
{% endfor %}
</div>
尽管用户名不存在,但我的 else 语句不起作用。但是如果用户名存在,则执行第一个语句。我在 if 语句中做错了什么,为什么我的 else 语句不起作用。谢谢。请不要降级我的问题,如果它对你不重要。
【问题讨论】:
标签: python html django-models