[英]How to call a function from models.py with class based views in Django?
[英]How to call a function from models.py to views or template
我是 Django 和 Python 的初学者。 我正在创建一个工资单项目,其计算是使用 models.py 中的 function 完成的。 用户必须通过模板输入变量并保存到sql。 然后搜索员工(再次通过模板)和 output 他的工资单详细信息。 那是我想使用计算function的时候。
来自数据库的数据正在工作并由模板输出。 至于计算出来的数据,根本就没有显示出来。
我一直在尝试使用 function 无济于事,我已经搜索了 3 天。 我不知道现在该做什么。
模型.py
from django.db import models
#Class in model.py acts as a table in database
class Add_Employee(models.Model):
name = models.CharField (max_length = 150, default = '', null = False)
position = models.CharField (max_length = 150, default = '', null = False)
email = models.EmailField (max_length = 150, default = '', null = False)
address = models.CharField (max_length = 500, default = '', null = False)
basic_pay = models.FloatField(default=None)
overtime_hours = models.IntegerField(default=None)
allowance = models.FloatField(default=None)
days_leave = models.IntegerField(default=None)
other_deductions = models.FloatField(default=None)
#Django admin page; the table will show the name
def __str__(self):
return '{}{}'.format(self.name, self.position, self.email, self.address)
def salary_calculation(self):
#Earnings
self.overtime_hours_pay = self.overtime_hours * 64
self.gross_income = self.basic_pay + self.overtime_hours + self.allowance
#Deductions
self.days_leave = self.days_leave * 512
self.pagibig = self.basic_pay * 0.01
self.gsis = self.basic_pay * 0.09
self.withholdingtax = self.basic_pay * 0.15
self.philhealth = self.basic_pay * 0.0275 / 2
self.total_deductions = self.days_leave + self.pagibig + self.gsis + self.withholdingtax + self.philhealth
#Net Pay
self.net_pay = self.gross_income - self.total_deductions
print ("Calculated.") #this was never outputted from all the times i tried.
return (self)
视图.py
#Code for form submission and redirect back to employer page again
def add_employee_form_submit(request):
print ("Form is successfully submitted.") #print can only be seen on the terminal, not the browser
#create local variable for each variable entered using the form
name = request.POST["name"]
address = request.POST["address"]
email = request.POST["email"]
position = request.POST["position"]
basic_pay = request.POST["basic_pay"]
overtime_hours = request.POST["overtime_hours"]
allowance = request.POST["allowance"]
days_leave = request.POST["days_leave"]
other_deductions = request.POST["other_deductions"]
#assigning the local variable into the database fields
employee_info = Add_Employee(name = name, address = address, email = email, position = position, basic_pay = basic_pay, overtime_hours = overtime_hours, allowance = allowance, days_leave = days_leave, other_deductions = other_deductions )
#save the entire stuff
employee_info.save()
return render(request, 'employer.html')
def search_employee_form_submit(request):
if request.method == "POST":
search_id = request.POST['search_id']
if search_id:
employee_match = Add_Employee.objects.filter( pk = search_id ) #pk is primary key
if employee_match:
return render (request, 'employee.html', {'Search': employee_match})
else:
messages.error(request, 'No results found.')
else:
return HttpResponseRedirect('employee/')
return render(request, 'employee.html')
employee.html(查询和结果查看模板)
<h1>Employee Payroll</h1>
<br><br>
<h2>Enter your ID:</h2>
<form action="/search_employee_form_submit/", method="post"> <!--it will use 'search_employee_form_submit' url after submitting form-->
{% csrf_token %} <!-- added for privacy reasons/security-->
<br>
<input type = "text" name="search_id" placeholder="Enter employee id">
<br><br>
<input type="submit" value="search employee">
</form>
<br><br>
<!-- Code for when your search has results / employee -->
{% if Search %}
{% for k in Search %}
Name:
{{ k.name }} <br>
Position:
{{ k.position }} <br>
Email:
{{ k.email }} <br>
..... etc (this part is working)
Overtime Hour Pay:
{{ k.overtime_hours_pay }}<br>
Gross Income:
{{ k.gross_income }}<br><br>
..... etc (calculated data = doesnt show result)
{% endfor %}
请原谅我的长篇文章,因为我不知道我在哪里遗漏了一些东西。
首先,您引用了 model 中不存在的字段,例如overtime_hours_pay
和gross-income
。 如果您在 model 实例上执行self.someFieldName
,则应在 model 中定义或继承该字段。 所以这是错误的:
self.overtime_hours_pay = self.overtime_hours * 64
你也可以
self
部分并使它们成为普通变量。 看起来像:def salary_calculation(self):
overtime_hours_pay = self.overtime_hours * 64
gross_income = self.basic_pay + self.overtime_hours + self.allowance
self.days_leave = self.days_leave * 512
pagibig = self.basic_pay * 0.01
gsis = self.basic_pay * 0.09
withholdingtax = self.basic_pay * 0.15
philhealth = self.basic_pay * 0.0275 / 2
total_deductions = self.days_leave + pagibig + gsis + withholdingtax + philhealth
net_pay = gross_income - total_deductions
return net_pay
之后,您可以在视图中计算 net_pay。
...
if employee_match:
# calculation
net_pay = employee_match.salary_calculation()
# then pass this to the template
return render (request, 'employee.html', {'Search': employee_match, 'net_pay': net_pay})
else:
messages.error(request, 'No results found.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.