在赋值之前引用的django局部变量

时间:2016-01-12 20:21:35

标签: python django

我正在创建一个django应用程序,但我不确定我做错了什么。我一直收到错误"本地变量' sigtracker'在转让前引用"这是错误引用的代码。我知道这与表单验证有关,但我不确定我做错了什么。

@login_required
@permission_required('auth.signiture_management')
def add_signiture_view(request):
logger.debug("add_signiture_view called by user %s" % request.user)
if request.method == 'POST':
    form = SignitureForm(request.POST)
    logger.debug("Request type POST contains form valid: %s" %     form.is_valid())
    if form.is_valid():
        # handle valid form
        sigtracker = sigtracker()
        sigtracker.ident = form.cleaned_data['ident']
        sigtracker.system = form.cleaned_data['system']
        sigtracker.name = form.cleaned_data['name']
        sigtracker.signiture_type = form.cleaned_data['signiture type']
        sigtracker.status = form.cleaned_data['status']
        sigtracker.save()
        logger.info("Created new signiture in %s at %s by user %s" % (sigtracker.system, request.user))
        return HttpResponseRedirect("/sigtracker/")
else:
    logger.debug("Returning new SignitureForm")
    form = SignitureForm()

render_items = {'form': form}

return render_to_response('registered/addsigniture.html', render_items, context_instance=RequestContext(request))

Environment:


Request Method: POST
Request URL: http://redacted/add_signiture/

Django Version: 1.6.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'django_evolution',
 'djcelery',
 'celerytask',
 'bootstrapform',
 'authentication',
 'portal',
 'registration',
 'services',
 'eveonline',
 'groupmanagement',
 'hrapplications',
 'timerboard',
 'srp',
 'sigtracker')
Installed Middleware:
('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')




 Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/allianceserver/allianceauth/sigtracker/views.py" in add_signiture_view
  46.             sigtracker = sigtracker()

Exception Type: UnboundLocalError at /add_signiture/
Exception Value: local variable 'sigtracker' referenced before assignment

1 个答案:

答案 0 :(得分:1)

问题在于这一行

sigtracker = sigtracker()

您已在视图函数之外导入模型类sigtracker()。在Python中,不可能在函数内引用此类,并将其分配给相同的本地变量名。

最快的解决方法是将sigtracker实例重命名为其他内容,例如st

    st = sigtracker()
    st.ident = form.cleaned_data['ident']
    st.system = form.cleaned_data['system']
    st.name = form.cleaned_data['name']
    st.signiture_type = form.cleaned_data['signiture type']
    st.status = form.cleaned_data['status']
    st.save()
    logger.info("Created new signiture in %s at %s by user %s" % (st.system, request.user))

尝试使用sigtracker来引用模型类和模型实例是很困惑的。建议您为模型使用大写名称,例如SigTracker(这是一般的Python类的pep8推荐)。

这样,您可以轻松区分模型SigTracker和模型实例sigtracker之间的区别。

如果您将模型重命名为SigTracker,请确保在您使用它的任何地方更新导入和代码。