Quando é feita uma busca por uma receita, o resultado sempre vem a o de "Receita não encontrada", mesmo tendo alguma receita com o nome busca.
Página view.py
def search(request):
recipes = Recipe.objects.order_by('-recipe_date').filter(published=True)
if 'search' in request.GET:
search_name = request.GET['search']
if search_name:
recipes = recipes.filter(recipe_name__icontains=search_name)
new_data = {
'recipe': recipes
}
return render(request, 'search.html', new_data)
seção de busca
{% load static %}
<!-- Search Wrapper -->
<div class="search-wrapper">
<!-- Close Btn -->
<div class="close-btn"><i class="fa fa-times" aria-hidden="true"></i></div>
<div class="container">
<div class="row">
<div class="col-12">
<form action="{% url 'search' %}">
{% csrf_token %}
<input type="search" name="search" placeholder="What a You looking for...">
<button type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
</div>
</div>
</div>
</div>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:recipe_id>', views.recipe, name='recipe'),
path('search', views.search, name='search')
]
models.py
class Recipe(models.Model):
user = models.ForeignKey(Users, on_delete=models.CASCADE)
recipe_name = models.CharField(max_length=200)
ingredients = models.TextField()
method_of_preparation = models.TextField()
time_of_preparation = models.IntegerField()
serves = models.CharField(max_length=100)
category = models.CharField(max_length=100)
recipe_date = models.DateField(default=datetime.now, blank=True)
recipe_image = models.ImageField(upload_to='photos/%d/%m/Y/', blank=True)
published = models.BooleanField(default=False)
def __str__(self):
return self.recipe_name
Obrigado