如何显示来自模型方法的数据
How to display data from a model method
大家晚上好
我是编程新手,我一直在做一个 Django 项目来提高我的技能。
我想在我的模板中显示来自方法模型的数据。
这是项目的模型,
class Company(models.Model):
#Company data
company_name = models.CharField(max_length=100)
outstanding_shares = models.IntegerField()
share_price = models.DecimalField(max_digits= 5, decimal_places=2)
revenue = models.IntegerField()
expenses = models.IntegerField()
total_assets = models.IntegerField()
total_liabilities = models.IntegerField()
current_assets = models.IntegerField()
current_liabilities = models.IntegerField()
operating_cashflows = models.IntegerField()
capex = models.IntegerField()
#Date of creation
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now= True)
def __str__(self):
return self.company_name
#Company methods
def net_income(self):
return self.revenue - self.expenses
这是视图文件,
def tools(request):
submitted = False
form = CompanyForm()
if request.method == 'POST':
print(request.POST)
form = CompanyForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/simple_investing/tools?submitted=True')
else:
form = CompanyForm
if 'submitted' in request.GET:
submitted = True
context = {'form': form, 'submitted': submitted}
return render(request, 'simple_investing/tools.htm', context)
这是表格文件,
class CompanyForm(ModelForm):
class Meta:
model = Company
fields = ('company_name', 'outstanding_shares', 'share_price', 'revenue', 'expenses', 'total_assets','total_liabilities', 'current_assets','current_liabilities', 'operating_cashflows', 'capex')
widgets = {
'company_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'Company name'}),
'outstanding_shares': forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Shares outstanding'}),
'share_price' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Share price'}),
'revenue' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Revenue'}),
'expenses' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Expenses'}),
'total_assets' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Total Assets'}),
'total_liabilities' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Total Liabilities'}),
'current_assets' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Current Assets'}),
'current_liabilities' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Current Liabilities'}),
'operating_cashflows' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Operating Cashflows'}),
'capex' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Capital Expenditure'}),
}
这是模板文件,
<body>
{% if submitted %}
Your company was submitted successfully. Hereunder is your company's net income:
{{% Company.net_income %}}
{% else %}
<p>In this section, we will analyze the fundamentals of a company of your choice. The goal is to assess if the <strong><i>stock market</i></strong> is offering us an investment opportunity. </p>
<p>Let's start by entering the data of the company that you are interested in: </p>
<div class="row g-3">
<form action ="" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-success">Submit</button>
</form>
我收到以下错误:
Could not parse the remainder: '% Company.net_income %' from '% Company.net_income %'
关于如何解决这个问题有什么想法吗?
在模板文件中
- 删除:
{{% Company.net_income %}}
- 试试这个:
{{ Company.net_income }}
大家晚上好
我是编程新手,我一直在做一个 Django 项目来提高我的技能。
我想在我的模板中显示来自方法模型的数据。
这是项目的模型,
class Company(models.Model):
#Company data
company_name = models.CharField(max_length=100)
outstanding_shares = models.IntegerField()
share_price = models.DecimalField(max_digits= 5, decimal_places=2)
revenue = models.IntegerField()
expenses = models.IntegerField()
total_assets = models.IntegerField()
total_liabilities = models.IntegerField()
current_assets = models.IntegerField()
current_liabilities = models.IntegerField()
operating_cashflows = models.IntegerField()
capex = models.IntegerField()
#Date of creation
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now= True)
def __str__(self):
return self.company_name
#Company methods
def net_income(self):
return self.revenue - self.expenses
这是视图文件,
def tools(request):
submitted = False
form = CompanyForm()
if request.method == 'POST':
print(request.POST)
form = CompanyForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/simple_investing/tools?submitted=True')
else:
form = CompanyForm
if 'submitted' in request.GET:
submitted = True
context = {'form': form, 'submitted': submitted}
return render(request, 'simple_investing/tools.htm', context)
这是表格文件,
class CompanyForm(ModelForm):
class Meta:
model = Company
fields = ('company_name', 'outstanding_shares', 'share_price', 'revenue', 'expenses', 'total_assets','total_liabilities', 'current_assets','current_liabilities', 'operating_cashflows', 'capex')
widgets = {
'company_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder' : 'Company name'}),
'outstanding_shares': forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Shares outstanding'}),
'share_price' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Share price'}),
'revenue' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Revenue'}),
'expenses' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Expenses'}),
'total_assets' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Total Assets'}),
'total_liabilities' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Total Liabilities'}),
'current_assets' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Current Assets'}),
'current_liabilities' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Current Liabilities'}),
'operating_cashflows' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Operating Cashflows'}),
'capex' :forms.NumberInput(attrs={'class': 'form-control', 'placeholder' : 'Capital Expenditure'}),
}
这是模板文件,
<body>
{% if submitted %}
Your company was submitted successfully. Hereunder is your company's net income:
{{% Company.net_income %}}
{% else %}
<p>In this section, we will analyze the fundamentals of a company of your choice. The goal is to assess if the <strong><i>stock market</i></strong> is offering us an investment opportunity. </p>
<p>Let's start by entering the data of the company that you are interested in: </p>
<div class="row g-3">
<form action ="" method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-success">Submit</button>
</form>
我收到以下错误:
Could not parse the remainder: '% Company.net_income %' from '% Company.net_income %'
关于如何解决这个问题有什么想法吗?
在模板文件中
- 删除:
{{% Company.net_income %}}
- 试试这个:
{{ Company.net_income }}