此代码可以正常工作,但不会在模板中显示“ {{skill.skillName}}
”
那是什么问题呢?
这是结果时,我尝试打印skills
从视图
<QuerySet [<Skill: nope>, <Skill: yep>, <Skill: yes>]>
models.py
class User_Model(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
user = models.OneToOneField(User, on_delete=models.CASCADE)
country = models.CharField(max_length=50)
city = models.CharField(max_length=50)
job = models.CharField(max_length=100)
phoneNumber = models.PositiveIntegerField()
description = models.CharField(max_length=600)
profilePicture = models.ImageField(upload_to='users/', default='users/user-man1.jpg')
def __str__(self):
return self.user.username
class Skill(models.Model):
skillName = models.CharField(max_length=30, null=True)
user = models.ManyToManyField(User)
def __str__(self):
return self.skillName
views.py
def memberOnlyDetail(request, username):
user = User.objects.get(username=username)
skills = Skill.objects.filter(user=user)
memberDetails =User_Model.objects.get(user=user)
return render(request, 'memberMemberProfilePage.html', {'memberDetails': memberDetails}, {'skills': skills})
enter code here
memberMemberProfilePage.html
{%for skill in skills%}
<div class="col-lg-2 col-md-3 col-sm-6 hvr-grow-rotate">
<p>{{skill1.skillName}}</p>
</div>
{%endfor%}
答案 0 :(得分:0)
这里的问题是使用render()
功能。
下面是从签名的django documentation:
render(request, template_name, context=None, content_type=None, status=None, using=None)
现在让我们来看看参数你逝去的:
render(request, 'memberMemberProfilePage.html', {'memberDetails': memberDetails}, {'skills': skills})
当您正确地将request
,template_name
和{'memberDetails': memberDetails}
传递为context
时,您将{'skills': skills}
传递为第四个参数,即{{1} }。
要在您的上下文中正确传递content_type
,只需将其包括在上下文字典中,如下所示:
skills