Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - language switching with query data
    primarykey
    data
    text
    <p>Hi I have a search results page which returns queries form the database using this view:</p> <pre><code>def search(request): show_results = False # check if POST if 'q' in request.POST: query = request.POST['q'].strip() # check if GET (paginated) if 'q' in request.GET: query = request.GET['q'].strip() # check if query length is more than 2 characters and proceed if query and len(query) &gt; 2: # if there is a query string show results (as opposed to arriving without any POST/GET show_results = True keywords = query.split() q = Q() for keyword in keywords: q = q &amp; (Q(title__icontains=keyword) | (Q(keywords__icontains=keyword))) query_set = Article.objects.filter(q) # create a new paginator instance with items-per-page paginator = Paginator(query_set, 10) # get the page number from a get param if param is blank then set page to 1 page = int(request.GET.get('page', '1')) # grab the current page from the paginator... items = paginator.page(page) # update search counter with term try: term = Search.objects.get(term=query) except Search.DoesNotExist: # if not already in db, then add query term = Search(term=query) term.counter += 1 term.last_search = datetime.now() term.save() elif len(query) &lt;= 2: short_string = True else: pass #render the template and pass the contacts page into the template return render_to_response('search_results.html', locals(), context_instance=RequestContext(request)) </code></pre> <p>and the template:</p> <pre><code>{% load i18n %} &lt;form action="/i18n/setlang/" name=postlink method="post"&gt; &lt;ul class="lang"&gt; &lt;li class="lang" style="color:gray"&gt; {% for lang in LANGUAGES %} {% if lang.0 != LANGUAGE_CODE %} &lt;input type="hidden" name="language" value="{{ lang.0 }}"&gt; &lt;a href=# onclick="submitPostLink()"&gt;{{ lang.1 }}&lt;/a&gt; {% else %} {{ lang.1 }} {% endif %} {% endfor %} &lt;/li&gt;&lt;/ul&gt; &lt;/form&gt; </code></pre> <p></p> <p>The language switching works fine on all pages except one case. When I submit the language change data on the search results page where no results have been returned (i.e. empty queryset), I get the following error:</p> <pre><code>UnboundLocalError at /search/ local variable 'query' referenced before assignment </code></pre> <p>I think I need to tweak the view slightly, but I'm not sure where. Any suggestions much appreciated.</p> <p>Traceback:</p> <pre><code>traceback: `Environment: Request Method: GET Request URL: http://localhost:8000/search/ Django Version: 1.3 Python Version: 2.7.1 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'journal', 'django.contrib.admin'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.csrf.CsrfResponseMiddleware') Traceback: File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/home/sam/public_html/django- projects/galapagos_research/../galapagos_research/journal/views.py" in search 40. if query and len(query) &gt; 2: Exception Type: UnboundLocalError at /search/ Exception Value: local variable 'query' referenced before assignment </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload