When I click on the submit button, my form validation is equal to True, however nothing displays. The catch is, if I press the submit button again, then everything displays and I no longer have to click the button twice going forward. Up until I restart the server again. I am perplexed and I would greatly appreciate if anyone can tell me what I'm doing wrong.
If I were to remove the validate_on_submit() function the form works fine, but doesn't validate. Which I do not want.
Edit 1
I've added print('------ {0}'.format(request.form)) and print(form.errors) to my code for debugging purposes.
When I insert the values into my form print('------ {0}'.format(request.form)) it prints out the following below:
------ ImmutableMultiDict([('csrf_token', 'IjFiMDE3OGVlZjVjYTYzODQ0MGE2MGMyYWI5NWFjOWYwNjZjMjlmN2Ii.YMFPNw.UtuYhwUnCAlmzdsGNaTEFPM9eek'), ('name', 'Bitcoin'), ('intial_investment', '1500'), ('coin_price', '500'), ('submit', 'Submit')])
While print(form.errors) prints the following:
{'csrf_token': ['The CSRF token is invalid.']}
I would like to believe the root cause of my issue is due to the CSRF being invalid.
However, how can it be invalid when the actual token is listed above?
Edit 2
I've removed {{ form.csrf_token() }} and request.form. Since I don't need to pass in the request when I instance the form.
However, the original issue still persist.
My routes.py
#application.route("/", methods=["GET", "POST"])
def home():
form = Crypto_Form()
coins = None
print('------ {0}'.format(request.form))
print(form.errors)
if form.validate_on_submit():
try:
TWOPLACES = Decimal(10) ** -2
Decimal('3.214').quantize(TWOPLACES)
user_input = form.name.data
coin_name = user_input.lower().replace(" ", "-")
coin_gecko_name = cg.get_coins_markets(vs_currency='usd', ids=[coin_name.lower()], order='market_cap_desc', per_page='1', page='1', sparkline='false')[0]
initial_investment = form.intial_investment.data
coin_image = cg.get_coins_markets(vs_currency='usd', ids=[coin_name.lower()], order='market_cap_desc', per_page='1', page='1', sparkline='false')[0]
coin_price = float(cg.get_price(ids=[coin_name.lower()], vs_currencies='usd')[coin_name.lower()]['usd'])
presale_price = form.coin_price.data
multiplier = Decimal(float(coin_price) / float(presale_price)).quantize(TWOPLACES)
profit = round((float(initial_investment) / float(presale_price)) * float(coin_price))
twenty_four_hour_change = Decimal(cg.get_price(ids=[coin_name.lower()], vs_currencies='usd', include_24hr_change='true')[coin_name.lower()]['usd_24h_change']).quantize(TWOPLACES)
ath = cg.get_coins_markets(vs_currency='usd', ids=[coin_name.lower()], order='market_cap_desc', per_page='1', page='1', sparkline='false')[0]
ath_change_percentage = cg.get_coins_markets(vs_currency='usd', ids=[coin_name.lower()], order='market_cap_desc', per_page='1', page='1', sparkline='false')[0]
coin = Coin(
crypto_name=coin_gecko_name['name'],
coin_initial_invesment=initial_investment,
image=coin_image['image'],
current_price=coin_price,
presale_price=presale_price,
multiple= multiplier,
profit=profit,
twenty_four_hour=twenty_four_hour_change,
all_time_high=ath['ath'],
all_time_percentage_change=Decimal(ath['ath_change_percentage']).quantize(TWOPLACES)
)
db.session.add(coin)
db.session.commit()
except Exception as e:
db.session.rollback()
print('Failed to add coin')
print(e)
coins = Coin.query.all()
coin_profit = Coin.query.with_entities(func.sum(Coin.profit).label('total')).first().total
if coin_profit is None:
coin_profit = 0
return render_template("home.html", coins=coins, value=coin_profit, form=form)
My forms.py
class Crypto_Form(Form):
name = StringField("Enter Coin Name", [validators.DataRequired(message="Please enter the Crypto Currency name.")])
intial_investment = DecimalField("Enter Initial Investment", [validators.DataRequired(message="Please enter a number")])
coin_price = DecimalField("Enter Coin Price", [validators.DataRequired(message="Please enter a number")])
submit = SubmitField("Submit")
My Jinja form
<form class="justify-content-center" method="POST" name="Crypto_Info" role="form">
{{ form.hidden_tag()
<div class="form-group">
{{ form.name.label(class="form-control-label") }}
{% if form.name.errors %}
{{ form.name(class="form-control form-control-lg is-invalid")}}
<div class="invalid-feedback">
{% for error in form.name.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.name(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.intial_investment.label(class="form-control-label") }}
{% if form.intial_investment.errors %}
{{ form.intial_investment(class="form-control form-control-lg is-invalid")}}
<div class="invalid-feedback">
{% for error in form.intial_investment.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.intial_investment(class="form-control form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ form.coin_price.label(class="form-control-label") }}
{% if form.coin_price.errors %}
{{ form.coin_price(class="form-control form-control-lg is-invalid")}}
<div class="invalid-feedback">
{% for error in form.coin_price.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.coin_price(class="form-control form-control-lg") }}
{% endif %}
</div>
<div>
{{ form.submit(class="btn btn-primary") }}
</div>
</form>
Related
How can I set my function in views.py so that a form redirects to the same page after submitting?
For example, I want to add time slots:
In my views.py:
#login_required
def post_available_timeslots(request):
print("Check")
if not check_existing_timeslot(request):
print("Time slot does not exist")
instance = TimeSlot()
data = request.POST.copy()
data["time_slot_owner"] = request.user
# data["food_avail_id"] = FoodAvail.objects.get(author=request.user).pk
form = TimeSlotForm(data or None, instance=instance)
if request.POST and form.is_valid():
form.save()
return redirect("food_avail:view_food_avail_res")
else:
print("Time slot does not exist")
messages.info(request, "Time Slot Already Exists")
return render(request, "food_avail/view_food.html", {"time_slot": form})
in models.py
class TimeSlot(models.Model):
time_slot_owner = models.ForeignKey(User, on_delete=models.CASCADE)
# food_avail_id = models.ForeignKey(FoodAvail, on_delete=models.CASCADE)
start_time = models.TimeField()
end_time = models.TimeField()
def __str__(self):
return str(self.start_time) + "-" + str(self.end_time)
In forms.py:
class TimeSlotForm(ModelForm):
class Meta:
model = TimeSlot
fields = ["start_time", "end_time"]
In urls.py:
from django.urls import path
from food_avail import views
app_name = "food_avail"
urlpatterns = [
path("post_food_avail/", views.post_available_food, name="post_food_avail"),
# path("post_food_avail/", views.FoodAvailCreateView.as_view(), name="post_food_avail"),
# path("update_food_avail/", views.post_available_food, name="update_food_avail"),
path("food_avail_all/", views.check_food_availibility, name="view_food_avail"),
path("food_avail_res/", views.view_available_food, name="view_food_avail_res"),
# path("food_avail_res/", views.post_available_timeslots, name="create_timeslots"),
]
in my html template:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
{{ field.label }} <strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
{{ field.label }} <strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
{% if user.is_authenticated %}
<div>
<div>
<div>
<div>
<h4>
{{ food.author.username }}<br>
</h4>
</div>
<div>
Food Available: {{ food.food_available }}<br>
Description: {{ food.description }}<br>
{% endif %}
</div>
</div>
<div>
<h4>Add Time Slot</h4>
<br><br>
</div>
<div>
<div>
<form method="post">
{{ time_slot.as_p }}
<button type="submit" class="button btn-success">Submit</button>
</form>
</div>
{% if time_slot %}
<h4> List Of Time Slots </h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover ">
<thead>
<tr>
<th>Start Time</th>
<th>End Time</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for x in time_slot %}
<tr>
<td>{{x.start_time | time:'H:i:s'}}</td>
<td>{{x.end_time | time:'H:i:s'}}</td>
<td>
{% endfor %}
{% endif %}
I have been stuck on this problem for a minute now so any help would be much appreciated. This is how it currently shows up on my HTML template:
I have been researching how can I avoid using snippet of code over and over. The answer probably will involve using (generic) Class-based functions. However, I am a beginner in Django and this seems confusing. Here is my view in views.py:
#login_required(login_url='/login')
def view_list(request, listing_id):
bid = Bid.objects.all().filter(listing=listing_id).order_by('-id')
b_u = bid[0].user
listing = Listing.objects.get(pk=listing_id)
if request.method == "GET":
return render(request, "auctions/view_list.html", {
"form": BidForm(),
"total_bids": bid.count(),
"bid": None if bid == 0 else bid[0].bid,
"listing": listing,
"bid_user": "Your bid is the current bid." if request.user == b_u else None
})
else:
form = BidForm(request.POST)
if form.is_valid():
value = form.cleaned_data
if value['bid'] <= bid[0].bid:
error_check = True
return render(request, "auctions/view_list.html", {
"error_check": error_check,
"alert": f"Your bid is lower than the current bid $({bid[0].bid})! Try placing a higher one.",
"form": BidForm(),
"total_bids": bid.count(),
"bid": None if bid == 0 else bid[0].bid,
"listing": listing,
"bid_user": "Your bid is the current bid." if request.user == b_u else None
})
else:
error_check = False
new_bid = form.save(commit=False)
new_bid.user_id = request.user.id
new_bid.listing_id = listing.id
new_bid.save()
return render(request, "auctions/view_list.html", {
"error_check": error_check,
"alert": "Your bid was successfully placed!",
"form": BidForm(),
"total_bids": bid.count(),
"bid": None if bid == 0 else bid[0].bid,
"listing": listing,
"bid_user": "Your bid is the current bid." if request.user == b_u else None
})
And here is my template code:
{% extends "auctions/layout.html" %}
{% load humanize %}
{% load crispy_forms_tags %}
{% block body %}
{% if error_check == True %}
<div class="alert alert-warning" role="alert">
{{ alert }}
</div>
{% elif error_check == False %}
<div class="alert alert-success" role="alert">
{{ alert }}
</div>
{% endif %}
<div>
<h3>Listing: {{ listing.title }}</h3>
<img src="{{ listing.image }}" alt="Listings' Images">
<p>{{ listing.description }}</p>
{% if not bid %}
<strong>${{ listing.price|stringformat:"1.2f" }}</strong>
{% else %}
<strong>${{ bid|stringformat:"1.2f" }}</strong>
{% endif %}
<p> {{ total_bids }} bid(s) so far. {% if bid_user %} {{ bid_user }} {% endif %}</p>
<form method="POST" name="bidding" action="{% url 'view_list' listing.id %}">
{% csrf_token %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">$</span>
</div>
<div style="margin: 0; padding: 0 2px; height: 6px;">
{% crispy form %}
</div>
<div class="input-group-append" >
<span class="input-group-text">.00</span>
</div>
<input type="submit" class="btn btn-primary" value="Place Bid">
</div>
</form>
<h4>Details</h4>
<li>Listed by: {{ listing.user }} </li>
<li>Category: {{ listing.category }} </li>
<li>Listing created at: {{ listing.created_at }} </li>
</div>
{% endblock %}
So how can I avoid all this repetition and make the code more succinct. Also, this way when the user places a successful bid, the rendered template does not contain the new information from the form.
The pattern is very simple
def some_view(request):
form = SomeForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
# Form processing
return render(request, "auctions/view_list.html", {
"form": form
})
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 %}
Here is my route function :
#app.route('/home/shelter/<int:shelter_id>/', methods=['GET','POST'])
def showShelterDetails(shelter_id):
shelter = session.query(Shelter).filter_by(id = shelter_id).one()
if request.method == 'POST':
if request.form['shelterName']:
shelter.name = request.form['shelterName']
session.add(shelter)
session.commit()
flash("Shelter("+shelter.name+") edited successfully!")
return render_template('shelterdetails.html', shelter_id=shelter_id, shelter=shelter)
else:
flash(u'Error Occured!','error')
return render_template('shelterdetails.html', shelter_id=shelter_id, shelter=shelter)
if request.form['shelterAddress']:
shelter.name = request.form['shelterAddress']
session.add(shelter)
session.commit()
flash("Shelter("+shelter.name+") address edited successfully!")
return render_template('shelterdetails.html', shelter_id=shelter_id, shelter=shelter)
else:
flash(u'Error Occured!','error')
return render_template('shelterdetails.html', shelter_id=shelter_id, shelter=shelter)
else:
return render_template('shelterdetails.html', shelter_id = shelter_id, shelter = shelter)
Here is my HTML template :
{% extends "master.html" %}
{% block title %}Home{% endblock %}
{% block body %}
{% with messages = get_flashed_messages(category_filter=["message"]) %}
{% if messages %}
<div class="alert alert-success">
{% for message in messages %}
<strong>{{message}}!</strong>
{% endfor %}
</div>
{% endif %}
{% endwith %}
{% with errors = get_flashed_messages(category_filter=["error"]) %}
{% if errors %}
<div class="alert alert-danger">
{% for message in errors %}
<strong>{{message}}!</strong>
{% endfor %}
</div>
{% endif %}
{% endwith %}
<form action="{{ url_for('showShelterDetails',shelter_id=shelter_id )}}" method = 'post'>
<fieldset class="form-group">
<label for="shelterName">Name :</label>
<input type="text" class="form-control" style="width:500px" name="shelterName" value='{{ shelter.name }}' >
</fieldset>
<fieldset class="form-group">
<label for="shelterName">Address :</label>
<input type="text" class="form-control" style="width:500px" name="shelterAddress" value='{{ shelter.address }}'>
</fieldset>
My problem is : The POST method doesn't check whether the shelterAddress field is empty or blank or not. Also while returning the success or failure messages. Only the top 1 message gets printed.
Why do the other messages not get printed ? Also why doesn't it check for empty fieds ?
EDIT 1:
Changed my view function to :
#app.route('/home/shelter/<int:shelter_id>/', methods=['GET','POST'])
def showShelterDetails(shelter_id):
shelter = session.query(Shelter).filter_by(id = shelter_id).one()
if request.method == 'POST':
if request.form['shelterName']!="":
shelter.name = request.form['shelterName']
session.add(shelter)
session.commit()
flash("Shelter("+shelter.name+") edited successfully!")
if request.form['shelterAddress']!= "":
shelter.name = request.form['shelterAddress']
session.add(shelter)
session.commit()
flash("Shelter("+shelter.name+") address edited successfully!")
else:
return render_template('shelterdetails.html', shelter_id = shelter_id, shelter = shelter)
ValueError: View function did not return a response
That's because you're returning early whether request.form['shelterName'] is truthy or not. You probably want one return statement at the very end of your view function instead.
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 %}