这是我在models.py中的课程:
from django.db import models
from django.contrib.auth.models import User as defaultUser
class User(models.Model):
user = models.OneToOneField(defaultUser)
points = models.PositiveIntegerField(default=50)
这是我的views.py:
def home(request):
username = request.user.username
points = request.points
return render(request, "index.html", {'username' : username , 'points' : points})
如果我评论点我没有得到任何错误,但当我使用点我得到这个错误:
我能帮帮忙吗?谢谢你的阅读。异常类型:AttributeError异常值:'WSGIRequest'对象 没有属性'points'异常 位置:/Users/jacintofleta/djcode/points/points/views.py在家里, 第8行
答案 0 :(得分:0)
您将points
定义为User
的成员,而不是WSGIRequest
。使用request.user.points
。
答案 1 :(得分:-1)
尝试扩展django类用户
from django.db import models
from django.contrib.auth.models import User as defaultUser
class User(defaultuser):
points = models.PositiveIntegerField(default=50)
views.py
from models import User
def home(request):
username = User.username
points = User.points
return render(request, "index.html", {'username' : username , 'points' : points})