如何在Django应用中解决此属性错误?

时间:2019-12-20 16:22:12

标签: python django middleware

我正在开发Django应用程序,并且创建了一个用户可以对电影进行增票或减票的地方,问题是当我按下以下按钮之一时,就会显示此错误:

in likesDislikesPelicula
voto = Likes.objects.filter(usuario=request.Usuarioclte, pelicula__pk=idpelicula)

AttributeError: 'WSGIRequest' object has no attribute 'Usuarioclte'

我的views.py

def detallesPelicula(request, pelicula_id):
    peliculas = get_list_or_404(Pelicula.objects.order_by('titulo'))
    pelicula = get_object_or_404(Pelicula, pk=pelicula_id)
    actor = get_list_or_404(Actor.objects)
    likes = conteolikes(pelicula)
    dislikes = conteodislikes(pelicula)
    context = {
        'pelicula': pelicula,
        'peliculas': peliculas,
        'likes': likes,
        'dislikes': dislikes
    }
    return render(request, 'detallesPelicula.html', context)

def conteolikes(pelicula):
        return pelicula.users_vote.filter(valor__gt=0).count()

def conteodislikes(pelicula):
        return pelicula.users_vote.filter(valor__lt=0).count()

@csrf_exempt
def likesDislikesPelicula(request):
      if request.method == 'POST':
          idpelicula= request.POST.get('idpelicula','')
          valor = request.POST.get('valor','')
          try:
             voto = Likes.objects.filter(usuario=request.Usuarioclte, pelicula__pk=idpelicula)
             print("Existe")
             print(voto)
          except  Likes.DoesNotExist:
             print("No existe")

my models.py(简体)

class Usuarioclte(models.Model):
    nickname = models.CharField(max_length=50)
    contraseña = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)

    def __str__(self):
        return self.nickname

class Likes(models.Model):
    #necesito que usuario y pelicula sean una calve compuesta
    #usuario_id= models.ForeignKey(User.id,on_delete= models.CASCADE)
    pelicula =  models.ForeignKey(Pelicula, on_delete=models.CASCADE, related_name = 'users_vote')
    usuarioclte = models.ForeignKey(Usuarioclte, on_delete=models.CASCADE, related_name= 
    'likes_and_dislikes')

    valor = models.IntegerField(default=0)
    def __str__(self):
        return self.usuarioclte.nickname

注意:这些用户是由Django身份验证系统获取和存储的,而不是在我创建的表中存储的。

我已经看到必须与MIDDLEWAREMIDDLEWARE_CLASSES做些事情,我试图进行更改,但仍然无法正常工作

我在setting.py中的MIDDLEWARE

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.middleware.locale.LocaleMiddleware', #i18n
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

0 个答案:

没有答案