UnboundLocalError local variables referenced before assignment / Consideration
<p>I have the following function based view</p>

<pre><code>def post_search(request):
    form = SearchForm()
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            cd = form.cleaned_data
            results = SearchQuerySet().models(Post).filter(content=cd['query']).load_all()
            # count total results
            total_results = results.count()
    return render(request, 'blog/post/search.html', {'form': form,
                                                     'cd': cd,
                                                     'results': results,
                                                     'total_results': total_results})
</code></pre>

<p>When I execute my url that call this function I get the following error</p>

<pre><code>UnboundLocalError: local variable 'cd' referenced before assignment
     [25/Aug/2016 22:48:13] "GET /blog/search/ HTTP/1.1" 500 69440
</code></pre>

<p><code>cd</code>, <code>results</code> and <code>total_results</code> variables are declared and used in a scope different to the <code>return render(request ...)</code> sentence at the end.</p>

<p>It's for this reason the error.</p>

<p>I've initialized this variables as a globals of this way:</p>

<pre><code>cd = results=total_results=None 
</code></pre>

<p>before of the conditional sentences</p>

<pre><code>def post_search(request):
    form = SearchForm()

    # Initialize variables
    cd = results=total_results=None
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            cd = form.cleaned_data
            results = SearchQuerySet().models(Post).filter(content=cd['query']).load_all()
            # count total results
            total_results = results.count()
    return render(request, 'blog/post/search.html', {'form': form,
                                                     'cd': cd,
                                                     'results': results,
                                                     'total_results': total_results})
</code></pre>

<p>And my GET request it's works </p>

<pre><code>[25/Aug/2016 23:05:33] "GET /blog/search/ HTTP/1.1" 200 2260
</code></pre>

<p>I could solve this of some another way?
This functionality is for search in a web, 
Is this good when I have many users using this functionality? </p>

<p>I think that the solution is not good practice ... I don't know.</p>

<p>It means, either your <code>form.is_valid()</code> is <code>False</code> or, you are not making the <code>GET</code> request. </p>

<p>Add <code>cd = results = total_results = None</code> before the first if condition. Because, in your current code, if any of the <code>if</code> condition fails, <code>cd</code>, <code>results</code> and <code>total_results</code> will not be initialized and hence will throw this error. Your updated code should be:</p>

<pre><code>def post_search(request):
    form = SearchForm()
    cd = results = total_results = None  # &lt;--- Add this to your code
    if 'query' in request.GET:
        . . . Something Something
    return render(request, 'blog/post/search.html', {'form': form,
                                                     'cd': cd,
                                                     'results': results,
                                                     'total_results': total_results})
</code></pre>

python django variables scope