You are not logged in.

Change Language:

Profile for brentofallpeople

Name brentofallpeople
Email Address n/a
Posts2
  • Re: Template renders with old values
    Board » Django » Templates
    NEVER MIND: I just found the problem in my urls.py. I checked urls.py twenty times and didn't notice until now. I can't believe I wasted five hours on this.
  • Template renders with old values
    Board » Django » Templates
    Hi,

    I'm using Jquery to dynamically alter pages on my Django project. One of my pages has a series of drop-down menus. At first, you can see only one menu. The user chooses a 'region' from that menu. To the right appears a new drop-down; the user chooses a country from the new menu. A new drop-down appears to the right, and the user chooses a province, and finally there's a new drop-down for the city. At each step, there's a display of your choice so far. Once you choose a city, you get the final result.

    Region ## You've chosen "North America"
    Region > Country ## You've chosen "United States, North America"
    Region > Country > Province ## You've chosen "New York, United States, North America"
    Region > Country > Province > City ## You've chosen "Albany, New York, United States, North America"

    Each drop-down calls its own javascript function, which makes an HTTP request to my ajax code in the Django app. Each ajax "view" loads the same template and renders it with values from the database (ie, countries in the chosen region, provinces in the chosen country, etc.). The rendered template is passed back to the javascript that called it, which updates the relevant section of the page. Here's the template:

    <select id="{{ select_id }}" class="{{ select_class }}" name="{{ select_name }}" onchange="{{ select_onchange }}">
    <option value="0">----</option>
    {% for option in select_options %}<option value="{{ option.id }}" onclick="{{ option_onclick }}">{{ option.name }}</option>
    {% endfor %}
    </select>

    It works fine until I get to city drop-down. It has the right cities, but the attributes in the select tag are the attributes for the country drop down. In other words:

    1. Choose region. Render template for country drop-down. Success!
    2. Choose country. Render template for province drop-down. Success!
    3. Choose province. Render template for city drop-down, BUT use the context from when you rendered the country drop-down. Then plug in the correct list of cities.

    I've noticed that the part of the template that always works is the for-loop, ie, the part that comes between {% and %}. Parts that come between {{ and }} don't work, unless they're part of the for-loop ( so, {{ option.id }}, but not {{option_onclick }} ).

    It's also weird that it works fine for province, then uses country values for the city menu. I think it's something under the hood in Django, but I'm having a hard time tracking down a solution. Does anyone here have any experience with this sort of behavior?

    Thanks,
    Brent

    --- code snippets ---
    # ajax.insert_countries(request) -----------------------------------------------
    def insert_countries(request):
    response_dict = {}

    region_id = request.POST.get('geo_id', False)

    if region_id:
    country_list = Geography.objects.filter( is_located_in=int(region_id) )

    # We'll be feeding values into the generic select_form widget.
    response_dict.update( {'select_id' : 'country_id' ,
    'select_class' : 'geo_drop_down' ,
    'select_name' : 'country_id' ,
    'select_onchange' : 'subdivisions_or_cities();' ,
    'select_options' : country_list ,
    'option_onclick' : 'subdivisions_or_cities();' })

    return render_to_response('widgets/select_form.fragment.html', response_dict)


    # ajax.insert_subdivisions(request) --------------------------------------------
    def insert_subdivisions(request):
    response_dict = {}

    country_id = request.POST.get('geo_id', False)

    if country_id:
    subdivision_list = Geography.objects.filter( type='Subdivision' )
    subdivision_list = subdivision_list.filter( is_located_in=int(country_id) )

    else:
    subdivision_list = Geography.objects.none()

    # We'll be feeding values into the generic select_form widget.
    response_dict.update( {'select_id' : 'subdivision_id' ,
    'select_class' : 'geo_drop_down' ,
    'select_name' : 'subdivision_id' ,
    'select_onchange' : 'insert_cities_for_subdivision();' ,
    'select_options' : subdivision_list ,
    'option_onclick' : 'insert_cities_for_subdivision();' })

    return render_to_response('widgets/select_form.fragment.html', response_dict)

    def insert_cities(request):
    response_dict = {}

    geo_id = request.POST.get('geo_id', False)
    # could be geo_id for country or subdivision

    if geo_id:
    city_list = Geography.objects.filter( type='City' )
    city_list = Geography.objects.filter( is_located_in=int(geo_id) )

    # We'll be feeding values into the generic select_form widget.
    response_dict.update( {'select_id' : 'city_id' ,
    'select_class' : 'geo_drop_down' ,
    'select_name' : 'city_id' ,
    'select_onchange' : 'select_city();' ,
    'select_options' : city_list ,
    'option_onclick' : 'select_city();' } )

    return render_to_response('widgets/select_form.fragment.html', response_dict)


Powered by Sphene Community Tools