0

这是我在 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 in home, line 8

请问我可以帮忙吗?感谢您的阅读。

4

2 回答 2

1

您定义points为 的成员User,而不是WSGIRequest。使用request.user.points.

于 2013-07-28T23:48:45.333 回答
-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})
于 2013-07-29T00:07:02.747 回答