我的models.py文件中有模型名为模板
class template(models.Model):
id = models.AutoField(primary_key=True)
customer_id = models.ForeignKey(customer,on_delete=models.CASCADE)
name = models.CharField(max_length=255,blank=False,null=False)
data = RichTextField()
def __unicode__(self):
return self.name
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("templatedetail",kwargs={"id": self.id})
然后我在我的views.py
中导入此模型from .models import template
然后当我尝试获取方法中的所有对象时
def assign_user_template(request,email=None):
if 'credentials' not in request.session:
return redirect('/oauth2callback/')
credentials = client.OAuth2Credentials.from_json(request.session['credentials'])
if credentials.access_token_expired:
return redirect('/oauth2callback/')
else:
http = httplib2.Http()
http = credentials.authorize(http)
user_service = build('admin','directory_v1',http=http)
user_profile = user_service.users().get(userKey=email).execute()
user_profile_email = user_profile.get('primaryEmail')
user_profile_service = build('oauth2', 'v2', http=http)
user_info = user_profile_service.userinfo().get().execute()
user_email = user_info.get('email')
admin_service = build('admin','directory_v1',http=http)
admin_profile = admin_service.users().get(userKey=user_email).execute()
customer_id = admin_profile.get('customerId')
templates = template.objects.filter(customer_id = customer_id)
template_user_object = template_user_map.objects.filter(user_email = email)
if template_user_object:
template_user_object = template_user_map.objects.get(user_email = email)
template_user = template_user_object.template_id
else:
template_user = None
if request.method == 'POST':
form = TemplateSelectForm(request.POST)
if form.is_valid():
template = form.cleaned_data['selected_template']
user_template_map_object = template_user_map(user_email=user_profile_email,template_id=template)
user_template_map_object.save()
user_profile = user_service.users().get(userKey=user_profile_email).execute()
customer_id = user_profile.get('customerId')
customer_object = customer.objects.get(id = customer_id)
//some code
return HttpResponseRedirect('/userlist/')
else:
form = TemplateSelectForm()
return render(request,'assign_user_template.html',{"form":form})
但是我得到了这个本地变量'模板'在赋值错误之前引用。模型以正确的方式导入。当我在不同的模型上尝试这个时,它没有给出任何错误。
完全追溯
Traceback:
File "C:\Users\User\Desktop\New folder (6)\final\lib\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\User\Desktop\New folder (6)\final\lib\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\Desktop\New folder (6)\final\lib\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\Desktop\New folder (6)\final\dashboard\views.py" in assign_user_template
675. templates = template.objects.filter(customer_id = customer_id)
Exception Type: UnboundLocalError at /edituser/bravin@demo.fcpl.biz/assigntemplate/
Exception Value: local variable 'template' referenced before assignment
答案 0 :(得分:1)
template = form.cleaned_data['selected_template']
我认为它的名称与类模板相冲突。盖上所有课程。