【问题标题】:Request.POST or Request.POST.get both not providing values in views.pyRequest.POST 或 Request.POST.get 都没有在 views.py 中提供值
【发布时间】:2020-02-06 06:30:42
【问题描述】:

我没有在我的解决方案中使用表单,只是尝试从 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">&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 - 我最终会尝试获取所有表单域,但为了调试,我只尝试获取一个表单域。

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

模型.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)

【问题讨论】:

  • 你能分享你用来点击视图的确切网址吗?
  • URL : path('modify/', views.modify, name='modify'),它实际上在我能够打印的情况下击中了视图定义(“调试”)从 def modify 内部。
  • 如何在你的 url 中传递这个 'txtCustShortName'?希望是这样 - /modify/2?txtCustShortName='hi'
  • No..form Method 是 POST 而不是 GET。所以这些值不会作为 URL 中的 queryStrings 传递
  • 你能显示呈现表单的视图吗?

标签: python django python-3.x django-views


【解决方案1】:

我需要更新 html 表单中的 URL,方法是在 URL 后面加上“修改”,这样着陆 URL 和单击保存按钮后的 URL 就会有所不同。

这是必要的,因为我的视图和编辑页面在同一个 html 上。

html中的jquery

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

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

URL.py

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

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多