我正在尝试从数据库中获取所有用户,以使用相关名称显示所有用户名,目标和目标ID,但是我似乎无法做到这一点。
模型
class GoalStatus(models.Model):
status_name = models.CharField(max_length=60)
def __str__(self):
return self.status_name
class ScrumyGoals(models.Model):
goal_status = models.ForeignKey(GoalStatus, on_delete=models.PROTECT, related_name='goalstatus')
user = models.ForeignKey(User, related_name='scrumygoals', on_delete=models.PROTECT)
goal_name = models.CharField(max_length=60)
goal_id = models.IntegerField()
created_by = models.CharField(max_length=60)
moved_by = models.CharField(max_length=60)
owner = models.CharField(max_length=60)
视图
def home(request):
user = User.objects.all()
# User1 = ScrumyGoals.objects.filter(goal_name='Learn Django')
context = {'User': User}
return render(request, 'django/home.html', context)
模板
{% for g in User.scrumygoals_set.all %}
<td>g.User.User</td>>
<td>g.goal_name</td>>
<td>g.goal_id</td>>
{% endfor %}
输出应该看起来像这样
User Weekly Goals Daily Goals Verify Goals Done Goals
username Goalname Goalid Goalname Goalid
答案 0 :(得分:1)
您不能以这种方式在模板中进行查询。试试这个:
views.py
WorkManager
django / home.html
// 1. The app is performing an auto-backup. Prior to O, JobScheduler could erroneously
// try to send commands to JobService in this state (b/32180780). Since neither
// Application#onCreate nor ContentProviders have run,...
或者,如果您想循环浏览Application
个实例,请执行以下操作:
views.py
onCreate
django / home.html
def home(request):
users = User.objects.all()
context = {'users': users}
return render(request, 'django/home.html', context)