Я не использую формы в своем решении, а только пытаюсь получить значения из request.POST, чтобы сохранить его в базе данных в Views.py.
Раньше это работало, но, как ни странно, когда я печатал ключ, пару значений объекта запроса, он иногда отображал значение, а иногда - None.
HTML:
<form id="modify" class="form-horizontal" action="{% url 'modify' customer.customerid %}" method="POST">
{% csrf_token %}
<fieldset class="scheduler-border">
<legend class="scheduler-border">{{ customer.customername }}</legend>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-building bigicon"></i><label for="txtCustomerName">Customer Name</label></span>
<div class="col-md-11">
<input type="text" name="txtCustomerName" id ="txtCustomerName" class="form-control" placeholder="Customer Name" value="{{ customer.customername }}" disabled="True">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-building bigicon"></i><label for="txtCustShortName">Customer Short Name</label></span>
<div class="col-md-11">
<input type="text" name="txtCustShortName" id="txtCustShortName" class="form-control" placeholder="Customer Short Name" value="{{ customer.customershortname }}" disabled="True">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-user bigicon"></i><label for="txtContactFirstName">Contact Person First Name</label></span>
<div class="col-md-12">
<input type="text" name="txtContactFirstName" id="txtContactFirstName" placeholder="Contact Person First Name" class="form-control" value="{{ contact.firstname }}" disabled="True">
</div>
</div>
<div class="form-group col-md-1 text-left">
</div>
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-user bigicon"></i><label for="txtContactLastName">Contact Person Last Name</label></span>
<div class="col-md-12">
<input type="text" name="txtContactLastName" id="txtContactLastName" placeholder="Contact Person Last Name" class="form-control" value="{{ contact.lastname }}" disabled="True">
</div>
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-envelope bigicon"></i><label for="txtCustomerEmail">Email</label></span>
<div class="col-md-11">
<input type="text" name="txtCustomerEmail" id="txtCustomerEmail" placeholder="Contact Email Address" class="form-control" value="{{ contact.contactemail }}" disabled="True">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-flag-checkered bigicon"></i><label for="selCustCountry">Country</label></span>
<div class="col-md-12">
<select name="selCustCountry" id="selCustCountry" class="form-control" disabled="True">
{% for c in country %}
<option value= "{{ c.countryshortname }}" {% if c.countryshortname in customer.country %}selected{% endif %}>{{ c.countryname }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group col-md-1 text-left">
</div>
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-flag bigicon"></i><label for="selCustState">State</label></span>
<div class="col-md-12">
<select name="selCustState" id="selCustState" class="form-control" disabled="True">
{% for l in location %}
<option value= "{{ l.locationshortname }}" {% if l.locationshortname in customer.state %}selected{% endif %}>{{ l.locationname }}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-globe bigicon"></i><label for="selCustTimeZone">Time Zone</label></span>
<div class="col-md-11">
<select name="selCustTimeZone" id="selCustTimeZone" class="form-control" disabled="True">
{% for l in location %}
<option value= "{{ l.timezone }}" {% if l.timezone in customer.timezone %}selected{% endif %}>{{ l.timezone }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-calendar bigicon"></i><label for="dtMSAStartDate">MSA Start Date</label></span>
<div class="col-md-12">
<input type="date" name="dtMSAStartDate" id="dtMSAStartDate" class="form-control" value= "{{ customer.msastartdate|date:'Y-m-d' }}" disabled="True">
</div>
</div>
<div class="form-group col-md-1 text-left">
</div>
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-calendar bigicon"></i><label for="dtMSAEndDate">MSA End Date</label></span>
<div class="col-md-12">
<input type="date" name="dtMSAEndDate" id="dtMSAEndDate" class="form-control" value="{{ customer.msaenddate|date:'Y-m-d' }}" disabled="True">
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-check-square bigicon"></i><label class="form-check-label" for="chkExternalCustomer">External Customer</label>
<span class="custom-control-indicator">        </span>
<input type="checkbox" name="chkExternalCustomer" id="chkExternalCustomer" class="form-check-input lg" checked="{{ customer.externalcustomer }}" disabled="True">
</span>
</div>
<div class="form-group col-md-1 text-left">
</div>
<div class="form-group col-md-5 col-md-offset-2 text-left">
<span class="col-md-1"><i class="fa fa-check-square bigicon"></i><label class="form-check-label" for="chkAllDSReq">All Data Source</label>
<span class="custom-control-indicator">        </span>
<input type="checkbox" name="chkAllDSReq" id="chkAllDSReq" class="form-check-input lg" checked="{{ customer.alldatasourcerequired }}" disabled="True">
</span>
</div>
</div>
<div class="form-group">
<div class="col-md-8">
<span>        </span>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-1 text-center">
<a href="javascript:{document.getElementById('modify').submit()}"><button class="btn btn-primary" type="button" name="btnSave" id="btnSave">
<span class="fa fa-save smallicon"> </span>Save</button></a>
</div>
<div class="form-group col-md-2 text-center">
<button class="btn btn-primary" type="button" name="btnCancel" id="btnCancel">
<span><i class="fa fa-undo smallicon"></i> </span>Cancel</button>
<button class="btn btn-primary" type="button" name="btnEdit" id="btnEdit">
<span><i class="fa fa-edit smallicon"></i> </span>Edit Customer Details</button>
</div>
<div class="form-group col-md-2 text-center">
</div>
<div class="form-group col-md-2 text-center">
</div
<div class="form-group col-md-2 text-center">
<a href="javascript:{document.getElementById('edit').submit()}"><button class="btn btn-primary" type="button" name="btnView" id="btnView">
<span><i class="fa fa-eye smallicon"></i> </span>View Service Details</button></a>
</div>
<div class="form-group col-md-1 text-center">
</div>
</div>
</fieldset>
</form>
Views.py - в конце концов я попытаюсь получить все поля формы, но для отладки я пытаюсь получить только одно поле формы.
def modify(request, customer_customerid):
customer = get_object_or_404(Customer, pk=customer_customerid)
if request.method =='POST':
customer.CustomerShortName = request.POST.get('txtCustShortName','')
customer.save()
return redirect('/customers/' + str(customer_customerid))
Model.py
class Customer(models.Model):
customerid = models.AutoField(db_column='CustomerID', primary_key=True) # Field name made lowercase.
customername = models.CharField(db_column='CustomerName', max_length=50) # Field name made lowercase.
customershortname = models.CharField(db_column='CustomerShortName', max_length=50) # Field name made lowercase.
msastartdate = models.DateField(db_column='MSAStartDate') # Field name made lowercase.
msaenddate = models.DateField(db_column='MSAEndDate') # Field name made lowercase.
externalcustomer = models.BooleanField(db_column='ExternalCustomer') # Field name made lowercase.
state = models.CharField(db_column='State', max_length=50) # Field name made lowercase.
country = models.CharField(db_column='Country', max_length=50) # Field name made lowercase.
alldatasourcerequired = models.BooleanField(db_column='AllDataSourceRequired') # Field name made lowercase.
timezone = models.CharField(db_column='TimeZone', max_length=50) # Field name made lowercase.
lastmodifiedutc = models.DateTimeField(db_column='LastModifiedUTC') # Field name made lowercase.
lastmodifiedby = models.CharField(db_column='LastModifiedBy', max_length=50) # Field name made lowercase.
active = models.BooleanField(db_column='Active') # Field name made lowercase.
class Meta:
managed = False
db_table = 'Customer'
Обновлен вид, который отображает форму:
def detail(request, customer_customerid):
customer = get_object_or_404(Customer, pk=customer_customerid)
contact = Contact.objects.get(reftable='Customer',
refid=customer_customerid)
country = Country.objects.all()
location = LocationTimezoneMapping.objects.all()
context = {'customer':customer,'contact':contact, 'country': country, 'location':location}
return render(request, 'customers/detail.html', context)