预期的行为:
我有一个django应用程序,用户可以在其中上传“ beats”(mp3文件),我最终希望拥有免费层和付费层。免费套餐将受到他们能够上传的“拍子”数量的限制。为免费用户说最多3个节拍。
我正在尝试在视图中运行查询,该查询会将用户发送到模板页面,如果用户超出允许上传的节拍数量,则要求他们成为高级会员。它应该检查谁是登录用户,获取该用户上传的节拍计数,如果小于允许的数量,则显示上传模板,否则显示注册模板。
当前行为:
使用self.request.user给出我的NameError自我未定义。将用户ID硬编码到查询中时,我能够使代码工作。这是我当前的代码。
#beatupload/views.py
class UploadView(ListView):
model = beat
template_name = 'uploads.html'
def get_queryset(self):
return beat.objects.filter(producer=self.request.user)
class uploadNew(CreateView): # new
model = beat
fields = ['title', 'beat']
success_url = reverse_lazy('uploads')
#Check number of beats uploaded by user and if exceeds amount require signup
#To render sign up template if true and proceed to upload if false
def get_queryset():
return beat.objects.filter(producer_id="3")
if get_queryset().count() <= 2:
template_name = 'uploadNew.html'
else:
template_name = 'becomeMember.html'
#END CHECK
def form_valid(self, form):
form.instance.producer = self.request.user
return super(uploadNew, self).form_valid(form)
# beatupload/models.py
from django.conf import settings
from django.db import models
class beat(models.Model):
title = models.CharField(max_length=50)
beat = models.FileField(upload_to='beats/')
producer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def __str__(self):
return self.title
如果您在上述类中看到了ListView,则表示已成功列出仅由登录用户上传的节拍。尝试修改该代码并获得计数是行不通的,因为我得到的“自我”未定义错误。例如:
class uploadNew(CreateView): # new
model = beat
fields = ['title', 'beat']
success_url = reverse_lazy('uploads')
#Check number of beats uploaded by user and if exceeds amount require signup
#To render sign up template if true and proceed to upload if false
def get_queryset(self):
return beat.objects.filter(producer=self.request.user)
if get_queryset(self).count() <= 2: ###ERROR COMES FROM HERE###
template_name = 'uploadNew.html'
else:
template_name = 'becomeMember.html'
#END CHECK
def form_valid(self, form):
form.instance.producer = self.request.user
return super(uploadNew, self).form_valid(form)
如上所述,我在代码的if语句部分收到名称错误。
如果我将if语句缩进到这样的函数中:
class uploadNew(CreateView): # new
model = beat
fields = ['title', 'beat']
success_url = reverse_lazy('uploads')
#Check number of beats uploaded by user and if exceeds amount require signup
#To render sign up template if true and proceed to upload if false
def get_queryset(self):
return beat.objects.filter(producer=self.request.user)
if get_queryset(self).count() <= 2:
template_name = 'uploadNew.html'
else:
template_name = 'becomeMember.html'
#END CHECK
def form_valid(self, form):
form.instance.producer = self.request.user
return super(uploadNew, self).form_valid(form)
我没有收到错误,但是django无法找到模板。
另外,如果我重新启动开发服务器,它似乎只能工作(当用硬编码生产者的ID编码时)。因此,如果限制设置为<= 2(我希望允许3次上传),则只要开发服务器正在运行,我就可以继续上传节奏。如果我停止服务器并再次启动它,则将我重定向到模板变得成为Member.html。
\views.py", line 27, in uploadNew
if get_queryset(self).count() <= 2:
NameError: name 'self' is not defined
这是名称错误。
这是模板错误:
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\Alex.INACOM-SBY\Documents\code\theBeatStore\agBeatStoreRoot\templates\beatupload\beat_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alex.INACOM-SBY\Documents\code\env\lib\site-packages\django\contrib\admin\templates\beatupload\beat_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alex.INACOM-SBY\Documents\code\env\lib\site-packages\django\contrib\auth\templates\beatupload\beat_form.html (Source does not exist)
答案 0 :(得分:0)
您应该重写get_template_names
方法,如下所示:
def get_template_names(self):
if beat.objects.filter(producer=self.request.user).count() <= 2:
return ['uploadNew.html',]
else:
return ['becomeMember.html',]