how to search in mulit model using django - python

i want search from all my model in database , so i create this search functions for do this ... but always i got ' No results for this search '
how to fix this ?
view.py:
def search(request):
if request.method == 'GET':
query= request.GET.get('q')
submitbutton= request.GET.get('submit')
if query is not None:
home_database= Homepage.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
pcprograms_database= PCprogram.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
androidapk_database= AndroidApks.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
androidgames_database= AndroidGames.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
antiruvs_database= Antivirus.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
systems_database= OpratingSystems.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
pcgames_database= PCgames.objects.filter(name__icontains=query,app_contect__icontains=query,page_url__icontains=query,app_image__icontains=query)
results= list(chain(home_database,pcprograms_database,androidapk_database,androidgames_database,antiruvs_database,systems_database,pcgames_database))
context={'results': results,
'submitbutton': submitbutton}
return render(request, 'html_file/enterface.html', context)
else:
return render(request, 'html_file/enterface.html')
else:
return render(request, 'html_file/enterface.html')
html search form :
<form id="search_form" action="{% url 'search' %}" method="GET" value="{{request.GET.q}}">
<input id="search_box" type="text" name="q" value="{{request.GET.q}}"
placeholder=" Search For ... "/>
<input id="search_button" type="submit" name="submit" value="Search"/>
</form>
html file :
{% if submitbutton == 'Search' and request.GET.q != '' %}
{% if results %}
<h1> <small> Results for </small><b>{{ request.GET.q }}</b> : </h1>
<br/><br/>
{% for result in results %}
<label id="label_main_app"> <img id="img_main_app_first_screen" src="{{result.app_image.url}}" alt="no image found !" height="170" width="165" > {{result.name}} <br><br> <p id="p_size_first_page"> {{result.app_contect}} <br> <br> <big> See More & Download </big> </p>
</label>
{% endfor %}
{% else %}
<h3> No results for this search </h3>
{% endif %}
{% endif %}
any help please ?

If you are trying to filter with multiple model fields you need Q from django.db.models
Homepage.objects.filter(Q(name__icontains=query)&Q(app_contect__icontains=query),....

Related

Double post after refresh django

After I posted my comment and decided to refresh the page it adds the same comment over and over on every refresh how can I stop this?
The code for the comment review is at the far bottom in the template.html.
And the review code is under "def product_detail" in the views.py.
Can someone give me some tips so if I refresh it dosen't do another post?
template file:
{% extends "base.html" %}
{% load static %}
{% block page_header %}
<div class="container header-container">
<div class="row">
<div class="col"></div>
</div>
</div>
{% endblock %}
{% block content %}
<div class="overlay"></div>
<div class="container-fluid">
<div class="row">
<div class="col-12 col-md-6 col-lg-4 offset-lg-2">
<div class="image-container my-5">
{% if product.image %}
<a href="{{ product.image.url }}" target="_blank">
<img class="card-img-top img-fluid" src="{{ product.image.url }}" alt="{{ product.name }}">
</a>
{% else %}
<a href="">
<img class="card-img-top img-fluid" src="{{ MEDIA_URL }}noimage.png" alt="{{ product.name }}">
</a>
{% endif %}
</div>
</div>
<div class="col-12 col-md-6 col-lg-4">
<div class="product-details-container mb-5 mt-md-5">
<p class="mb-0">{{ product.name }}</p>
<p class="lead mb-0 text-left font-weight-bold">${{ product.price }}</p>
{% if product.category %}
<p class="small mt-1 mb-0">
<a class="text-muted" href="{% url 'products' %}?category={{ product.category.name }}">
<i class="fas fa-tag mr-1"></i>{{ product.category.friendly_name }}
</a>
</p>
{% endif %}
{% if product.get_rating > 0 %}
<small class="text-muted"><i class="fas fa-star mr-1"></i>{{ product.get_rating }} / 5</small>
{% else %}
<small class="text-muted">No Rating</small>
{% endif %}
{% if request.user.is_superuser %}
<small class="ml-3">
Edit |
<a class="text-danger" href="{% url 'delete_product' product.id %}">Delete</a>
</small>
{% endif %}
<p class="mt-3">{{ product.description }}</p>
<form class="form" action="{% url 'add_to_bag' product.id %}" method="POST">
{% csrf_token %}
<div class="form-row">
{% with product.has_sizes as s %}
{% if s %}
<div class="col-12">
<p><strong>Size:</strong></p>
<select class="form-control rounded-0 w-50" name="product_size" id='id_product_size'>
<option value="xs">XS</option>
<option value="s">S</option>
<option value="m" selected>M</option>
<option value="l">L</option>
<option value="xl">XL</option>
</select>
</div>
{% endif %}
<div class="col-12">
<p class="mt-3"><strong>Quantity:</strong></p>
<div class="form-group w-50">
<div class="input-group">
<div class="input-group-prepend">
<button class="decrement-qty btn btn-black rounded-0"
data-item_id="{{ product.id }}" id="decrement-qty_{{ product.id }}">
<span class="icon">
<i class="fas fa-minus"></i>
</span>
</button>
</div>
<input class="form-control qty_input" type="number"
name="quantity" value="1" min="1" max="99"
data-item_id="{{ product.id }}"
id="id_qty_{{ product.id }}">
<div class="input-group-append">
<button class="increment-qty btn btn-black rounded-0"
data-item_id="{{ product.id }}" id="increment-qty_{{ product.id }}">
<span class="icon">
<i class="fas fa-plus"></i>
</span>
</button>
</div>
</div>
</div>
</div>
<div class="col{% if s %}-12 mt-2{% endif %}">
<a href="{% url 'products' %}" class="btn btn-outline-black rounded-0 mt-5">
<span class="icon">
<i class="fas fa-chevron-left"></i>
</span>
<span class="text-uppercase">Keep Shopping</span>
</a>
<input type="submit" class="btn btn-black rounded-0 text-uppercase mt-5" value="Add to Bag">
</div>
<input type="hidden" name="redirect_url" value="{{ request.path }}">
{% endwith %}
</div>
</form>
</div>
</div>
</div>
<div class="w-full mt-6">
<h2 class="text-xl">Reviews</h2>
{% if request.user.is_authenticated %}
<form method="post" action="." class="mt-6 mb-6 p-6 bg-gray-100 rounded-xl">
{% csrf_token %}
<div>
<label>Rating</label><br>
<select class="form-control" name="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="form-group">
<label for="comment">Comment</label>
<textarea class="form-control" rows="5" id="comment" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>
{% endif %}
{% for review in reviews %}
<div class="px-4 py-6 bg-gray-100 rounded-xl mb-4">
<strong>Author:</strong> {{ review.created_by }}<br>
<strong>Rating:</strong> {{ review.rating }}/5<br>
<strong>Date:</strong> {{ review.created_at|date:"Y-m-d" }}<br>
<strong>Comment:</strong><br>
{{ review.content }}
</div>
{% endfor %}
</div>
</div>
{% endblock %}
{% block postloadjs %}
{{ block.super }}
{% include 'products/includes/quantity_input_script.html' %}
{% endblock %}
views.py:
from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db.models.functions import Lower
from .models import Product, Category, Review
from .forms import ProductForm
# Create your views here.
def all_products(request):
""" A view to show all products, including sorting and search queries """
products = Product.objects.all()
query = None
categories = None
sort = None
direction = None
if request.GET:
if 'sort' in request.GET:
sortkey = request.GET['sort']
sort = sortkey
if sortkey == 'name':
sortkey = 'lower_name'
products = products.annotate(lower_name=Lower('name'))
if sortkey == 'category':
sortkey = 'category__name'
if 'direction' in request.GET:
direction = request.GET['direction']
if direction == 'desc':
sortkey = f'-{sortkey}'
products = products.order_by(sortkey)
if 'category' in request.GET:
categories = request.GET['category'].split(',')
products = products.filter(category__name__in=categories)
categories = Category.objects.filter(name__in=categories)
if 'q' in request.GET:
query = request.GET['q']
if not query:
messages.error(request, "You didn't enter any search criteria!")
return redirect(reverse('products'))
queries = Q(name__icontains=query) | Q(description__icontains=query)
products = products.filter(queries)
current_sorting = f'{sort}_{direction}'
context = {
'products': products,
'search_term': query,
'current_categories': categories,
'current_sorting': current_sorting,
}
return render(request, 'products/products.html', context)
def product_detail(request, product_id):
""" A view to show individual product details """
product = get_object_or_404(Product, pk=product_id)
if request.method == 'POST':
rating = request.POST.get('rating', 3)
content = request.POST.get('content', '')
review = Review.objects.create(
product=product,
rating=rating,
content=content,
created_by=request.user
)
reviews = Review.objects.filter(product=product)
context = {
'product': product,
'reviews': reviews
}
return render(request, 'products/product_detail.html', context)
#login_required
def add_product(request):
""" Add a product to the store """
if not request.user.is_superuser:
messages.error(request, 'Sorry, only store owners can do that.')
return redirect(reverse('home'))
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
product = form.save()
messages.success(request, 'Successfully added product!')
return redirect(reverse('product_detail', args=[product.id]))
else:
messages.error(request, 'Failed to add product. Please ensure the form is valid.')
else:
form = ProductForm()
template = 'products/add_product.html'
context = {
'form': form,
}
return render(request, template, context)
#login_required
def edit_product(request, product_id):
""" Edit a product in the store """
if not request.user.is_superuser:
messages.error(request, 'Sorry, only store owners can do that.')
return redirect(reverse('home'))
product = get_object_or_404(Product, pk=product_id)
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES, instance=product)
if form.is_valid():
form.save()
messages.success(request, 'Successfully updated product!')
return redirect(reverse('product_detail', args=[product.id]))
else:
messages.error(request, 'Failed to update product. Please ensure the form is valid.')
else:
form = ProductForm(instance=product)
messages.info(request, f'You are editing {product.name}')
template = 'products/edit_product.html'
context = {
'form': form,
'product': product,
}
return render(request, template, context)
#login_required
def delete_product(request, product_id):
""" Delete a product from the store """
if not request.user.is_superuser:
messages.error(request, 'Sorry, only store owners can do that.')
return redirect(reverse('home'))
product = get_object_or_404(Product, pk=product_id)
product.delete()
messages.success(request, 'Product deleted!')
return redirect(reverse('products'))
You should implement the Post/Redirect/Get architectural pattern [wiki], and thus return a redirect such that the browser will make a GET request next, and thus prevent making another POST request when refreshing the page:
def product_detail(request, product_id):
product = get_object_or_404(Product, pk=product_id)
if request.method == 'POST':
rating = request.POST.get('rating', 3)
content = request.POST.get('content', '')
Review.objects.create(
product=product,
rating=rating,
content=content,
created_by=request.user
)
# redirect to the same page 🖟
return redirect('product_detail', product_id=product_id)
reviews = Review.objects.filter(product=product)
context = {
'product': product,
'reviews': reviews
}
return render(request, 'products/product_detail.html', context)

How do I make my button increment the value of my iteration value and show the effect on the site? (django)

views.py
def create(request):
choicenumber = 3
submitbutton = request.POST.get('submit', False)
choicebutton = request.POST.get('add_choice', False)
if choicebutton:
choicenumber += 1
render(request, 'polls/create.html', {
'questionfields': Question.__dict__,
'choicenumber': choicenumber,
})
if submitbutton:
new_question = Question.objects.create(question_text=request.POST.get('question_text', ''),
pub_date=timezone.now())
if new_question.question_text == '':
context = {
'questionfields': Question.__dict__,
'error_message': "No poll question entered.",
'choicenumber': choicenumber,
}
del new_question
return render(request, 'polls/create.html', context)
else:
for i in range(choicenumber + 1):
choice = request.POST.get(('choice' + str(i)), '')
if choice:
choice_obj = Choice.objects.create(question=new_question, choice_text=choice)
new_question.choice_set.add(choice_obj)
new_question.save()
return HttpResponseRedirect(reverse('polls:detail', args=(new_question.id,)))
else:
return render(request, 'polls/create.html', context)
create.html
{% extends "polls/base.html" %}
{% block title %}Create a Poll{% endblock title %}
{% block header %}Create:{% endblock header %}
{% load custom_tags %}
{% block content %}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:create' %}" method="post"> {% csrf_token %}
{% for field in questionfields %}
{% if field == 'question_text' %}
<label for="{{ field }}">{{ field|capfirst|replace }}:</label>
<input type="text" name="{{ field }}" id="{{ field }}">
<br>
{% endif %}
{% endfor %}
<br>
{% for choice in choicenumber|rangeof %}
<br>
<label for="choice{{ forloop.counter }}">Choice {{ forloop.counter }}:</label>
<input type="text" name="choice{{ forloop.counter }}" id="choice{{ forloop.counter }}">
<br>
{% endfor %}
<br>
<input type="button" name="add_choice" value="Add choice">
<br>
<br>
<input type="submit" value="Create" name="submit">
</form>
{% endblock content %}
I recently added the add_choice button to my template:
<input type="button" name="add_choice" value="Add choice">
Using this, I tried using a request.POST.get() to check if it has been clicked, and if so for it to increment 'choicenumber' and re-render the view with the increased value of choicenumber:
if choicebutton:
choicenumber += 1
return render(request, 'polls/create.html', {
'questionfields': Question.__dict__,
'choicenumber': choicenumber,
})
Since the increment causes choicenumber to increase, within the create.html the for loop for producing the input boxes for each choice --
{% for choice in choicenumber|rangeof %}
<br>
<label for="choice{{ forloop.counter }}">Choice {{ forloop.counter }}:</label>
<input type="text" name="choice{{ forloop.counter }}" id="choice{{ forloop.counter }}">
<br>
{% endfor %}
-- should loop once more than usual on the re-render. This does not happen however, and the website renders as originally.
Does anyone know how to make this work properly or improve my code? I am very new to the Django framework, so any advice would be great. Thanks!
EDIT::
updated views.py
def create(request):
choicenumber = 3
context = {
'questionfields': Question.__dict__,
'choicenumber': choicenumber,
}
submitbutton = request.POST.get('submit', False)
choicebutton = request.POST.get('add_choice', False)
if request.method == 'GET':
request.session['choicenumber'] = 0
if request.method == 'POST':
if choicebutton:
request.session['choicenumber'] += 1
if submitbutton:
new_question = Question.objects.create(question_text=request.POST.get('question_text', ''),
pub_date=timezone.now())
if new_question.question_text == '':
context = {
'questionfields': Question.__dict__,
'error_message': "No poll question entered.",
'choicenumber': choicenumber,
}
del new_question
return render(request, 'polls/create.html', context)
else:
for i in range(choicenumber + 1):
choice = request.POST.get(('choice' + str(i)), '')
if choice:
choice_obj = Choice.objects.create(question=new_question, choice_text=choice)
new_question.choice_set.add(choice_obj)
new_question.save()
return HttpResponseRedirect(reverse('polls:detail', args=(new_question.id,)))
else:
return render(request, 'polls/create.html', context)
EDIT 2::
updated create.html
{% extends "polls/base.html" %}
{% block title %}Create a Poll{% endblock title %}
{% block header %}Create:{% endblock header %}
{% load custom_tags %}
{% block content %}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:create' %}" method="post"> {% csrf_token %}
{% for field in questionfields %}
{% if field == 'question_text' %}
<label for="{{ field }}">{{ field|capfirst|replace }}:</label>
<input type="text" name="{{ field }}" id="{{ field }}">
<br>
{% endif %}
{% endfor %}
<br>
{% for choice in {{ request.session.choicenumber }}|rangeof %}
# the update is here: choicenumber -> {{ request.session.choicenumber }}
# this defaults as 0 though, even if it is defined as 3.
<br>
<label for="choice{{ forloop.counter }}">Choice {{ forloop.counter }}:</label>
<input type="text" name="choice{{ forloop.counter }}" id="choice{{ forloop.counter }}">
<br>
{% endfor %}
<br>
<input type="button" name="add_choice" value="Add choice">
<br>
<br>
<input type="submit" value="Create" name="submit">
</form>
{% endblock content %}

iterate trough a forms in dictionary passed from view to html in python with jinja

I would appreciate your help in the below problem:
I need to use 3 different models in a python app which I want the user to populate from a HTML form. I want to use model forms and I pass all the three different form in one dictionary (see my views.py):
def new_sales_trip(request):
if request.method == "POST":
Restaurant_Form = RestaurantForm(request.POST)
SalesEvent_Form = SalesEventForm(request.POST)
NextAction_Form = NextActionForm(request.POST)
if Restaurant_Form.is_valid() and SalesEvent_Form.is_valid() and NextAction_Form.is_valid():
Restaurants = Restaurant_Form.save()
SalesEvents = SalesEvent_Form.save(False)
NextActions = NextAction_Form.save(False)
SalesEvents.restaurants = Restaurants
SalesEvents.save()
NextActions.restaurants = Restaurants
NextActions.save()
return redirect('/')
else:
allforms = {
'Restaurant_Form': Restaurant_Form,
'SalesEvent_Form': SalesEvent_Form,
'NextAction_Form': NextAction_Form,
}
return render(request, 'sales/SalesTrip_form.html', allforms)
else:
Restaurant_Form = RestaurantForm()
SalesEvent_Form = SalesEventForm()
NextAction_Form = NextActionForm()
c = {}
c.update(csrf(request))
allforms = {
'Restaurant_Form': Restaurant_Form,
'SalesEvent_Form': SalesEvent_Form,
'NextAction_Form': NextAction_Form,
}
return render(request, 'sales/SalesTrip_form.html', allforms)
So far it works - however I don't know how to use this dictionary to iterate trough in my template so I shouldn't reference all the form by name separatelly. I try to to do something like this:
{% for key, value in allforms.items %}
{% for field in value %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <!--this gonna be only displayed if there is an error in it-->
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10"> <!--this gonna be displayed if there is no error -->
{{ field }}
</div>
</div>
{% endfor %}
{% endfor %}
Unforunatelly it is not rendering anything but a blank page. If I try the .items() property than it says:
Could not parse the remainder: '()' from 'allforms.items()'
You just need to add () to the end of items:
e.g.,
{% for key, value in allforms.items() %}
{% for field in value %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <!--this gonna be only displayed if there is an error in it-->
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ field.label_tag }}</label>
<div class="col-sm-10"> <!--this gonna be displayed if there is no error -->
{{ field }}
</div>
</div>
{% endfor %}
{% endfor %}

Django - show the query in the search box on the results page

I'm trying to show User's search query in the Search Box on the results page. A very basic problem, but for some reason, it doesn't work for me
My views.py looks like:
def search(request):
result_list = []
if request.method == 'POST':
query = request.POST['query'].strip()
if query:
print(query)
print(type(query))
# Run our Webhose search function to get the results list!
result_list = run_query(query)
context_dict = {'result_list': result_list, 'query': query}
context_dict = {'result_list': result_list}
return render(request, 'rango/search.html', context_dict)
my search.html template is below:
{% block body_block %}
<div>
<h1>Search with Rango</h1>
<br/>
<form class="form-inline" id="user_form"
method="post" action="{% url 'search' %}">
{% csrf_token %}
<div class="form-group">
<input class="form-control" type="text" size="50"
name="query" value="{{query}}" id="query" />
</div>
<button class="btn btn-primary" type="submit" name="submit"
value="Search">Search</button>
</form>
<div>
{% if result_list %}
<h3>Results</h3>
<!-- Display search results in an ordered list -->
<div class="list-group">
{% for result in result_list %}
<div class="list-group-item">
<h4 class="list-group-item-heading">
{{ result.title }}
</h4>
<p class="list-group-item-text">{{ result.summary }}</p>
</div>
{% endfor %}
</div>
{% endif %}
</div>
</div>
{% endblock %}
Any advice?
My context_dict was being overwritten.
Please find the solution below:
def search(request):
query = ''
result_list = []
if request.method == 'POST':
query = request.POST['query'].strip()
if query:
print(query)
print(type(query))
# Run our Webhose search function to get the results list!
result_list = run_query(query)
context_dict = {'result_list': result_list, 'query': query}
return render(request, 'rango/search.html', context_dict)

Next and Before Links for a django paginated query

I'm trying to make a search form for Django.
Its a typical search form and then returns a table of matches. I wish to paginate the tables returned.
The problem lies in the Previous and Next buttons.
The links for the return query goes to /records/search/?query=a (search sample is a)
The page outputs the table and its previous and next links. However the links redirect to /records/search/?page=2 and the page displays a blank table.
Any help on which links I should pass for Prev/Next?
search.html:
{% extends 'blank.html' %}
{% block content %}
<div class="row">
<form id="search-form" method="get" action=".">
{{ form.as_p }}
<input type="submit" value="Search" />
</form>
</div>
<br><br>
//display table code//
{% if is_paginated %}
<div class="pagination">
<span class="step-links">
{% if agent_list.has_previous %}
forrige
{% endif %}
<span class="current">
Page {{ agent_list.number }} of {{ agent_list.paginator.num_pages }}.
</span>
{% if agent_list.has_next %}
Next
{% endif %}
</span>
</div>
{% endif %}
{% endblock %}
and the search view:
def search_page(request):
form = SearchForm()
agents = []
show_results=False
if request.GET.has_key('query'):
show_results=True
query=request.GET['query'].strip()
if query:
form=SearchForm({'query': query})
agents = \
Agent.objects.filter(Q(name__icontains=query))
paginator = Paginator(agents, 10)
page = request.GET.get('page')
try:
agents = paginator.page(page)
except PageNotAnInteger:
agents = paginator.page(1)
except EmptyPage:
agents = paginator.page(paginator.num_pages)
variables = RequestContext(request,
{ 'form': form,
'agent_list': agents,
'show_results': show_results,
'is_paginated': True,
}
)
return render_to_response('search.html', variables)
I've seen the similar questions but I can't understand/make them work. Any help?
Edit:
For a quickfix (haven't really looked at the cons)
I added a variable in my view:
variables = RequestContext(request,
{ 'form': form,
'agent_list': agents,
'show_results': show_results,
'is_paginated': True,
**'query': query,**
}
)
Where query without the quotes is the recieved query variable.
Then simply change the URL to:
Previous
If you have a better way of answering the question, please do or appending a URL to your currently opened URL.
I would recommend putting the solution in a template tag like so:
myapp/templatetags/mytemplatetags.py:
from django import template
register = template.Library()
#register.simple_tag
def url_replace(request, field, value):
d = request.GET.copy()
d[field] = value
return d.urlencode()
#register.simple_tag
def url_delete(request, field):
d = request.GET.copy()
del d[field]
return d.urlencode()
Then from templates do:
{% load mytemplatetags %}
...
previous
you can use {{ request.get_full_path }} this tag to get current url.
Next
this worked for me
The below works before and after a search form has been submitted:
Views.py
class PostListView(ListView):
model = Post #.objects.select_related().all()
template_name = 'erf24/home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts' # default >> erf24/post_list.html
ordering = ['date_posted']
paginate_by = 3
def is_valid_queryparam(param):
return param != '' and param is not None
def invalid_queryparam(param):
return param == '' and param is None
class SearchView(ListView):
model = Post #.objects.select_related().all()
template_name = 'erf24/home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts' # default >> erf24/post_list.html
ordering = ['date_posted']
paginate_by = 3
def get_queryset(self): # new
key = self.request.GET.get('key')
minp = self.request.GET.get('min')
maxp = self.request.GET.get('max')
if is_valid_queryparam(key):
obj = Post.objects.filter(Q(content__icontains=key) | Q(location__icontains=key)).distinct().order_by('date_posted')
if is_valid_queryparam(minp):
obj = Post.objects.filter(Q(price__gte=minp)).distinct().order_by('date_posted')
if is_valid_queryparam(maxp):
obj = Post.objects.filter(Q(price__lte=maxp)).distinct().order_by('date_posted')
if is_valid_queryparam(minp) & is_valid_queryparam(maxp):
obj = Post.objects.filter(Q(price__gte=minp) & Q(price__lte=maxp)).distinct().order_by('date_posted')
if is_valid_queryparam(key) & is_valid_queryparam(minp) & is_valid_queryparam(maxp):
obj = Post.objects.filter(Q(content__icontains=key) | Q(location__icontains=key)).distinct()
obj = obj.filter(Q(price__gte=minp) & Q(price__lte=maxp)).order_by('date_posted')
if invalid_queryparam(key) & invalid_queryparam(minp) & invalid_queryparam(maxp):
obj = Post.objects.all()
return obj
url.py
urlpatterns = [
path('', PostListView.as_view(), name='erf24-home'),
path('search/', SearchView.as_view(), name='erf24-search'),
]
Home.html
<form action="{% url 'erf24-search' %}" method="GET">
<div class="form-group">
<label for="inputAddress">Search keyword</label>
<input type="text" class="form-control" id="key" name="key" placeholder="keyword">
</div>
<label for="">Price</label>
<div class="form-row">
<div class="form-group col-md-6">
<input type="number" class="form-control" id="min" name="min" placeholder="min price">
</div>
<div class="form-group col-md-6">
<input type="number" class="form-control" id="max" name="max" placeholder="max price">
</div>
</div>
<button type="submit" class="btn btn-primary btn-sm mt-1 mb-1">Search</button>
<button type="reset" class="btn btn-secondary btn-sm mt-1 mb-1">Clear</button>
</form>
{% for post in posts %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}" alt="">
{{ post.author }}
<small class="text-muted">{{ post.date_posted }}</small>
<!-- use |date: "specs" to filter date display -->
</div>
<h2>
{{ post.price }}
</h2>
<p class="article-content">{{ post.content }}</p>
<p class="article-content">{{ post.location }}</p>
<p><a class="like-btn" data-href="{{ post.get_api_like_url }}" href="">{{ post.likes.count }}
{% if user in post.likes.all %} Unlike
{% else %} Like
{% endif %}
</a></p>
</div>
{% for image in post.image_set.all %}
<img class="account-img" src="{{ image.image.url }}" alt="">
{% endfor %}
</article>
{% endfor %}
{% if is_paginated %}
{% if page_obj.has_previous %}
First
Previous
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
{{ num }}
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
{{ num }}
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
Next
Last
{% endif %}
{% endif %}
Worked like a charm :) Enjoy!
You can use {{ request.get_full_path }} template tag
Next
you can use this, I use it because I use filters in the url itself, so all the url params are used to build the next or previous url
import re
from django import template
register = template.Library()
PAGE_NUMBER_REGEX = re.compile(r'(page=[0-9]*[\&]*)')
#register.simple_tag
def append_page_param(value,pageNumber=None):
'''
remove the param "page" using regex and add the one in the pageNumber if there is one
'''
value = re.sub(PAGE_NUMBER_REGEX,'',value)
if pageNumber:
if not '?' in value:
value += f'?page={pageNumber}'
elif value[-1] != '&':
value += f'&page={pageNumber}'
else:
value += f'page={pageNumber}'
return value
then, in your pagination nav you can call it like this:
{% append_page_param request.get_full_path page_obj.previous_page_number %}

Categories