search_start and filter by category

This commit is contained in:
Mikhail Ronchyk 2021-12-23 05:19:11 +03:00
parent 8ded2cefb5
commit 404ec59e69
8 changed files with 86 additions and 5 deletions

View File

@ -5,12 +5,28 @@
<title>Events</title>
</head>
<body>
<p>All {{ numofnews }} news</p>
<br>
<p>Wyszukaj</p><br>
<p>Kategorie</p><br>
<p>Filtry</p><br>
{% block content %}
<form method="GET" action="{% url 'events:search' %}">
<input name="q" value="{{ request.GET.q }}" placeholder="search">
<button type="submit">Search</button>
</form>
<nav>
<p class="cat">Category:</p>
{% for i in categories %}
<a class="cat" href="category/{{ i.id }}/">{{ i.name }}</a><br>
{% endfor %}
</nav>
<br>
<nav>
<h1 class="cat">Events:</h1>
@ -21,6 +37,7 @@
<p>Kiedy sie odbedzie: {{ i.data }}</p>
<p>Likes: {{ i.likes }}</p>
<p>Opublikowane: {{ i.pub_date }}</p><br>
<p>Kategorie: {{ i.category }}</p>
{% endfor %}
</nav>
{% endblock %}

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ListOfCat</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
</head>
<body>
<h1>{{ category.name }}</h1>
{% for p in post %}
<ul>
<li><a href="">{{ p.title }}</a></li>
<br>
</ul>
{% endfor %}
</body>
</html>

View File

@ -8,5 +8,8 @@ urlpatterns = [
path('', views.index, name='events'),
path('add_event/', views.create),
path('add_category/', views.createCategory),
path('search/', views.search, name='search'),
path('category/<int:pk>/', views.listofcategories, name='listofcategories'),
path('all_events/', views.events, name='events'),
#path('events/', TemplateView.as_view(template_name='events/index.html'), name='events'),
]

View File

@ -3,8 +3,9 @@ from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.utils import timezone
from .models import Event
from .models import Event, Category
from .forms import EventForm, CategoryForm
from django.shortcuts import render, get_object_or_404, redirect
from django.db.models import Q
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
@ -38,6 +39,7 @@ def events(request):
page = request.GET.get('page', 1)
paginator = Paginator(latest_news_list, 10)
numofnews = len(Event.objects.all())
categories = Category.objects.all()
try:
users = paginator.page(page)
@ -52,6 +54,8 @@ def events(request):
'Place': Event.place,
'description': Event.description,
'numofnews': numofnews,
'category': Category.name,
'categories': categories
}
return render(request, 'events/all_events.html', context)
@ -101,4 +105,40 @@ def createCategory(request):
form = CategoryForm()
context = {'form': form}
return render(request, 'add_category.html', context)
return render(request, 'add_category.html', context)
def search(request):
template = 'events/all_events.html'
query = request.GET.get('q')
result = Event.objects.filter(Q(title__contains=query) | Q(description__contains=query))
page = request.GET.get('page', 1)
paginator = Paginator(result, 10)
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
context = {
'result': result,
'page': page,
'paginator': paginator,
'users': users,
}
return render(request, template, context)
def listofcategories(request, pk):
categories = Category.objects.all()
post = Event.objects.all()
category = get_object_or_404(Category, pk=pk)
post = post.filter(category=category)
template = 'events/listofcategories.html'
context = {
'categories': categories,
'post': post,
'category': category
}
return render(request, template, context)

View File

@ -21,12 +21,14 @@ from events import views
urlpatterns = [
#path('events/', include('events.urls')),
#path('events/', TemplateView.as_view(template_name='events/index.html'), name='events'),
path('events/', TemplateView.as_view(template_name='index.html')),
path('events/', include('events.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('profil/', views.index, name='profil'),
path('profil/add_event/', views.create),
path('profil/add_category/', views.createCategory),
path('all_events/category/<int:pk>/', views.listofcategories, name='listofcategories'),
path('all_events/', views.events, name='events'),
#path('profil/add_event/', TemplateView.as_view(template_name='add_event.html')),
]