最近几个月我正在学习Django。现在,我正在进行一个演示项目,该项目是关于那些可以教授首选学科的老师的。但是工作了几天后,我无法克服编程知识的某些限制。我想在某些选定的帖子上显示一个按钮。我使用了FOR循环来创建发布模型,在其中我还使用了IF语句。但这不是我想要的方式。我希望仅在教师可用时显示该按钮。你们能帮我摆脱这个问题吗?提前致谢!
在Post模型中,我包括一些布尔字段并将其默认值设置为False。在博客视图中,我为这些布尔字段创建了一些查询集,以便在需要时调用它们。当我在HTML中使用它们时,不会出现任何错误。请看下面的代码。
这是教师应用程序的教师模型
from django.db import models
from django.utils import timezone
from PIL import Image
class Teacher(models.Model):
name = models.CharField(max_length = 100)
teacher_photo = models.ImageField(upload_to = 'photos/%Y/%m/%d/')
description = models.TextField()
teacher_govt_id = models.CharField(unique = True ,max_length = 20)
phone = models.CharField(unique = True ,max_length = 20)
nid = models.CharField(unique = True ,max_length = 14)
email = models.CharField(max_length = 50)
is_selected = models.BooleanField(default = False)
join_date = models.DateTimeField(default = timezone.now)
def __str__(self):
return self.name
def save(self):
super().save()
img = Image.open(self.teacher_photo.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.teacher_photo.path)
这是博客应用的发布模型
# from django.forms import ModelForm
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from teacher.models import Teacher
class Post(models.Model):
teacher = models.ForeignKey(Teacher, on_delete = models.DO_NOTHING, blank=True, null=True)
author = models.ForeignKey(User, on_delete = models.CASCADE)
title = models.CharField(max_length = 100)
clip = models.FileField(upload_to = 'videos/%Y/%m/%d/')
description = models.TextField(max_length = 2400)
publish = models.DateTimeField(default = timezone.now)
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
is_published = models.BooleanField(default = True)
is_group = models.BooleanField(default = False)
is_private = models.BooleanField(default = False)
is_available = models.BooleanField(default = False)
def __str__(self):
return self.title
这是帖子视图
from django.shortcuts import render
from . models import Post
from teacher.models import Teacher
def post_list(request):
posts = Post.objects.order_by('-publish').filter(is_published = True)
# get teacher
teacher_available = Post.objects.filter(is_available = True)
assigned_private_teacher = Post.objects.all().filter(is_private = True)
assigned_group_teacher = Post.objects.all().filter(is_group = True)
teacher_selected = Teacher.objects.filter(is_selected = True)
context = {
'posts': posts,
'teacher_available': teacher_available,
'assigned_private_teacher': assigned_private_teacher,
'assigned_group_teacher': assigned_group_teacher,
'teacher_selected': teacher_selected
}
return render(request, 'blog/home.html', context)
这是HTML
{% extends 'base.html' %}
{% load static %}
{% block content %}
<a class="skip-link screen-reader-text" href="#content">Skip to content</a>
<!-- <div class="badge"></div> -->
<main id="content" class="main-area">
{% for post in posts %}
<section class="main-content">
<div class="teacher-student">
<div class="teacher">
<img src="{% static 'img/headshot.jpg' %}" alt="">
<!-- <p>TEACHER</p> -->
<a href="user_profile.html">{{ post.author.get_full_name }}</a>
<small>{{ post.publish }}</small>
</div>
<div class="available">
{% if teacher_available %}
<a href="#">Teacher Available</a>
{% endif %}
</div>
</div>
<div class="main-article">
<a href="detail.html">
<h1>{{ post.title }}</h1>
</a>
<video playsinline oncontextmenu="return false;" controls preload="metadata" controlsList="nodownload"
disablePictureInPicture src="{{ post.clip.url}}" type='video/mp4'>
</video>
<article>
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Asperiores magni eveniet
praesentium esse molestias iusto doloremque ducimus nobis ex hic. Natus, ipsum...
</p>
</article>
<div class="total-like-share">
<p class="like"><span>Like 400</span></p>
<p class="share"><span>Comments 4000</span></p>
</div>
</div>
</section>
{% endfor %}
</main>
<!-- Left Side Bar -->
<aside class="sidebar-left">
<div class="left-sidebar">
<h2 class="left-sidebar-title">Result Board</h2>
<div class="updates-contens">
<p><a href="#">Recent Results</a></p>
<p><a href="#">Old Results</a></p>
</div>
</div>
</aside>
<!-- Right Side Bar -->
<aside class="sidebar-right">
<div class="right-sidebar">
<h2 class="right-sidebar-title">Post Updates</h2>
<div class="updates-contens">
<p><a href="#">Latest Posts</a></p>
<p><a href="#">Most comented posts</a></p>
<p><a href="#">Most shared posts</a></p>
</div>
</div>
</aside>
{% endblock content %}
答案 0 :(得分:0)
您不需要通过上下文发送teacher_available
,可以在post
变量中进行访问:
<div class="available">
{% if post.is_available %}
<a href="#">Teacher Available</a>
{% endif %}
</div>
此外,{% if teacher_available %}
不起作用,因为您正在通过该变量发送查询集。如果要检查查询集中是否存在任何值,请使用.exists
。例如:
# in template
{% if teacher_selected.exists %}
# in python
if teacher_selected.exists():
最后,您不需要通过上下文发送teacher_available
,assigned_private_teacher
,assigned_group_teacher
,因为您可以像这样从posts
访问这些值:>
{% for post in posts %}
{% if post.is_avalilable %}
// do something
{% endif %}
{% if post.is_private %}
// do something
{% endif %}
{% if post.is_group %}
// do something
{% endif %}
{% if post.is_selected %}
// do something
{% endif %}
{% endfor %}