Django request is launched automatically - django

I'm getting a little issue and I don't remember the way to solve my problem.
I have a template which let to query database and get a result according to user's criteria.
My view looks like :
#login_required
def Identity_Individu_Researching(request) :
query_lastname_ID = request.GET.get('q1ID')
query_firstname_ID = request.GET.get('q1bisID')
query_naissance_ID = request.GET.get('q1terID')
sort_params = {}
set_if_not_none(sort_params, 'id__gt', query_lastname_ID)
set_if_not_none(sort_params, 'Prenom__icontains', query_firstname_ID)
set_if_not_none(sort_params, 'VilleNaissance', query_naissance_ID)
query_ID_list = Individu.objects.filter(**sort_params)
return render(request, 'Identity_Individu_Recherche.html', context)
But this request is launched automatically when the template is loaded.
In my HTML template, I have :
<form autocomplete="off" method="GET" action="">
<input type="text" name="q1ID" placeholder="Nom (ex:TEST) " value="{{ request.GET.q1ID }}"> et
<input type="text" name="q1bisID" placeholder="Prénom (ex:Test)" value="{{ request.GET.q1bisID }}">
<input type="text" name="q1terID" placeholder="Ville Naissance" value="{{ request.GET.q1terID }}"> (optionnel)
<input class="button" type="submit" name="recherche" value="Rechercher">
</form>
<br></br>
<table style="width:120%">
<tbody>
<tr>
<th>ID</th>
<th>État</th>
<th>N° Identification</th>
<th>Civilité</th>
<th>Nom</th>
<th>Prénom</th>
<th>Date de Naissance</th>
<th>Ville de Naissance</th>
<th>Pays de Naissance</th>
<th>Institution</th>
</tr>
{% for item in query_ID_list %}
<tr>
<td>{{ item.id}}</td>
<td>{{ item.Etat}}</td>
<td>{{ item.NumeroIdentification}}</td>
<td>{{ item.Civilite }}</td>
<td>{{ item.Nom }}</td>
<td>{{ item.Prenom }}</td>
<td>{{ item.DateNaissance }}</td>
<td>{{ item.VilleNaissance }}</td>
<td>{{ item.PaysNaissance }}</td>
<td>{{ item.InformationsInstitution }}</td>
</tr>
{% endfor %}
</tbody>
</table>
So How I can launch the queryset only if user submit the form with the form button ? I know it's based on name = "jhjh"

You should check if the request contains the form data.
if 'recherche' in request.GET:
...

You can change method for your form in template
<form autocomplete="off" method="POST" action="">
<!-- ^^^^^ -->
in the view:
query_ID_list = Individu.objects.all()
if request.method == 'POST':
# your logic
query_ID_list = query_ID_list.filter(**sort_params)
return render(request, 'Identity_Individu_Recherche.html', context)

Related

Delete multiple rows in django

I am trying to delete severals rows at the same time in django.
How can I simply change the state of the booleanfield 'to_delete', just by clicking on the checkbox?
I am using datatables. The way how I am doing it is to create a boolean to_delete in my model, when the checkbox is selected, I am calling the function delete_multiple_company in my view. However, this doesn`t work. Any idea, what I am doing wrong please. Many Thanks,
I`ve created my view:
views.py
def delete_multiple_company(request, company_id):
company = get_object_or_404(Company, pk=company_id)
company = Company.objects.get(pk=company_id)
company.objects.filter(to_delete=True).delete()
return HttpResponseRedirect(reverse("company:index_company"))
urls.py
url(r'^(?P<company_id>[0-9]+)/delete_multiple_company/$', views.delete_multiple_company, name='delete_multiple_company'),
models.py
class Company(models.Model):
to_delete = models.BooleanField(default=False)
index.html
<span class="fa fa-plus"></span>Delete Companies
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I experienced a similar issue as you, what I did was put in a form.
index.py
<form method="POST" class="post-form">{% csrf_token %}
<button type="submit" class="save btn btn-default">Delete</button>
<span class="fa fa-plus"></span>Delete Companies</a>
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="save btn btn-default">Delete</button>
</form>
Clicking on the button then triggered the POST method in my view.
views.py
def index(request):
if request.method == 'POST':
print('I made it here')
# Put your code here, note you will return a dict, so some trial and error should be expected

How to filter data displayed in a table in django?

I have a project where when a user enters a vehicle no, the database is filtered and a table containing that vehicle no and the information corresponding to is displayed. I further want to filter this displayed table, eg: if the user chooses to see quantity greater than 18kl, then the matching vehicle number with quantity greater than 18 is displayed. Also I want to hide the columns selected by the users as there are many columns. Can someone tell me how to do this in django, or suggest some better ways. (I am providing only the related code snippet.)
forms.py
class VehicleSearch(forms.Form):
vehicl[![enter image description here][1]][1]e_no = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}), required=False)
#filter form
class Filter(forms.Form):
capacity_grp = forms.ChoiceField(label='Show only', widget=forms.RadioSelect,
choices=[('abv', '>18 kl'), ('blw', '<18 kl')], required=False)
views.py
def search(request):
form_1 = forms.VehicleSearch()
if request.method == 'POST' and 'btnform1' in request.POST:
form_1 = forms.VehicleSearch(request.POST)
if form_1.is_valid():
vehicle_no = form_1.cleaned_data['vehicle_no']
transport = models.Transport.objects.filter(vehicle=vehicle_no)
my_dict.update({'transport': transport})
return render(request, 'search.html', my_dict)
search.html
/Vehicle form/
<form id="f1" method="POST">
{% csrf_token %}
{{form_1.as_p}}
<p style="padding: 10px;"><button class="myButton" name="btnform1">Search</button></p>
</form>
/*Table display*/
<div class="submain">
{% if transport %}
<table id="transportation">
<thead>
<th>Vehicle</th>
<th>Carrier</th>
<th>Location No</th>
<th>MCMU</th>
<th>Location</th>
<th>Customer Code</th>
<th>Zone</th>
<th>Quantity</th>
<th>RTKM</th>
<th>KL* KM</th>
<th>Amount</th>
<th>Load</th>
<th>Capacity</th>
<th>Rate</th>
<th>Cost</th>
</thead>
{% for i in transport %}
<tr class="item">
<td>{{ i.vehicle }}</td>
<td>{{ i.carrier }}</td>
<td>{{ i.location_no }}</td>
<td>{{ i.mcmu }}</td>
<td>{{ i.location }}</td>
<td>{{ i.customer_code }}</td>
<td>{{ i.zone }}</td>
<td>{{ i.quantity }}</td>
<td>{{ i.rtkm }}</td>
<td>{{ i.klkm }}</td>
<td>{{ i.amount }}</td>
<td>{{ i.load }}</td>
<td>{{ i.capacity }}</td>
<td>{{ i.rate }}</td>
<td>{{ i.cost }}</td>
</tr>
{% endfor %}
</table>
</div>
In the table display you can add a loop with the name's field, something like that:
View:
def search(request):
form_1 = forms.VehicleSearch()
if request.method == 'POST' and 'btnform1' in request.POST:
form_1 = forms.VehicleSearch(request.POST)
columns = request.POST.getlist('filter_hide_columns')
if form_1.is_valid():
vehicle_no = form_1.cleaned_data['vehicle_no']
transport = models.Transport.objects.filter(vehicle=vehicle_no)
my_dict.update({'transport': transport}, {'columns': columns})
return render(request, 'search.html', my_dict)
TemplateTag
def lookup(model, attr):
if hasattr(model, attr):
return getattr(model, attr)
else:
return None
refer https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/
Template:
{% if transport %}
<table id="transportation">
<thead>
<tr>
{% for field in columns %}
<th>{{ field.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for t in transport %}
<tr>
{% for field in columns %}
<td>{{ t|lookup:field.name }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
Please refer https://docs.djangoproject.com/en/2.2/topics/forms/#looping-over-the-form-s-fields

Django-- Get model fields in template only for current user

First of all, I know there are lot of questions about accessing model in template but let me explain why this is different.
I want profile page where User can see their details. I am able to do that but with one little bug. If there are 3 Users(say A, B,C) and User A want to see his profile he sees it three time. Like this:
How do I stop the loop after one iteration so that the User only get's his profile information once.
This is my URL:
url(r'^profile/$', views.IndexView.as_view(), name='profile'),
Views.py:
class IndexView(generic.ListView):
template_name = 'profile.html'
def get_queryset(self):
return Profile.objects.all()
and profile.html:
{% if user.is_authenticated %}
{% for profile in object_list %}
<h1>{{ request.user }}'s Profile</h1>
<table>
<tr>
<td>Username:</td>
<td>{{ user.username }}</td>
</tr>
<tr>
<td>First Name:</td>
<td>{{ user.first_name }}</td>
</tr>
<tr>
<td>Last Name:</td>
<td>{{ user.last_name }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ user.email }}</td>
</tr>
<tr>
<td>Personal Info:</td>
<td>{{ user.profile.personal_info }}</td>
</tr>
<tr>
<td>Job Title:</td>
<td>{{ user.profile.job_title }}</td>
</tr>
<tr>
<td>Department:</td>
<td>{{ user.profile.department }}</td>
</tr>
<tr>
<td>Location:</td>
<td>{{ user.profile.location }}</td>
</tr>
<tr>
<td>Expertise:</td>
<td>{{ user.profile.expertise }}</td>
</tr>
<tr>
<td>Phone Number:</td>
<td>{{ user.profile.phone_number }}</td>
</tr>
<tr>
<td>Contact Skype:</td>
<td>{{ user.contact_skype }}</td>
</tr>
<tr>
<td>Contact Facebook:</td>
<td>{{ user.contact_facebook }}</td>
</tr>
<tr>
<td>Contact Linkedin:</td>
<td>{{ user.profile.contact_linkedin }}</td>
</tr>
</table>
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'form-template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<a href={% url 'profile_edit' %}><input type="button" class = " col-sm-offset-2 btn bk-bs-btn-warning " name="cancel" value="Edit" /></a>
</div>
</div>
</form>
{% endfor %}
{% else %}
<h2>Please login to see your Profile</h2>
{% endif %}
I am a newbie to django, thanks is advance.
You're looping over all Profile's but not actually using this data in the loop, instead you're using user.profile.
This means that you likely have 3 Profile objects in the database, then for each you display the details of the current user, which isn't desirable.
So you can remove the loop entirely, and just keep its contents that refer to all the user.profile attributes to achieve the desired result.
edit
Looks like user is passed into your template by default, and points to the currently logged in user. So user.profile will return the profile, and this works without doing anything else. So user.profile.personal_info is valid and should work. When you tried to use profile directly, that is not the same thing, and wasn't defined, so profile.personal_info didn't work. Then in your loop you used profile to loop over Profile objects, but this wasn't necessary or was used. Hope this makes sense.

request.POST.getlist returning None list in Django?

I have a html template like this:
<table border="1" align="center">
{% for result in result %}
<tr>
<td><input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice }}" /></td>
<td><label for="choice{{ forloop.counter }}">{{ choice }}</label><br /></td>
<td>{{ result.file_name }}</td>
<td>{{ result.type }}</td>
<td>{{ result.size }}</td>
<td>{{ result.end_date }}</td>
<td>{{ result.source }}</td>
{% endfor %}
</tr>
</table>
{{ c }}
<h4>Delete File</h4>
result variable is generated from:
def uploaded_files(request):
log_id = request.user.id
b = File.objects.filter(users_id=log_id, flag='F') #Get the user id from session .delete() to use delete
return render_to_response('upload.html', {'result': b}, context_instance=RequestContext(request))
This is where I try to get the value from choice from template:
def delete_files(request):
log_id = request.user.id
choices = request.POST.getlist('choice') #Get the file name from the as a list
for i in choices:
File.objects.filter(users_id=log_id, file_name=i).update(flag='D')
return render_to_response('upload.html', {'c': choices}, context_instance=RequestContext(request))
Include [] after choice because you are getting array form request
choices = request.POST.getlist('choice[]')
this will solve your prob
As Rohan mentioned in the comment, you have no <form> tag in the template, and appear to be simply assuming that clicking on a normal <a> link will submit some data. That's not at all how it works: if you want to submit data from input elements, they need to be inside a <form>, you need to set the action of that form appropriately, and you need to submit it using a <input type="submit"> (or button) rather than a link.
This is basic HTML, by the way, not Django.

problem using django's User authentication model. Uncomprehensible error

I want to register users.
So I made a:
class RegisterForm(ModelForm):
class Meta:
model=User #(from django.auth.contrib.User)
now i display that form in a template for the users to fill it and to submit it.
When i do form.is_valid(), i get this validation error: Your username and password didn't match. Please try again.
Does anyone know what can cause this?
i've included the ModelForm, the view that processes the registration form and the template that displays the registration form.
Thank you for your time and kind concern.
MY MODEL FORM
class RegisterForm(ModelForm):
class Meta:
model=User
THE VIEW THAT HANDLES THE REGISTRATION
def register(request):
if request.method=='POST':
form=RegisterForm(request.POST)
if form.is_valid(): # HERE I GET A VALIDATION ERROR
new_user=User.objects.create_user(username=form.cleaned_data['username'],
password=form.cleaned_data['password'],
email=form.cleaned_data['email'],
)
new_user.is_active=True
new_user.first_name=form.cleaned_data['first_name']
new_user.last_name=form.cleaned_data['lastname']
return HttpResponseRedirect(reverse('confirm_registered'),args=[form.cleaned_data['username']])
else:
return render(request,'login/register.html',{'form':form})
else:
raise ValueError()
THE REGISTRATION TEMPLATE
{% extends "store/index2.html" %}
{% block canvas %}
<h3>Registration</h3>
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="/retailstore/login/register.html">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
<tr>
<td>{{ form.first_name.label_tag }}</td>
<td>{{ form.first_name }}</td>
</tr>
<tr>
<td>{{ form.last_name.label_tag }}</td>
<td>{{ form.last_name }}</td>
</tr>
<tr>
<td>{{ form.email.label_tag }}</td>
<td>{{ form.email }}</td>
</tr>
</table>
<input type="submit" value="register" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
The form is not validating, but your template is set to always show the same error message about username and password not matching:
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
The reason it is not validating is probably because you are only using some of the fields from the User object, and a ModelForm expects all of the non-editable fields. In the Form's Meta, set
fields = 'username', 'password', 'first_name', 'last_name', 'email'
to tell it to only require those fields.
Also note that passwords are not stored in the database as plain text, so creating a user with a password like that will not work. You need to use user.set_password(form_password).
As Dave points out, you always show the same message no matter what the validation error. You should do this:
{% if form.errors %}
{{ form.errors }}
{% endif %}