我最近几个月在学姜戈。现在我正在做一个演示项目,是关于教师谁可以教首选学科。但是在工作了几天之后,我无法超越编程知识的某些局限性。我想在一些选定的帖子上显示一个按钮。我使用for循环来创建post模型,在其中还使用了if语句。但这不是我想要的方式。我希望只有当
教师可用
. 你们能帮我解决这个问题吗?事先谢谢!
在post模型中,我包含了一些布尔字段,并将它们的默认值设置为false。在blog视图中,我为这些布尔字段创建了一些查询集,以便在需要时调用它们。当我在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)
以下是blog应用程序的post模型
# 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 %}