0

Currently I have a drop down box with all the options. After submit is pressed, the page is reloaded with all the selected variables in the url

http://127.0.0.1:8000/website/?var1=choice1&var2=choice3

So the same view is reloaded and the variables are collected and analysed by the view. Based on the selection, different data from the database is sent to this webpage page, thus making different tables load underneath this dropdown menu.

Is it possible to show the selected data in the form after the refresh? Something like this: My current site

So far I've only used HTML. I thought that perhaps using jQuery I could get this to work, but the language is perplexing (though I'm very willing to learn, if someone knows how to do it with jQuery)

SOLUTION

Under Chris' instructions, I managed to make this to work. First of all, what didn't work with Chris' solution was the request.GET method. Therefore, inside the VIEW that calls return render_to_response('website.html') (so the corresponding view), I added the following piece of code:

requestDict = request.GET

return render_to_response('optionset.html', {'request':requestDict}) 

Inside my template therefore, I did the following:

<form action="" name="OptionSetForm">
     <select name="var1" size=2>
     <OPTGROUP LABEL="FirstPickHere">
            <option {% if request.var1 == "Choice1" %} selected="selected"{% endif %}>Choice1</option>
            <option {% if request.var1 == "Choice2" %} selected="selected"{% endif %}>Choice2</option>
            <option {% if request.var1 == "Choice3" %} selected="selected"{% endif %}>Choice3</option>
    </OPTGROUP>
    </select>
</form>

Make sure that you have spaces between the == and the two variables.

SaiyanGirl
  • 16,376
  • 11
  • 41
  • 57

2 Answers2

1

You can use jQuery to parse the variables from the query string (here's a library for that) and then populate the multiselect fields as described here.

Community
  • 1
  • 1
Joe Mornin
  • 8,766
  • 18
  • 57
  • 82
1

Since you're using select boxes, you'll need to add the following to each option:

<option value="choice1"{% if request.GET.var1 == "choice1" %} selected="selected"{% endif %}>Choice 1</option>

Or, if you'd rather use jQuery:

var queryString = window.location.search.substring(1);
var params = queryString.split('&');
for (var i=0; i<params.length; i++) {
    var param = params[i].split('=');
    $('[name='+param[0]+']').val(param[1])
}
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • hmm... I tried that, but it doesn't seem to work. Even doing `{% if request.GET.var1 == "choice1" %} hey {% endif %}` outside my table doesn't print anything =/. Between the two, I'd rather use your first option since I don't know much jQuery yet. – SaiyanGirl Aug 10 '12 at 21:31
  • Also, I don't know how much it matters, but my dropdown is loaded as `{% for d in dim %} {% endfor %}` and when doing the line of code `{% if request.GET.var1=={{d}} %}` I get the error `Could not parse the remainder: '=={{d}}'` :( – SaiyanGirl Aug 10 '12 at 21:54
  • You don't use `{{ }}` inside a `{% %}` block. Just the variable itself: `{% if request.GET.var1==d %}` – Chris Pratt Aug 13 '12 at 14:29
  • Still the same error `Could not parse the remainder: '==d'`. The reason why this happened though was because I didn't have a space between the `==`. Also, instead of using the request.GET method doesn't seem to work inside the template. However, it works inside the view, so I just passed the obtained dictionary and now it works! :D – SaiyanGirl Aug 13 '12 at 16:14
  • Sorry. That was my mistake. Yes, there should be spaces around the `==`. If `request` isn't available in your template, you need to add `django.core.context_processors.request` to `TEMPLATE_CONTEXT_PROCESSORS`. – Chris Pratt Aug 13 '12 at 17:20
  • Hmm, I don't know. The default already has that (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS). I did try it, but it didn't work. If you really want to figure this out, I don't mind trying out your solutions. If you don't feel like it, then no worries, as I do have something that works :). Thanks! – SaiyanGirl Aug 13 '12 at 17:31
  • If you have that in your `TEMPLATE_CONTEXT_PROCESSORS`, then the only other thing is using `RequestContext` (see: https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext). If you're running Django 1.3+, using `render` to return the response from your views will take care of this automatically. Otherwise, the docs describe how to make it work. – Chris Pratt Aug 13 '12 at 19:39