loremIpsum1771
loremIpsum1771

Reputation: 2527

Error when saving variables in Django template context

I've made a webpage that submits a request to an api that returns a JSON object of information about a Concert tour based on the parameters provided by the user in a form. I'm able to successfully get the JSON and parse it. The structure of the data is a set of Concert objects with information about the venue, the date, location, etc. each of which I'm parsing and saving to a list (which is working). I then need to be able to display these as list (formatted as if they were concerts on Stubhub or Ticketmaster) in the template corresponding to the view.

To do this, I thought I could just store each list as a variable in the context dictionary for the template as I had already been doing with literal strings. For some reason, when I try this I'm getting Exception Value: local variable 'form_artistSelect' referenced before assignment for the following code:

def search(request):
    form = SearchForm(request.POST or None)
    if form.is_valid():
        form_artistSelect = urllib2.quote(form.cleaned_data.get("artist_select"))
        form_city =   urllib2.quote(form.cleaned_data.get("city"))
        form_state = urllib2.quote(form.cleaned_data.get("state"))
        mile_radius = urllib2.quote(form.cleaned_data.get("radius"))
        #print "testing"
        url = "http://api.bandsintown.com/events/search?artists[]=" + form_artistSelect + "&location=" +form_city+","+ form_state+"&radius="+ mile_radius + "&format=json&app_id=YOUR_APP_ID"
        data = json.load(urllib2.urlopen(url))

        #titles = [ i.get("title") for i in data]
        raw_dts = [str(i.get("datetime")) for i in data]

        #formatted_dts = [i.get("formatted_datetime") for i in data]
        ticket_urls = [str(i.get("ticket_url")) for i in data]
        ticket_statuses = [str(i.get("ticket_status")) for i in data]
        venue_names = [str(i.get("venue").get("name")) for i in data]
        venue_cities = [str(i.get("venue").get("city")) for i in data]
        venue_region = [str(i.get("venue").get("region")) for i in data]
context = {
        "form_artistSelect" : form_artistSelect,
        "raw_dts" : raw_dts,
        "ticket_urls" : ticket_urls,
        "ticket_statuses" : ticket_statuses,
        "venue_names" : venue_names,
        "venue_cities" : venue_cities,
        "venue_region" : venue_region,
        "form" : form
    }
    return render(request,"searchform.html" , context)

Why would be causing a problem with this approach? If this isn't the correct way to pass parsed data from a response to a template, then how should I go about doing so?

Upvotes: 0

Views: 91

Answers (1)

mhawke
mhawke

Reputation: 87084

At what level of indentation is the line context = { at?

As posted this is a syntax error. I imagine though that it is indented to the same level as the if statement.

form_artistSelect is assigned only if form.is_valid() is True so, if the form is not valid, form_artistSelect is undefined when you build the context dictionary. That will cause the exception that you see.

You need to figure out why the form is invalid, or move all of the code into the if form.is_valid(): block and add an else to render and return an error (or whatever is appropriate for your application).

Upvotes: 3

Related Questions