django - Python - if, elif, elif, else Statement Not Working as Expected -


i'm using django web site , needed build context processor provide referrer (variable named referer) information.

i have simple if, elif, elif, else statement:

[ . . . ]  host = get_current_site(request) local_url = site_urls['local'] dev_url = site_urls['dev'] prod_url = site_urls['prod']  # print referer debugging purposes - remove when done... print("current host: {0}".format(host)) print("current urls: {0} {1} {2}".format(local_url, dev_url, prod_url))  # determine default referer - eg, set host/site name if host == prod_url:     referer = prod_url  elif host == dev_url:     referer = dev_url  elif host == local_url:     referer = local_url  else:     # set referer current request     try:         referer = request.meta['http_referer']      except keyerror e:         print('error: key error - referer doesn\'t exist: {0}'.format(str(e)));  [ . . . ] 

what's weird print statements above yield host being equal local_url (from console):

current host: http://localhost:8000 current urls: http://localhost:8000 [ . . . ] 

yet still evaluating else > try , throwing key error... point when default host/site not available, request.meta['http_referer'] valid.

what going wrong here? i'm missing something. python telling me host != local_url why?

edit

thanks great hint @martijn pieters. changed print statements , see this:

current host: <site: http://localhost:8000> current urls: 'http://localhost:8000' 

i think forgot use attributes sites framework:

https://docs.djangoproject.com/en/dev/ref/contrib/sites/

most have whitespace issue; replace formatting with:

print("current host: {0!r}".format(host)) print("current urls: {0!r} {1!r} {2!r}".format(local_url, dev_url, prod_url)) 

to use repr() values instead; these include more information on type of value , trailing whitespace obvious.

if see django.contrib.sites.models.site object, compare against domain attribute:

if host.domain == prod_url: 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -