Python django表单处理和执行行为跳过执行
我在 python django 中处理表单时遇到一些问题。处理表单时出现一些异常行为。下面是我的表单和在视图中处理表单的部分函数。我对 python 和 django 相当陌生,所以任何帮助将不胜感激。
class SearchForm(forms.Form):
pr_name = forms.CharField(label="Pr Name", max_length=64, required=False)
org = forms.ModelChoiceField(queryset=Org.objects.all(), required=False)
group_name = forms.CharField(label="Unique Submission Name", max_length=64, required=False)
group_ref = forms.CharField(label="Ref", max_length=12, required=False)
group_url = forms.URLField(label="URL", required=False)
def search(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
p_ids = []
g_ids = []
f_ids = []
logging.debug('hello1')
# Filter first
firstQuery = 'SELECT * FROM pr where '
pr_name = form.cleaned_data['pr_name']
if pr_name:
logging.debug('hello2')
firstQuery += '(name like \'%' + pr_name + '%\')'
else:
pass
logging.debug('hello3')
org = form.cleaned_data['org']
if org:
org = Org.objects.get(name = org)
org_id = org.id
firstQuery += '(org_id = ' + str(org_id) + ')'
else:
pass
firstQuery = firstQuery.replace(')(', ') AND (')
logging.debug('First query: %s' % firstQuery)
p_search_results = P.objects.raw(firstQuery)
logging.debug('First query: %s' % p_search_results)
for x in p_search_results:
p_ids.append(x.id)
logging.debug('p_ids: %s' % p_ids)
# Filter Group
secondQuery = 'SELECT * FROM group where '
group_name = form.cleaned_data['group_name']
if group_name:
secondQuery += '(name like \'%' + group_name + '%\')'
else:
pass
group_ref = form.cleaned_data['group_ref']
if group_ref:
secondQuery += '(ref like \'%' + group_ref + '%\')'
else:
pass
group_url = form.cleaned_data['group_url']
if group_url:
secondQuery += '(method_url like \'%' + group_url + '%\')'
else:
pass
secondQuery = secondQuery.replace(')(', ') AND (')
logging.debug('Second query: %s' % secondQuery)
group_search_results = PredictionGroup.objects.raw(secondQuery)
logging.debug('Second query: %s' % group_search_results)
for x in group_search_results:
g_ids.append(x.id)
logging.debug('g_ids: %s' % g_ids)
...
...
...
当我在表单中输入 pr_name 并提交时,视图会输入第一个 if 语句,在控制台中打印出“hello1”和“hello2”,但会跳过“hello3”并跳过第二个 if 语句并向前跳至 secondaryQuery = secondaryQuery.replace(')(', ') AND (')
调试输出为:
DEBUG hello1
DEBUG hello2
错误为:
UnboundLocalError at /search/
local variable 'secondQuery' referenced before assignment
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File ".../views.py" in search
209. secondQuery = secondQuery.replace(')(', ') AND (')
Exception Type: UnboundLocalError at /search/
Exception Value: local variable 'secondQuery' referenced before assignment
预期输出:
DEBUG hello1
DEBUG hello2
DEBUG hello3
DEBUG First query: SELECT * FROM pr where .... (firstQuery)
DEBUG First query: ..... (p_search_results)
I am having some problems with processing my form in python django. There is some unusual behavior when processing the form. Below is my form and part of the function that processing the form in the view. I am fairly new to python and django, so any help will be much appreciated.
class SearchForm(forms.Form):
pr_name = forms.CharField(label="Pr Name", max_length=64, required=False)
org = forms.ModelChoiceField(queryset=Org.objects.all(), required=False)
group_name = forms.CharField(label="Unique Submission Name", max_length=64, required=False)
group_ref = forms.CharField(label="Ref", max_length=12, required=False)
group_url = forms.URLField(label="URL", required=False)
def search(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
p_ids = []
g_ids = []
f_ids = []
logging.debug('hello1')
# Filter first
firstQuery = 'SELECT * FROM pr where '
pr_name = form.cleaned_data['pr_name']
if pr_name:
logging.debug('hello2')
firstQuery += '(name like \'%' + pr_name + '%\')'
else:
pass
logging.debug('hello3')
org = form.cleaned_data['org']
if org:
org = Org.objects.get(name = org)
org_id = org.id
firstQuery += '(org_id = ' + str(org_id) + ')'
else:
pass
firstQuery = firstQuery.replace(')(', ') AND (')
logging.debug('First query: %s' % firstQuery)
p_search_results = P.objects.raw(firstQuery)
logging.debug('First query: %s' % p_search_results)
for x in p_search_results:
p_ids.append(x.id)
logging.debug('p_ids: %s' % p_ids)
# Filter Group
secondQuery = 'SELECT * FROM group where '
group_name = form.cleaned_data['group_name']
if group_name:
secondQuery += '(name like \'%' + group_name + '%\')'
else:
pass
group_ref = form.cleaned_data['group_ref']
if group_ref:
secondQuery += '(ref like \'%' + group_ref + '%\')'
else:
pass
group_url = form.cleaned_data['group_url']
if group_url:
secondQuery += '(method_url like \'%' + group_url + '%\')'
else:
pass
secondQuery = secondQuery.replace(')(', ') AND (')
logging.debug('Second query: %s' % secondQuery)
group_search_results = PredictionGroup.objects.raw(secondQuery)
logging.debug('Second query: %s' % group_search_results)
for x in group_search_results:
g_ids.append(x.id)
logging.debug('g_ids: %s' % g_ids)
...
...
...
When I enter a pr_name in the form and submit, the view enters the first if statement prints out 'hello1' and 'hello2' in the console but skips the 'hello3' and skips the second if statement and skips ahead to secondQuery = secondQuery.replace(')(', ') AND (')
The debug output is:
DEBUG hello1
DEBUG hello2
And the error is:
UnboundLocalError at /search/
local variable 'secondQuery' referenced before assignment
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File ".../views.py" in search
209. secondQuery = secondQuery.replace(')(', ') AND (')
Exception Type: UnboundLocalError at /search/
Exception Value: local variable 'secondQuery' referenced before assignment
Expected output:
DEBUG hello1
DEBUG hello2
DEBUG hello3
DEBUG First query: SELECT * FROM pr where .... (firstQuery)
DEBUG First query: ..... (p_search_results)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道在你的源代码中混合了空格和制表符,并且你认为应该执行的代码块实际上没有执行,因为它位于“if”语句内?
can it be that in your source you have spaces and tabs mixed and the block of code that you think should be executed is actually not executed because it is inside an "if" statement?