'One field in Django cannot retrieve the correct value when showing in template
models.py
class Order(models.Model):
PAYMENT_OPTIONS = (
('VISA','VISA'),
('Master','Master'),
('Octopus','Octopus'),
('Cash','Cash'),
)
STATUS = (
('Pending','Pending'),
('Delivered','Delivered'),
('Collected','Collected'),
)
METHODS = (
('外賣自取','外賣自取'),
('送遞','送遞'),
)
user = models.ForeignKey(User,models.CASCADE,null=True,blank=True)
customer = models.CharField(max_length=200,null=True,blank=True)
card_id = models.IntegerField(null=True,blank=True)
mobile = models.IntegerField(null=True,blank=True)
email = models.CharField(max_length=200,null=True,blank=True)
total_price = models.DecimalField(decimal_places=2,max_digits=7)
payment_method = models.CharField(max_length=50,choices=PAYMENT_OPTIONS,null=True,blank=True)
status = models.CharField(max_length=50,choices=STATUS,default='Pending')
take_method = models.CharField(max_length=50,choices=METHODS,null=True,blank=True)
points_earned = models.IntegerField(default=0)
date = models.DateTimeField(auto_now_add=True)
address = models.CharField(max_length=350,null=True,blank=True)
def __str__(self):
return str(self.customer)+'\'s Order'
views.py
def checkout(request):
global cartlist
cartlist1 = cartlist
total = 0
for unit in cartlist:
total += int(unit[3])
grandtotal = total + 100
earnings = int(grandtotal/5)
print(grandtotal)
if request.method == 'POST':
print(type(request.POST.get('customerPayment')))
print(request.POST.get('customerPayment'))
print(request.POST.get('customerTakeaway'))
customerName = request.POST.get('customerName','')
customerID = request.POST.get('customerID','')
customerMobile = request.POST.get('customerMobile','')
customerEmail = request.POST.get('customerEmail','')
customerPayment = request.POST.get('customerPayment'),
customerTakeaway = request.POST.get('customerTakeaway'),
customerAddress = request.POST.get('customerAddress','')
new_order = Order.objects.create(
user = User.objects.get(username=request.user),
customer = customerName,
card_id = customerID,
mobile = customerMobile,
email = customerEmail,
total_price = grandtotal,
payment_method = customerPayment,
take_method = customerTakeaway,
points_earned = earnings,
address = customerAddress)
account = User.objects.get(username=request.user)
checkout.html
<tr>
<td style="width:160px;text-align:center;">
<strong>付款方式</strong>
</td>
<td style="width:200px;text-align:center;">
<input type="radio" name="customerPayment" value="VISA" id="customerPayment">VISA
<input type="radio" name="customerPayment" value="Master" id="customerPayment">Master<br>
<input type="radio" name="customerPayment" value="Octopus" id="customerPayment">Octopus
<input type="radio" name="customerPayment" value="Cash" id="customerPayment">Cash
</td>
</tr>
<tr>
<td style="width:160px;text-align:center;">
<strong>領取方式</strong>
</td>
<td style="width:200px;text-align:center;">
<input type="radio" name="customerTakeaway" value="外賣自取" id="customerTakeaway">外賣自取
<input type="radio" name="customerTakeaway" value="送遞" id="customerTakeaway">送遞
</td>
</tr>
order_summary.html
<tr>
<td style="width:160px;text-align:center;">
<strong>付款方式</strong>
</td>
<td style="width:200px;text-align:center;">
{{ customer_order.payment_method }}
</td>
</tr>
<tr>
<td style="width:160px;text-align:center;">
<strong>領取方式</strong>
</td>
<td style="width:200px;text-align:center;">
{{ customer_order.take_method }}
</td>
</tr>
When I try to submit the form in the checkout method, since I have the Order model having the CharFields with choices and I used request.POST.get(field_name) that is the radiobuttons from the HTML. After I submitted the form, the {{ customer_order.payment_method }} shows ('VISA',) instead of the correct value VISA and the database record could not update that record. Can anyone help me to find out the problem?
Solution 1:[1]
You aren't using any of Django's built-in form validation, or even a form on your checkout page (unless it was cut off in the snippet you pasted).
Typically you would have a <form method="post"></form> with a csrf token and the inputs as child elements at minimum.
See Part 4 of the official Django tutorial for an example of this.
Also, your radio inputs should be labeled properly:
<input type="radio" name="customerPayment" value="VISA" id="customerPayment">
<label for="customerPayment">VISA</label>
That may be impacting how the post data is sent to the server.
Alternatively you can use Django forms themselves to build your form with Python code. This adds the built-in validation I mentioned earlier. It also offers the ChoiceField, which is built specifically for model choices and handles the validation for you.
Then instantiate the unbound form and pass it into your view as context if the request method is get.
And in your view if the method is post, check if the form is valid with form.is_valid() and optional additional cleaning of the values with form.cleaned_data.
I recommend reading further on the basics of Django forms in the documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wjh18 |
