我正在尝试打印“任务”类中的字段,但我尝试执行的操作似乎无效。在html模板中,我尝试打印'assignment_title'。这是我所有的文件,可帮助显示我在哪里
urls.py
url(r'^feedback/$', views.assignment, name='assignments')
models.py
class Assignment(models.Model):
assignment_title = models.CharField(max_length=256, default='')
lecturer = models.ForeignKey(User)
views.py
# Assignment view
def assignment(request):
args = {'assignment': Assignment}
return render(request, 'mainfocus/feedback.html', args)
html
<div class="container">
<h1>Feedback</h1>
<p>{{ obj.assignment_title }}</p>
</div>
只需确认一下,我也已经在'admin.py'中注册了该类。任何帮助将不胜感激,我也已迁移并填充了表格,因此也不应该成为问题
答案 0 :(得分:1)
答案!
views.py
Month year sales P/L streak 12_max
0 January 2017 34400 False 0 NaN
1 February 2017 35530 True 0 NaN
2 March 2017 34920 False 0 NaN
3 April 2017 35950 True 0 NaN
4 May 2017 36230 True 1 NaN
5 June 2017 36820 True 2 NaN
6 July 2017 34590 False 0 NaN
7 August 2017 36500 True 0 NaN
8 September 2017 36600 True 1 NaN
9 October 2017 37140 True 2 NaN
10 November 2017 36790 False 0 NaN
11 December 2017 43500 True 0 43500.0
12 January 2018 34900 False 0 43500.0
13 February 2018 37700 True 0 43500.0
14 March 2018 37900 True 1 43500.0
15 April 2018 38100 True 2 43500.0
16 May 2018 37800 False 0 43500.0
17 June 2018 38500 True 0 43500.0
18 July 2018 39400 True 1 43500.0
19 August 2018 39700 True 2 43500.0
20 September 2018 39980 True 3 43500.0
21 October 2018 40600 True 4 43500.0
22 November 2018 39100 False 0 43500.0
23 December 2018 46600 True 0 46600.0
24 January 2019 42500 False 0 46600.0
models.py
# Assignment view
def assignment(request):
assignments = Assignment.objects.all()
args = {
'assignments': assignments
}
return render(request, 'mainfocus/feedback.html', args)
html
class Assignment(models.Model):
title = models.CharField(max_length=256, default='')
lecturer = models.ForeignKey(User)
答案 1 :(得分:0)
obj
中的变量views.py
包含数据库中所有的Assignment
。如果您只想打印一份作业,则应过滤它们:
# Assignment view
def assignment(request):
obj = Assignment.objects.first() # get the first assignment for example
return render(request, 'mainfocus/feedback.html',{'obj': obj})
如果要打印所有这些,则应修改模板以使用for loop:
<div class="container">
<h1>Feedback</h1>
{% for assignment in obj %}
<p>{{ assignment.assignment_title }}</p>
{% endfor %}
</div>