我的models.py:
class Recipe(models.Model):
recipe_title = models.CharField("Назва Рецепту", max_length=50)
recipe_text = models.TextField("Текст Рецепту")
recipe_image = models.ImageField(default="", null=True, blank=True, upload_to="recipeImages/")
isHot = models.BooleanField(default=False)
isVegan = models.BooleanField(default=False)
author_name = models.CharField("Імʼя автора", max_length=100)
pub_date = models.DateTimeField("Дата публікації")
def __str__(self):
return self.recipe_title
def isHOT(self):
if self.isHot == True:
return self.recipe_title
我的views.py:
def index(request):
latest_recipes = Recipe.objects.order_by('-pub_date')[:5]
search_query = request.GET.get('search', '')
if 'ishot' in request.POST:
recipes = Recipe.objects.filter(isHot=True).all()
else:
recipes = Recipe.objects.all()
if search_query:
recipes = Recipe.objects.filter(recipe_title__icontains=search_query)
else:
recipes = Recipe.objects.all()
return render(request, 'recipes/list.html', {'latest_recipes': latest_recipes, 'recipes': recipes})
我的html页面:
{% for a in recipes %}
<a href="{% url 'recipes:detail' a.id %}" style="text-decoration: none; color: black;">
<div class="row">
<div class="column">
<div class="card">
<img src="{{a.recipe_image.url}}" style="width: 15%; float:left">
<div id="text">
<h3>{{a.recipe_title}}</h3>
<p>{{a.recipe_text}}</p>
{% if a.was_published_recently %}
<h4 style="color: red;">NEW</h4>
{% endif %}
{% if a.isHOT %}
<img style="width:3%;" src="/images/hot.png" name="#">
{% endif %}
{% if a.isVEGAN %}
<img style="width:3%;" src="/images/vegan.png" name="#">
{% endif %}
</div>
</div>
</div>
</a>
{% endfor %}
“search_query”函数可以工作,“ishot”函数本身也可以工作,但filter“recipes = Recipe.objects.filter(isHot=True).all()”不能。它应该过滤对象,类似的函数可以完成它的工作,我不知道问题出在哪里
我的db(有乌克兰话):
1条答案
按热度按时间szqfcxe21#
您正在向视图发出GET请求,但您正在
request.POST
中查找ishot
。因此,您应该通过以下方法解决此问题: