我试图在Django(python)的html模型中添加一个条件,但它不起作用:
这是我的HTML:
<p class="card-text text-muted h6">{{ post.author }} </p>
{% if post.author == 'John_Woo' %}
<p class="card-text text-muted h6"> NOK </p>
{% else %}
<p class="card-text text-muted h6"> ok </p>
{% endif %}
不知道这里出了什么事。。。 尽管我有一个吴宇森的作者,但我只收到好的消息
这是my models.py:
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
这是我的观点.py:
from django.views import generic
from .models import Post
class PostList(generic.ListView):
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
class PostDetail(generic.DetailView):
model = Post
template_name = 'post_detail.html'
需要
post.author.name
(其中.name
是User
对象的任何字段,该对象有时包含字符串'John_Woo'
)Post.author
返回类的表示形式,如(str,unicode,repr)函数。你必须重写这个函数,这取决于你的django版本。但你有一种方法来比较。就像if Post.author.first_name == "John_Woo"
相关问题 更多 >
编程相关推荐