【发布时间】:2020-04-29 02:48:27
【问题描述】:
所以我正在尝试使用 HTML 表单创建一个 Django 站点。该表单接受供应商选择和数量,然后需要将其传递到视图中。需要更新员工表的余额,从当前登录用户的余额中扣除支付的金额,并且需要在交易表中添加一个条目,说明详细信息,信用 = 0,其余信息取自表单。如何做到这一点?
以下是我的文件: html:
<form method="POST" action="/profiles/updatingBalance">
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" value="1" id="defaultUnchecked" name="defaultRadios">
<label class="custom-control-label" for="defaultUnchecked">Vendor 1</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" value="2" id="defaultUnchecked" name="defaultRadios">
<label class="custom-control-label" for="defaultUnchecked">Vendor 2</label>
</div>
<input type="" class="form-control" id="amount1" name="amt" aria-describedby="emailHelp" placeholder="Enter amount">
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
模型.py:
from django.db import models
from django.contrib.auth.models import User
import django
import datetime
# Create your models here.
class vendor(models.Model):
id = models.CharField(max_length=20, on_delete=models.CASCADE, primary_key=True)
name = models.CharField(maxlength=30, on_delete=models.CASCADE)
class employee(models.Model):
name = models.OneToOneField(User, on_delete=models.CASCADE)
id = models.CharField(max_length=20, on_delete=models.CASCADE, primary_key=True)
balance = models.IntegerField(default=0)
class transaction(models.Model):
vendor_id = models.ForeignKey(vendor, on_delete=models.CASCADE)
emp_id = models.ForeignKey(employee, on_delete=models.CASCADE)
debit = models.IntegerField()
credit = models.IntegerField()
timestamp = models.DateField(_("Date"), default=datetime.date.today)
这是我试过的views.py。我一直在更新员工表的余额(不确定是否正确):
def updatingBalance(request):
if request.method=="POST":
ven_id = request.POST["defaultRadios"]
amount = request.POST["amt"]
x = employee.objects.filter(id = request.User.id)
x.balance = x.balance - amount
p = transaction(vendor_id =ven_id.value, emp_id = request.User.id, debit=amount, credit=0)
p.save()
return render(request, 'profiles/userLogin.html', employee)
return HttpResponseRedirect(request.META.get('profiles/userLogin.html'))
我很困惑如何从纯 html 表单中获取数据(我是初学者),以及如何使用这些信息来获得我想要的结果。任何帮助将不胜感激。
【问题讨论】:
标签: html django forms django-models django-views