我试图在用户个人资料页面上显示当前课程金额,但我无法显示它。
这是我的models.py:
class LessonCount(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
lesson_current_amount = models.PositiveIntegerField(default=0, verbose_name='Current Number of Lessons')
def __unicode__(self):
return str(self.user.username)
views.py:
@login_required
def user_profile(request):
user = None
lessoncount = LessonCount.objects.all()
context = {
'user': user,
'lessoncount': lessoncount
}
return render(request, 'account/profile.html', context)
profile.html:
{% extends 'base.html' %}
{% block content %}
<h3 style="color: red;">{{ lessoncount.lesson_current_amount }}</h3>
<h3 style="color: red;">{{ request.user.username }}</h3>
{% endblock %}
提前致谢!
答案 0 :(得分:3)
您需要LessonCount
个对象,但LessonCount.objects.all()
是QuerySet。
将该行更改为:
lessoncount = LessonCount.objects.get(user=request.user)
答案 1 :(得分:2)
您需要遍历对象:
{% for lesson in lessoncount %}
<h3 style="color: red;">{{ lesson.lesson_current_amount }}</h3>
{% endfor %}
详细了解for
循环in the docs。