Django pagination resets - python

I am building an application that lists certain records from a database. The database is quite large and I am building a pagination to show a specific number of rows. Currently I have set a default value for the paginate_by. Here is the class that I am using.
class HomePageView(ListView):
model = Syslog
template_name = 'home.html'
context_object_name = 'all_logs';
paginate_by = 10
def get_paginate_by(self, queryset):
return self.request.GET.get("paginate_by", self.paginate_by)
I have implemented and a feature that allows clients to change the pagination levels like so:
<form action="" method="get">
<select name="paginate_by" onchange='this.form.submit()'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</form>
When the home page is loaded with the default value the number of pages show correctly. When a user submit another paginate_by value the number of pages change accordingly. The problem is that when the pagination parameter is changed and a user click on let's say 2nd page the paginate_by resets to the default.
Regards,
Jordan

you have to first get request path and after that you have to put a page number in
{{ request.path }} # -without GET parameters
{{ request.get_full_path }} # - with GET parameters
you have to use without get parameters
<select name="report" onchange="location = this.value;" class="form-control">
<option value="{{request.path}}?page=page_logic">1</option>
</select>
here page_logic can be 1,2,3,4 ...

I have figure out a way to do what I was looking for, so I ended up with this.
This is how the form looks like.
<form action="" method="get" id="form">
<select name="paginate_by" onchange = "this.form.submit();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
For the pagination feature I ended up using a custom template tag that alters the ?page parameter, so when a client clicks on ?page=1 for example, he is redirected to www.example.com/?paginate_by=2&page=1 and if he clicks on ?page=2, he is redirected to www.example.com/?paginate_by=2&page=2 (without the custom tag he would be redirected to www.example.com/?paginate_by=2&page=1&page=2)

Related

Method Not Allowed (POST): /items/

So i'm trying to do a filter with django, to filter the items on the main page. A little context:
An item can be assigned to a user or not. I want a filter, to see what items are assigned and which items aren't.
This is my function in views.py:
class ItemsView(ListView):
template_name = 'inventory/items.html'
context_object_name = 'items_list'
def get_queryset(self):
if (self.request.user.is_authenticated):
if self.request.method == "get":
searched = self.request.GET.get('searched')
if searched == "None":
return Item.objects.filter(assigned_user__isnull=True, company=getCompany(self.request.user))
else:
return Item.objects.filter(assigned_user__isnull=False, company=getCompany(self.request.user))
else:
return Item.objects.filter(company=getCompany(self.request.user))
And this is from my items.html:
<form method="post" action="/items">
{% csrf_token %}
<select name="item_filter">
<option value="None">Not Checked In</option>
<option value="Checked_In">Checked In</option>
</select>
<input type="submit" value="filter">
</form>
So basically what i want is, that the user can pick one of the two choices in the dropdown-menu, and the items should be listed based on the choice he made.
When i use this form and click the submit button, the screen gets white and in my console, the error
Method Not Allowed (POST): /items/ appears. Has this something to do with the fact i'm using the generic.ListView for my view?
Thank you
in list view is POST not allowed. But you dont need it:
In template:
<form action="/items">
<select name="item_filter">
<option value>Not Checked In</option>
<option value="Checked_In">Checked In</option>
</select>
<input type="submit" value="filter">
</form>
in view:
class ItemsView(ListView):
... # any staff
def get_queryset(self):
query = {'company':getCompany(self.request.user)}
if (self.request.user.is_authenticated):
query['assigned_user__isnull'] = bool(self.request.GET.get('item_filter'))
return Item.objects.filter(query)

In django python i want to get the users name as well as only the active users in the dropdown

I am having two html forms where i fill the forms and submit and show the forms right now I want two fields to be worked simultaneously I have two fields
This is what I am fetching the value of database and showing in html form as dropdown
<select name="assignee" required id="assignee" class="form-control">
<option hidden value="">Select Assignee</option>
{% for r in b %}
<option>{{r.firstname}} {{r.lastname}}</option>
{% endfor %}
</select>
This is my status dropdown to select the assignees are active or inactive now from the above html snippet I want to do something that it only fetches the active users right now I am fetching the all users but I want to fetch only the active users and show it in the dropdown and not the inactive how can I do this if anyone knows please help.
<select required name="status" id="status" class="form-control">
<option hidden value="">Select Status</option>
<option value = "Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
Then in the view that renders the first snippet, you have
b = Users.objects.all()
make it
b = Users.objects.filter(is_active=True)

can i do process with form django python

there is a input and select with options in form tag. i want to get input value and option that entered. and i will do somethings on backend with python that value and option than i will return a result to website. but i dont use to forms.py. how can i do? is it possible?
<form action="" method="post">
{% csrf_token %}
<input type="text" name="getinputValue">
<select name="">
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
</select>
<button type=""class=""></button>
<p>{{result}}</p>
</form>
So, from what I understanding in your question, you want to retrieve the data from the form in your views and do something with it. You will have to give your <select> a name attribute so that you can reference it in your views.
<select name="level">
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
</select>
In your views, one of the ways you can retrieve this data is by:
def options_view(request):
level = request.POST.get('level') # get name of option as specified in the <select> tag
# here you can do something with it
print(level)
return render(request, 'home.html', {'level': level})
This is one way to do it.

Flask/Python - handling dropdown to open different HTML pages

I'm learning flask and Python along with HTML and CSS. I've got a Flask template to render a dropdown.
What I need to do is this: When you select a value from the dropdown it opens a new HTML page corresponding to the value chosen(different page for different options chosen).
I tried searching the web but couldn't get much resources. When I submit the dropdown option by submit button the page gives a error message saying:
Method Not Allowed
The method is not allowed for the requested URL.
Please guide me to the best possible solution.
Below is my code.
pro.html
<form name="startpage" method="POST" action="">
<div class="form-group">
<div class="input-group">
<select name = "method_cipher" id = "method_cipher">
<option value="Encrypt">Encrypt</option>
<option value="Decrypt">Decrypt</option>
</select>
<select name = "cipher_type" id = "cipher_type">
<option value="Caesar">Caesar</option>
<option value="Transposition">Transposition</option>
<option value="Reverse">Reverse</option>
</select>
</div>
<button type="submit" name="submit" value="success" class="btn btn-default">Submit</button>
</div>
</form>
test.py
import flask
APP = flask.Flask(__name__)
#APP.route('/')
def index():
return flask.render_template('pro.html')
#APP.route("/test" , methods=['GET', 'POST'])
def test():
if flask.request.method == 'POST':
select = flask.request.form.get('method_cipher')
if(select == 'Encrypt'):
return flask.render_template('lastpage.html')
if __name__ == '__main__':
APP.debug=True
APP.run()
EDIT - I want to open different web pages(HTML page) for the different dropdown option as chosen by the user like when user chose "Encrypt" and submit I want to open "lastpage.html" if he chose "Decrypt" then any other page and so on for different options. But it is not happening so.
Your form in pro.html sends the data to the url specified by the action attribute. If you leave that out or set it to an empty string like you did the requests goes to the same page which is /.
But you already set up the form handling in a different view function. You probably want to post to that one.
So change your form to post to the correct url
<form ... action="/test" ...>
That will work but it is not a good idea to hardcode the target url in the template. I assume you are using jinja so you can let it insert the url for you
<form ... action="{{ url_for("test") }}" ...>
Take a look at the url_for function if you haven't already.
Simply do this
<select onchange ="if (this.value)window.location.href=this.value">
<option value="/target1">test1<option>
<option value="/target2">test2<option>
</select>

Django. How to connect drop-down list (from html) and forms?

A have a field in model (name model is Users_data):
bir_date = models.DateField(verbose_name="")
And form which represents model:
class Form_registration (ModelForm):
class Meta:
model = Users_data
in html:
<form name="registration" method="post" action="save_data_user/">
{% csrf_token %}
{{ form_registration.as_p }}
<input type="submit" value="SignUp">
</form>
View which saves form:
def saves_data_user_on_registration (request):
if request.method == 'POST':
c = {}
c.update(csrf(request))
form_user_data = Form_registration(request.POST, request.FILES)
if form_user_data.is_valid():
print form_user_data.errors
form_user_data.save()
return render_to_response('see_you_later.html', c, context_instance=RequestContext(request))
else:
print form_user_data.errors
return render_to_response('error.html', c, context_instance=RequestContext(request))
I can save simple data in form.
But I need save a data in drop-down list from html in field from my model Users_data.
<select name="DateOfBirth_Month">
<option>Month</option>
<option value="1">January</option>
<option value="2">February</option>
...
<select id="cd-dropdown" class="cd-select">
<option value="-1" selected>Day</option>
<option value="1">01</option>
<option value="2">02</option>
...
<select name="DateOfBirth_Year">
<option>Year</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
...
And I don't understand how I can to connect drop-down list with my form or model.
Thanks for answers.
You need to define a widget and set it for the DateField using widgets on the Meta class.
A good example of the particular widget that splits year, month and day into separate dropdowns can be found in Django documentation, see DateSelectorWidget:
class Form_registration (ModelForm):
class Meta:
model = Users_data
widgets = {'bir_date': widgets.DateSelectorWidget()}
This code assumes you've created a widgets.py module with DateSelectorWidget class inside.
Another good widget for the task is django.forms.extras.widgets.SelectDateWidget:
Wrapper around three Select widgets: one each for month, day, and
year.
Also see:
Django SelectDateWidget to show month and year only
django-datetime-widget
Hope that helps.
The answer is to use a choices attribute for your DateField in the model.
Here is how to do it: https://docs.djangoproject.com/en/dev/ref/models/fields/

Categories

Resources