简体   繁体   中英

Request.POST or Request.POST.get both not providing values in views.py

I am not using Forms in my solution and only trying to get the values from the request.POST so as to save it in the database in the Views.py.

It was working before but strangely when I print the key, value pair of the request object it sometimes shows the value and sometime has None in it.

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">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;</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">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;</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>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;</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">&nbsp;</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>&nbsp;</span>Cancel</button>
                          <button class="btn btn-primary" type="button" name="btnEdit" id="btnEdit">
                            <span><i class="fa fa-edit smallicon"></i>&nbsp;</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>&nbsp;</span>View Service Details</button></a>
                        </div>
                        <div class="form-group col-md-1 text-center">
                        </div>
                    </div>
                </fieldset>
            </form>

Views.py - I will eventually try to get all the form fields but for the sake of debugging I am trying to get one form field only.

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'

Updated with the View which renders the form:

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)

I needed to update the URL in the html form by appending the URL with "modify" so that there is a difference in the landing URL and the URL after clicking the Save button.

This is necessary as the my view and edit pages were on the same html.

jquery in html

      $("#btnSave").click(function(){

        $(location).attr("href").append("modify")
        $("#frmDetails").submit();
     });

URL.py

path('modify/<int:customer_customerid>/', views.modify, name='modify'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM