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

时间:2011-03-29 17:38:36

标签: django-forms

我的观点:

def refer(request, seek_id):
    seek_obj = SeekSolutions.objects.get(pk = seek_id)
    if request.method == 'POST':
        form = SeekReferForm(request.POST)
        if form.is_valid():
            obj = form.save(commit = False)
            obj.refferer = request.user
            obj.seek_id = seek_obj
            obj.save()
            send_message(obj)
            return HttpResponseRedirect('/seek/referred')
    return render_to_response('seek/refer.html', {'form' : form , } )

追溯:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/options.py in wrapper
                return self.admin_site.admin_view(view)(*args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/views/decorators/cache.py in _wrapped_view_func
        response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/sites.py in inner
            return view(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in _wrapper
            return bound_func(*args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in bound_func
                return func(self, *args2, **kwargs2) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/options.py in changelist_view
            'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py in __len__
                self._result_cache = list(self.iterator()) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py in iterator
        for row in compiler.results_iter(): ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py in results_iter
        for rows in self.execute_sql(MULTI): ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py in execute_sql
        cursor.execute(sql, params) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/util.py in execute
            return self.cursor.execute(sql, params) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute
            return Database.Cursor.execute(self, query, params) ...
▶ Local vars

2 个答案:

答案 0 :(得分:2)

虽然这个问题已经很老了,但是由于我遇到了同样的问题(并且刚解决了),我想我应该回答它以供将来参考。但后来我遇到了一个不同的错误。但是你似乎在代码中遗漏了一些东西。

def refer(request, seek_id):
seek_obj = SeekSolutions.objects.get(pk = seek_id)
if request.method == 'POST':
    form = SeekReferForm(request.POST)
    if form.is_valid():
        obj = form.save(commit = False)
        obj.refferer = request.user
        obj.seek_id = seek_obj
        obj.save()
        send_message(obj)
        return HttpResponseRedirect('/seek/referred')
else:
    form = SeekReferForm()
return render_to_response('seek/refer.html', {'form' : form , } )

这包括else阻止您的第一个if。这是因为当您尝试从浏览器访问该URL时,您实际上是在资源上执行GET请求。结果是第一个if被违反并直接进入return语句,在该语句中它尝试返回一个字典项'form'作为key和一个form对象从来没有创造过。因此代码中的错误。如果您尝试在浏览器中打开网址,则需要有一个其他部分可以实例化form对象。我希望这有助于事业。

答案 1 :(得分:0)

只有在request.method =='POST'时才会实例化表单