Here is my code for using session in Django. Everything seems fine but getting KeyError at /addfixeddeposit/
views.py
def anotherpage(request, account_number):
form = FixedDepositForm()
context = {'form' : form}
request.session['account_number'] = account_number
return render(request, 'bankmanagement/another_page.html', context)
def addfixeddeposit(request):
if request.method == "POST":
var = request.session['account_number']
print(var)
form = FixedDepositForm(request.POST)
if form.is_valid:
acc = Account.objects.get(pk = var)
tenure = request.POST['tenure']
rate = reuquest.POST['rate']
new_fixed = FixedDeposit(account_number = acc, tenure = tenure, rate = rate)
new_fixed.save()
del request.session['account_number']
return redirect('behome')
return redirect('behome')
It's the html page in which I'm using session value. And then in addfixeddeposit in views.py
another_page.html
<form method="POST" action="{% url 'addfixeddeposit' %}">
{% csrf_token %}
<div class="form-field">
<table>
<tr>
<td><label for="accountnumfield" class="label--required">Account Number</label></td>
<td><label id="accountnumfield">{{ request.session['account_number'] }} </td>
</tr>
<tr>
<td><label for="tenure" class="label--required">Tenure</label></td>
<td><span id='tenure'>{{ form.tenure }}</span></td>
</tr>
<tr>
<td><label for="rate" class="label--required">Rate</label></td>
<td><span id='rate'>{{ form.rate }}</span></td>
</tr>
</table>
</div>
<div class="form-buttons">
<button type="submit" class="a-btn--filled">Add</button>
</div>
</form>
If you're tying to send account_number from my templates to views, use a query string, makes it easier, like this in your templates:
<form method="POST" action="{% url 'addfixeddeposit' %}?account_number={{account_number}}">
And then in your views.py do:
var = request.GET.get('account_number')
Related
I am tasked with making a shopping crud project with models Products,categories,sub_categories,size,colors. Categories and subcategories are connected via foreign keys and I am using SERAILIZERS.the problem is that when I try to insert the data into sub Categories it doesnt come in both the database and the webpage
I also tried value = "{{c.category_name}}" as well in select dropdown as well
below are the models
class Categories(models.Model):
category_name = models.CharField(max_length=10)
category_description = models.CharField(max_length=10)
isactive = models.BooleanField(default=True)
class SUBCategories(models.Model):
category_name = models.ForeignKey(Categories,on_delete=models.CASCADE)
sub_categories_name = models.CharField(max_length=20)
sub_categories_description = models.CharField(max_length=20)
isactive = models.BooleanField(default=True)
show and insert function of sub_categories
def show_sub_categories(request):
showsubcategories = SUBCategories.objects.filter(isactive=True)
#print(showsubcategories)
serializer = SUBCategoriesSerializer(showsubcategories,many=True)
print(serializer.data)
return render(request,'polls/show_sub_categories.html',{"data":serializer.data})
def insert_sub_categories(request):
if request.method == "POST":
insertsubcategories = {}
insertsubcategories['sub_categories_name']=request.POST.get('sub_categories_name')
insertsubcategories['sub_categories_description']=request.POST.get('sub_categories_description')
form = SUBCategoriesSerializer(data=insertsubcategories)
if form.is_valid():
form.save()
print("hkjk",form.data)
messages.success(request,'Record Updated Successfully...!:)')
print(form.errors)
return redirect('sub_categories:show_sub_categories')
else:
print(form.errors)
else:
insertsubcategories = {}
form = SUBCategoriesSerializer(data=insertsubcategories)
category_dict = Categories.objects.filter(isactive=True)
category = CategoriesSerializer(category_dict,many=True)
hm = {'context': category.data}
if form.is_valid():
print(form.errors)
return render(request,'polls/insert_sub_categories.html',hm)
html of the insert page and show page respectively
<td>category name</td>
<td>
<select name="category_name" id="">
{% for c in context %}
<option value="{{c.id}}">{{c.category_name}}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>sub categories Name</td>
<td>
<input type="text" name="sub_categories_name" placeholder="sub categories ">
</td>
</tr>
<tr>
<td>Sub categories Description</td>
<td>
<textarea name="sub_categories_description" id="" cols="30" rows="10">
</textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Insert" />
</td>
<td>
{% if messages %}
{% for mess in messages %}
<b style="color:green;">{{mess}}</b>
{% endfor %}
{% endif %}
</td>
<tbody>
<tr>
<td><b>{{result.sub_categories_name}}</b></td>
<td><b>{{result.sub_categories_description}}</b></td>
<td style="position: relative;left:50px;">
<a href="sub_categories/edit_sub_categories/{{result.id}}">
<button class="btn btn-primary">
<i class="fa-solid fa-pen-to-square">EDIT</i>
</button>
</a>
</td>
<td>
<a href="{% url 'sub_categories:delete_sub_categories' result.id %}" onclick="return confirm('Are You Sure you want to delete?')">
<button class="btn btn-danger">
<i class="fa-solid fa-trash">DELETE</i>
</button>
</a>
</td>
</tr>
</tbody>
categories and sub categories serializer
class CategoriesSerializer(serializers.ModelSerializer):
class Meta:
model = Categories
fields = "__all__"
extra_kwargs = {'category_name': {'required': False}}
class SUBCategoriesSerializer(serializers.ModelSerializer):
class Meta:
model = SUBCategories
fields = "__all__"
where am I going wrong in the code?
Try this out and let me know, a few changes you'd require
def insert_sub_categories(request):
if request.method == "POST":
form = SUBCategoriesSerializer(data=request.POST)
if form.is_valid():
form.save()
messages.success(request, "Record Updated Successfully...!:)")
return redirect("sub_categories:show_sub_categories")
category_dict = Categories.objects.filter(isactive=True)
category = CategoriesSerializer(category_dict, many=True)
hm = {"context": category.data}
return render(request, "polls/insert_sub_categories.html", hm)
Form part
<form method="POST">
{% csrf_token %}
<table>
<thead>
<tr>
<td>category name</td>
<td>
<select name="category_name" id="">
{% for c in context %}
<option value="{{c.id}}">{{c.category_name}}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>sub categories Name</td>
<td>
<input type="text" name="sub_categories_name" placeholder="sub categories ">
</td>
</tr>
<tr>
<td>Sub categories Description</td>
<td>
<textarea name="sub_categories_description" id="" cols="30" rows="10">
</textarea>
</td>
</tr>
<tr>
<td>Active</td>
<td>
<input type="checkbox" name="isactive" value="true" checked>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Insert" />
</td>
<td>
{% if messages %}
{% for mess in messages %}
<b style="color:green;">{{mess}}</b>
{% endfor %}
{% endif %}
</td>
</tr>
</thead>
</table>
<button class="btn btn-success">Go To Home</button>
</form>
model
class SUBCategories(models.Model):
category_name = models.ForeignKey(Categories, on_delete=models.CASCADE)
sub_categories_name = models.CharField(max_length=20)
sub_categories_description = models.CharField(max_length=20)
isactive = models.BooleanField(default=True)
def __str__(self):
return self.category_name.category_name
This error occurs because there is a chance in your function view, that it does not return proper Response or Redirect object. If you send POST request, but the form is not valid, then it will not return anything.
def insert_sub_categories(request):
if request.method == "POST":
...
if form.is_valid():
...
return redirect('sub_categories:show_sub_categories')
else:
print(form.errors)
# HERE YOU MISS REDIRECT/RENDER
else:
...
return render(request,'polls/insert_sub_categories.html',hm)
Also you don't pass SUBCategories.category_name to your serializer. It's required field, so it will never be valid without it. You may try form = SUBCategoriesSerializer(request.POST) instead of adding values one by one.
when I checked formset_factory validation and get fields to value from formset it returns only one field value from formset. But I can not find any problem in my code. please help me to solve the problem.
My form.py:
class SbTitleForm(forms.Form):
sb_title = forms.CharField(required=False)
sb_permission = forms.BooleanField(required=False)
SbTitleFormSet = formset_factory(SbTitleForm, extra=0)
My view.py:
def menuuser(request):
sbtitle = SbTitle.objects.all().values()
sbtitle_form = SbTitleFormSet(initial=sbtitle)
if request.method == 'POST':
sbtitle_form = SbTitleForm(request.POST, initial=sbtitle)
if sbtitle_form.is_valid():
print(sbtitle_form.cleaned_data)
context = {
'sbtitle_form':sbtitle_form,
}
return render(request, 'admins/user_role/user_permission.html', context)
My HTML:
{% extends 'base/base.html' %}
{% load static %}
{% block content %}
<div class="card">
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
{{ sbtitle_form.management_form }}
{{ sbitleelement_form.management_form }}
<div id="DataTables_Table_2_wrapper" class="dataTables_wrapper no-footer">
<div class="datatable-scroll">
<table class="table table-bordered table-hover datatable-highlight dataTable no-footer" id="DataTables_Table_2" role="grid" aria-describedby="DataTables_Table_2_info">
<thead>
<tr role="row" class="bg-teal-400">
<th class="sorting text-center h5" tabindex="0" aria-controls="DataTables_Table_2" rowspan="1" colspan="1" aria-label="Job Title: activate to sort column ascending">Sidebar Title</th>
</tr>
</thead>
<tbody>
{% for field in sbtitle_form %}
<tr role="row" class="odd">
<td class="sorting_1 text-center"><h4>{{ field.sb_title.value }} {{ field.sb_permission }}<p class="text-danger">{{ field.errors.as_text }}</p></h4></td>
{% endfor %}
</tbody>
</table>
</div>
<div class="card-footer">
<button class="btn btn-lg btn-primary" type="submit">Save</button>
</div>
</div>
</form>
</div>
{% endblock %}
when I try to print(sbtitle_form.cleaned_data) its return only one field value like as {'sb_title': '', 'sb_permission': False}. I can not get other field value in formset. please help me to solve the problem.
sbtitle_form = SbTitleForm(request.POST, initial=sbtitle)
shoud be:
sbtitle_form = SbTitleFormSet(request.POST, initial=sbtitle)
def menuuser(request):
sbtitle = SbTitle.objects.all().values()
sbtitle_form = SbTitleFormSet(initial=sbtitle)
if request.method == 'POST':
sbtitle_form = SbTitleFormSet(request.POST, initial=sbtitle) # <----
if sbtitle_form.is_valid():
print(form.cleaned_data)
context = {
'sbtitle_form':sbtitle_form,
}
return render(request, 'admins/user_role/user_permission.html', context)
If you want to access to each of forms:
def menuuser(request):
sbtitle = SbTitle.objects.all().values()
sbtitle_form = SbTitleFormSet(initial=sbtitle)
if request.method == 'POST':
sbtitle_form = SbTitleFormSet(request.POST, initial=sbtitle)
if sbtitle_form.is_valid():
for form in sbtitle_form.forms: # <----
print(form.cleaned_data)
context = {
'sbtitle_form':sbtitle_form,
}
return render(request, 'admins/user_role/user_permission.html', context)
I am creating a small application where I am able to add / modify / delete / view a member.
def member_edit(request,member_id):
MemberForm = modelform_factory(Member, fields=('employee_id', 'employee_name'))
if request.method == 'POST':
member = MemberForm(request.POST)
if member.is_valid():
member.save()
return HttpResponseRedirect(reverse("members:member_view"))
else:
member = get_object_or_404(Member, pk=member_id)
return render(request, 'members/member_edit.html', {'member': member})
I notice that instead of being able to edit all the fields become in display mode.
<form action="" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<table>
<tr>
<th> <label for="{{ member.employee_id.id_for_label }}">Employee id:</label> </th>
<td> <input type="text" value = {{ member.employee_id }} /> </td>
<td> {{ member.employee_id.errors }} </td>
</tr>
<tr>
<th> <label for="{{ member.employee_name.id_for_label }}">Employee Name:</label> </th>
<td> <input type="text" value = {{member.employee_name }} /></td>
<td> {{ member.employee_name.errors }} </td>
</tr>
</table>
<input type="submit" value="Update">
If I change the member_edit function to this, it works but I don't have any data to change(obviously) :
def member_edit(request,member_id):
MemberForm = modelform_factory(Member, fields=('employee_id', 'employee_name'))
if request.method == 'POST':
member = MemberForm(request.POST)
if member.is_valid():
member.save()
return HttpResponseRedirect(reverse("members:member_view"))
else:
member = MemberForm() # change made only here
return render(request, 'members/member_edit.html', {'member': member})
What am I missing here ?
I think you need to pass the member instance to the form.
from django.shortcuts import get_object_or_404
def member_edit(request, member_id):
MemberForm = modelform_factory(Member, fields=('employee_id', 'employee_name'))
if request.method == 'POST':
instance = get_object_or_404(Member, id=member_id)
member = MemberForm(request.POST, instance=instance)
if member.is_valid():
member.save()
return HttpResponseRedirect(reverse("members:member_view"))
else:
member = MemberForm() # change made only here
return render(request, 'members/member_edit.html', {'member': member})
I would like to add data filled into database and output it. But i have no idea where is wrong because my data was't saved into database at all. In views.py, Scholarship is just one scholarship object, LScholarship is displaying all the data in Scholarship. I have similar code for other models and views but i have no idea what i did wrong in here, making the data can't be saved into database. Could anyone please advice me where am i wrong
add_remove_scholarship.html
<div align="center" >
<form method="POST" onsubmit="return validation()" action="">
{% csrf_token %}
{{ form.errors }}
<p>Upload File: {{scholarship.doc}} <input id="doc" type="text" name="doc"> </p>
<p>Faculty: {{scholarship.faculty}} <input id="faculty" type="text" name="faculty"> </p>
<p>Opening date: {{scholarship.openDate}} <input id="odate" type="date" name="openDate"> </p>
<p>Closing date: {{scholarship.closeDate}} <input id="edate" type="text" name="closeDate"> </p>
<input type="submit" name="AddScholarship" value="Add Scholarship" >
</form>
</div>
<br></br>
<button id="button" type="button">Delete Selected Scholarship</button>
<br></br>
<form method="POST" action="">
{% csrf_token %}
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th> Scholarship </th>
<th> Faculty </th>
<th> Open Date </th>
<th> Close Date </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td>{{item.doc}}</td>
<td>{{item.faculty}}</td>
<td>{{item.openDate}}</td>
<td>{{item.closeDate}}</td>
</tr>
{% endfor %}
</table>
</form>
models.py
#consists of all the details of the scholarship under the 'Add/Remove
Scholarship'
class Scholarship(models.Model):
doc = models.TextField("Doc", max_length=1000)
faculty = models.TextField("Faculty", max_length=1000)
openDate = models.DateField("Opening Date", max_length=8)
closeDate = models.TextField("CLosing Date", max_length=18)
def __str__(self):
return self.doc
#consists of all the details of the scholarship under the 'Add/Remove
Scholarship'
class LScholarship(models.Model):
scholarship = models.ForeignKey(Scholarship, on_delete=models.CASCADE)
views.py
def scholarship(request):
if request.method == "POST":
form = ScholarshipForm(request.POST)
if form.is_valid():
scholarship = form.save(commit=False)
scholarship.save()
else:
form = ScholarshipForm()
return render(request, 'hrfinance/add_remove_scholarship.html', {'form': form})
def lscholarship(request):
query_results = Scholarship.objects.all()
data={'query_results':query_results}
return render(request, 'hrfinance/add_remove_scholarship.html', data)
class ScholarshipForm(forms.ModelForm):
class Meta:
model = Scholarship
fields = '__all__'
You are using two views to render a single template.
You can merge your views into a single one and maybe reduce the logic.
If I have understood your problem correctly you could use your view more like this,
def scholarship(request, id=None):
query_results = []
if request.method == "POST":
form = ScholarshipForm(request.POST)
if form.is_valid():
scholarship = form.save(commit=False)
scholarship.save()
else:
form = ScholarshipForm()
id = request.GET.get('scholarship')
query_results = Scholarship.objects.all()
data = {
'query_results':query_results,
'form': form,
}
return render(request, 'hrfinance/add_remove_scholarship.html', data)
In your template,
<div align="center" >
<form method="POST" onsubmit="return validation()" action="">
{% csrf_token %}
{{ form.errors }}
<p>Upload File: {{ form.doc }}</p>
<p>Faculty: {{ form.faculty }} </p>
<p>Opening date: {{ form.startDate }} </p>
<p>Closing date: {{ closeDate }} </p>
<button type="submit" name="AddUser" value="Add Scholarship" onclick="add()" >Add Scholarship</button>
</form>
</div>
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th> Scholarship </th>
<th> Faculty </th>
<th> Open Date </th>
<th> Close Date </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td>{{item.doc}}</td>
<td>{{item.faculty}}</td>
<td>{{item.openDate}}</td>
<td>{{item.closeDate}}</td>
</tr>
{% endfor %}
</table>
Timesheet.html is basically a form to fill in details. I have created a list_timesheet.html to list out all the timesheet being filled. But i have no idea when i run it only heading being showed like this :
{ % for item in query_results % } { % end for % }
Student ID Student Name Start Date End Date
list_timesheet.html
<form method="POST" class="display" cellspacing="0" width="100%">
{% csrf_token %}
<table>
<tr>
<th> Student ID </th>
<th> Student Name </th>
<th> Start Date </th>
<th> End Date </th>
</tr>
{ % for item in query_results % }
<tr>
<td>{{item.studentID}}</td>
<td>{{item.studentName}}</td>
<td>{{item.startDate}}</td>
<td>{{item.endDate}}</td>
</tr>
{ % end for % }
</table>
</form>
timesheet.html
<form method="POST" onsubmit="return validation()">
{% csrf_token %}
<div class="content-wrapper">
<div class="sub-content">
<div>
<p>Student ID: {{timesheet.studentID}}</p>
<input id="sid" type="field" name="studentid">
</div>
</div>
<div class="sub-content">
<div>
<p>Student Name: {{timesheet.studentName}}</p>
<input id="sname" type="field" name="studentname">
</div>
</div>
<div class="sub-content">
<div>
<p>Start Date: {{timesheet.startDate}}</p>
<input id="sdate" type="date" name="startdate">
</div>
</div>
<div class="sub-content">
<div>
<p>End Date: {{timesheet.endDate}}</p>
<input id="edate" type="date" name="enddate">
</div>
</div>
</div>
<div class="end-content">
<div class="center-align">
<div class="checklist">
<p>By checking this box I agree that I have satisfied all requirements to continue receiving my scholarship
allowance.</p>
<input id="agree" type="checkbox" name="checkbox" class="tick-att">
</div>
<br>
<div class="align-right">
<input type="submit" class="button" name="submit" value="submit" >
</div>
</div>
</div>
</form>
models.py
class Timesheet(models.Model):
studentID = models.CharField("Student ID", max_length=8, primary_key=True, default="")
studentName = models.CharField("Student Name", max_length=500, default="")
startDate = models.DateField("Start Date", max_length=8)
endDate = models.DateField("End Date", max_length=8)
def __str__(self):
return self.studentID
#consists of all the details of the timesheet under 'View Timesheets'
class LTimesheet(models.Model):
timesheet = models.ForeignKey(Timesheet, on_delete=models.CASCADE)
status = models.CharField("Status", max_length=100)
views.py
def timesheet(request):
if request.method == "POST":
if 'submit' in request.POST:
form = TimesheetForm(request.POST)
if form.is_valid():
timesheet = form.save(commit=False)
timesheet.save()
return HttpResponseRedirect(reverse('hrfinance/timesheet.html'))
#if the form is not valid, redirect the student to the same page
else:
form = TimesheetForm()
return render(request, 'hrfinance/timesheet.html', {'form': form})
else:
form = TimesheetForm()
return render(request, 'hrfinance/timesheet.html', {'form': form})
def ltimesheet(request):
query_results = LTimesheet.objects.all()
data={query_results:query_results}
return render(request, 'hrfinance/list_timesheet.html', data)
class TimesheetForm(forms.ModelForm):
class Meta:
model = Timesheet
fields = '__all__'
def timesheet(request):
if request.method == "POST":
if 'submit' in request.POST:
form = TimesheetForm(request.POST)
if form.is_valid():
timesheet = form.save(commit=False)
timesheet.save()
return HttpResponseRedirect(reverse('hrfinance/timesheet.html'))
#if the form is not valid, redirect the student to the same page
else:
form = TimesheetForm()
return render(request, 'hrfinance/timesheet.html', {'form': form})
else:
form = TimesheetForm()
return render(request, 'hrfinance/timesheet.html', {'form': form})
change your view to this
in your template after querying in the view.py
{% for item in query_results %}
<tr>
<td>{{item.studentName }}</td>
<td>{{item.put_field_you_have_in_model }}</td>
</tr>
{% endfor %}
and in list_timesheet.html
{% for item in query_results %}
<tr>
<td>{{item.studentID}}</td>
<td>{{item.studentName}}</td>
<td>{{item.startDate}}</td>
<td>{{item.endDate}}</td>
</tr>
{% endfor %}