Было бы полезно, если бы вы объяснили причину использования action в случае поиска? [закрыто]
Честно говоря, это работает, даже если вы не указываете действие для отправки данных на страницах входа в систему и регистрации. Было бы полезно, если бы вы объяснили причину использования действия в случае поиска??
{% расширяет "base/index.html" %} {% загружает статический %}
{% тело блока %}
<h1 class="display-4 text-center my-5">All Quizzes</h1>
<div class="container">
<div class="d-flex">
<a href="{% url 'all_quizzes_view' %}" class="btn btn-light me-2">All Quiz</a>
{% comment %} <a href="./all-quiz.html" class="btn btn-light me-2">English</a> {% endcomment %}
{% for category in categories %}
<a href="{% url 'cat_search' category.name %}" class="btn btn-sm btn-light m-1">{{category.name}}</a>
{% endfor %}
</div>
</div>
{% comment %} <form class="container d-flex my-4" role="search" method="get" action="{% url 'search' %}">
<input value="{{ query }}" name="q" class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button type="submit" class="btn btn-primary ms-2">Search</button>
</form> {% endcomment %}
<form class="container d-flex my-4" role="search" method="get">
<input type="search" name="q" class="form-control me-2" placeholder="Search quizzes..." value=" " aria-label="Search">
<button type="submit" class="btn btn-primary ms-2">Search</button>
</form>
<div class="container">
<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-3 g-3">
{% if quizzes|length > 0 %} {% comment %} {{ quizzes|length }}, here quizzes object all properties will be counted {% endcomment %}
{% for quiz in quizzes %}
<div class="col">
<div class="card shadow-sm">
<div class="card-body">
<h4> {{quiz.title}} </h4>
<p class="card-text">Total Questions - 10</p>
<p class="card-text"> {{quiz.description|truncatewords:7}} </p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-success">Start Quiz</button>
</div>
<small class="text-body-secondary"> {{quiz.created_at|timesince}} ago </small>
</div>
</div>
</div>
</div>
{% endfor %}
{% else %}
<div class="col-lg-12 col-md-12 col-12">
<p class="text-center text-danger fs-5">No quiz available for this category </p>
</div>
{% endif %}
</div>
</div>
{% тело конечного блока %}
из django.ярлыки импортируют рендеринг из django.contrib.auth.декораторы импортируют логин_required из .модели импортируют тест, категорию из django.db.модели импортируют Q
Создавайте свои просмотры здесь.
@login_required(login_url='логин') определение all_quizzes_view(запрос):
quizzes = Quiz.objects.order_by('-created_at')
categories = Category.objects.all()
context = {"quizzes" : quizzes, "categories" : categories}
return render(request, 'all-quizzes.html', context)
@login_required(login_url='логин') определение cat_search_view(запрос, категория): печать("Запуск просмотра")
# search by category
if category != " ":
quizzes = Quiz.objects.filter(category__name=category).order_by('-created_at')
# In category__name, category is the ForeignKey field in the Quiz model. Category (uppercase) is the model name. category__name accesses the name field inside the related Category model with the help of ForeignKey category field.
else:
quizzes = Quiz.objects.order_by('-created_at')
# print(quizzes)
categories = Category.objects.all()
context = {"categories" : categories, "quizzes" : quizzes}
return render(request, 'all-quizzes.html', context)
@login_required(login_url='логин') определение search_view(запрос):
# print("View triggered")
q = request.GET.get('q', '') # Get the search query (None if no query is provided)
if q:
quizzes = Quiz.objects.filter(Q(title__icontains=q) | Q(description__icontains=q)).order_by('-created_at')
else:
quizzes = Quiz.objects.all().order_by('-created_at') # Show all quizzes if no query
categories = Category.objects.all()
# Define the context dictionary only once
context = {
"quizzes": quizzes,
"categories": categories,
"query": q
}
return render(request, 'all-quizzes.html', context)