Search code examples
djangodjango-deployment

Django views error when deployed


I have modified the following method on my development machine and it works perfectly fine there:

def profile_search(request):
    args = {}
    qs=[]
    if request.method == 'GET':        
        page = request.GET.get('page')
        print 'page is:' + str(page)
        if 'search-profiles-post' in request.session: 
            request.POST = QueryDict('').copy()
            request.POST.update(request.session['search-profiles-post'])            
            request.method = 'POST' 


    if request.method == "POST":
        form = AdvancedSearchForm(request.POST)
        request.session['search-profiles-post'] = request.POST


        if form.is_valid():
            cd = form.cleaned_data

            s_country=cd['country']
            s_province=cd['province']
            s_city = cd['city']

            if s_country: qs.append(Q(country__icontains = s_country))
            if s_province: qs.append( Q(province__icontains=s_province))             
            if s_city: qs.append( Q(city__icontains=s_city))




            f = None
            for q in qs:
                if f is None: 
                    f=q                                  
                else: f &=q
            print f

            if f is not None:
                profiles = UserProfile.objects.filter(f).order_by('-created_at') 


        else:
            form = AdvancedSearchForm()
            profiles = UserProfile.objects.all().order_by('-created_at') 

    paginator = Paginator(profiles,12) # << This line is problematic

    page= request.GET.get('page')
    try:
        results = paginator.page(page)
    except PageNotAnInteger:
        results = paginator.page(1)  

    except EmptyPage:
            results = paginator.page(paginator.num_pages)        

    args.update(csrf(request))    
    args['form'] = form  
    args['results'] = results
    return render_to_response('userprofile/advanced_search.html', args,
                              context_instance=RequestContext(request)) 

However when I copy the views into my production (which worked fine before the modification) I get this error:

UnboundLocalError at /search/
local variable 'profiles' referenced before assignment

Traceback:

File "/root/.djenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./userprofile/views.py" in profile_search
  739.     paginator = Paginator(profiles,12)

Exception Type: UnboundLocalError at /search/
Exception Value: local variable 'profiles' referenced before assignment

As far as I can say, the only code that is different between my production and development is the settings.py (which use different databases).

Since the search queries are to be saved into session, I suspect it has something to do with session handing in production but I don't know how.

A point that might be relevant: While I have installed debug-toolbar on my production machine, I can not see its tab even at debug mode.

I appreciate your hints to fix this.


Solution

  • Your problem is the profiles variable not being defined in production.

    There is a code path that doesn't define your profiles variable.

    Upon closer look, you should be able to why it's happening.

    your qs may be empty, your assumption that request.method must be either get or post might be wrong. (client might send patch, delete request). There could be more reasons...

    In any case, your code has holes that doesn't define profiles variable, and your production server (or client using the server) is different in a sense that it is going through the different execution path.

    You could put a import pdb; pdb.set_trace() and debug it. or mentally analyse your code to fix it.