Django fields editable in create but not in edit - python

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})

Related

Django error value error: The view sub_categories.views.insert_sub_categories didn't return an HttpResponse object. It returned None instead

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.

Django form post not saving

I am making a Django application (for the first time in my life). As part of the application, a timetable needs to be implemented. Loading data (from the databse) into the timetable works fine:
Timetable view
The thing is that the data should be editable. So users need to be able to change the time or the yes/no parameter. I've created a POST handler in views.py, but when I press save, the page quickly reloads and the old values are back. What am I doing wrong?
models.py
class timeTable(models.Model):
key = models.CharField(max_length=200, unique=True, null=True)
value = models.CharField(max_length=200, null=True)
def __str__(self):
return self.key
views.py
#login_required(login_url='login')
def timetable(request):
timeTableFormset = modelformset_factory(timeTable, fields='__all__' ,extra=0)
timetableform = timeTableFormset(queryset=timeTable.objects.all())
if request.method == 'POST':
form = timeTableFormset(request.POST)
if form.is_valid():
form.save()
return render(request, 'VulnManager/timetable.html', {'timetableform': timetableform})
timetable.html:
<form method="POST">
{% csrf_token %}
<table id="tablePreview" class="table table-borderless table-hover">
<!--Table head-->
<thead>
<tr>
<th></th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
</thead>
<!--Table head-->
<!--Table body-->
<tbody>
<tr>
<th scope="row">Scan?</th>
{{ timetableform.management_form }}
{% for timeTableValue in timetableform.forms %}
{% if forloop.counter <= 7 %}
<td><select class="form-control" id="{{ timeTableValue.key.value }}" disabled="true">
<option>{{ timeTableValue.value.value }}</option>
<option>
{% if timeTableValue.value.value == "true" %}
false
{% elif timeTableValue.value.value == "false" %}
true
{% endif %}
</option>
</select></td>
{% if forloop.counter == 7 %}
</tr>
<tr>
<th scope="row">Time begin</th>
{% endif %}
{% elif forloop.counter >= 8 and forloop.counter <= 14 %}
<td><input type="text" id="{{ timeTableValue.key.value }}" onchange="validateHhMm(this);" value="{{ timeTableValue.value.value }}" readonly /></td>
{% if forloop.counter == 14 %}
</tr>
<tr>
<th scope="row">Time end</th>
{% endif %}
{% elif forloop.counter >= 15 and forloop.counter <= 21 %}
<td><input type="text" id="{{ timeTableValue.key.value }}" onchange="validateHhMm(this);" value="{{ timeTableValue.value.value }}" readonly /></td>
{% endif %}
{% endfor %}
</tr>
</tbody>
<!--Table body-->
</table>
<input type="button" class="btn-primary" id="edit" value="Edit" />
<input type="submit" class="btn-primary" id="save" value="Save" />
</form>
Your "form" variable has the formset in it, but you have to loop through the forms in the formset individually:
if form.is_valid():
for form_line in form:
form_line.save()
Your view is always rendering the "get" form for lack of a better term.
I think if you keep your variable names the same, it'll make things a bit simpler and consistent. So to explain this;
views.py
#login_required(login_url='login')
def timetable(request):
timeTableFormset = modelformset_factory(timeTable, fields='__all__' ,extra=0)
timetableform = timeTableFormset(queryset=timeTable.objects.all())
if request.method == 'POST':
form = timeTableFormset(request.POST) # This form just gets left in this block
if form.is_valid():
form.save()
return render(
request,
'VulnManager/timetable.html',
{'timetableform': timetableform} # The context includes the fresh new form, with no post data
)
What you should do, is just call it formset, because it makes life easier;
from django.contrib import messages
#login_required(login_url='login')
def timetable(request):
timeTableFormset = modelformset_factory(timeTable, fields='__all__' ,extra=0)
formset = timeTableFormset(queryset=timeTable.objects.all())
if request.method == 'POST':
formset = timeTableFormset(data=request.POST, queryset=timeTable.objects.all())
if formset.is_valid():
formset.save()
# Do something here to reassure the user it saved. Redirect or add a message
messages.success(request, 'Form saved.')
return render(
request,
'VulnManager/timetable.html',
{'timetableform': formset}
)
There's an article here which may provide some worthy reading for you; https://medium.com/#MicroPyramid/understanding-djangos-model-fromsets-in-detail-and-their-advanced-usage-131dfe66853d

why do I get a single field value in Django formset_factory?

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)

Getting KeyError in request.session

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')

can't add values from field into database Django

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>

Categories

Resources