Reputation: 105
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)
Upvotes: 0
Views: 143
Reputation: 29576
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?
Upvotes: 1