0

I am using django.

My webpage works like this, If i check the radio button and click on submit. it redirects to the same page with jobs redefined on the basis of which radiobuttons were checked. My problem is after loading the page none of the radio buttons are checked.

I want to check the radio button which are checked before i click submit

code

class MyHome(View):
    filter_options=(
        (('exp','Jobs by Experience'),
            (['l1','Less Than 1 year',0],
                ['1b2','1 to 2 Years',0],
                ['2b3','2 to 3 Years',0],
                ['3b5','3 to 5 Years',0],)),
        (('sal','Jobs by Salary'),
            (['l1000','Less Than $1000',0],
                ['1000b3000','$1000 to $3000',0],
                ['3000b5000','$3000 to $5000',0])),
        (('loc','Jobs by Location'),
            (['che','Chennai',0],
                ['ban','Banglore',0])),
    )    
    def get(self, request):
        from django.db.models import Q
        jobs=Job.objects.all().order_by("-postdate")
        c_radio=[]
        if 'exp' in request.GET:pass
        if 'sal' in request.GET:
            fl=request.GET['sal']
            c_radio.append(fl)
            if 'l' in fl:
                #return HttpResponse(fl[1:])
                q1=Q(basicpay__lte=fl[1:]) 
                self.filter_options[1][1][0][2]=1         
                jobs=jobs.filter(q1)
            elif 'b' in fl:
                jobs=jobs.filter(basicpay__range=fl.split('b'))
                self.filter_options[1][1][1][2]=1  
        if 'loc' in request.GET:
            c_radio.append(request.GET["loc"])
            jobs=jobs.filter(gmaplocation__district__icontains=request.GET["loc"])            
            #[2][1][1] ['ban', 'Banglore', 0]

        return render(request,'job/jobs.html',{"jobs":jobs,'fopts':self.filter_options,'c_radio':c_radio})   

html

{% for opts in fopts %}          
        <div class="accordion-group">
          <div class="accordion-heading emp">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapse{{ forloop.counter }}"><!--item 1-->
              {{ opts.0.1 }}
            </a>
          </div>
          <div style="height: 0px;" id="collapse{{ forloop.counter }}" class="accordion-body collapse">
            <div class="accordion-inner">                  
              {% for opt in opts.1 %}
              <label class="radio">
                <input type="radio" name="{{ opts.0.0 }}" value="{{ opt.0 }}"{% ifequal opt.0.2 1 %} checked{% endifequal %}> {{ opt.1 }}{{ opt.0.2 }}
              </label>
              {% endfor %}
              <!--<label class="checkbox">
                <input type="checkbox" name="redefine"> Other
                <input type="text" name="other">
              </label>-->
            </div>
          </div>
        </div><!--/accordion-group-->
      {% endfor %}

update:

This worked for me:

class RedefineChoices(forms.Form):
    CHOICES_EXP=(('l1','Less Than 1 year'),
            ('1b2','1 to 2 Years'),
            ('2b3','2 to 3 Years'),
            ('3b5','3 to 5 Years'),
            )
    CHOICES_SAL=(('l1000','Less Than $1000'),
            ('1000b3000','$1000 to $3000'),
            ('3000b5000','$3000 to $5000'),
            )
    CHOICES_LOC=(('che','Chennai'),
            ('ban','Banglore'),)
    exp = forms.ChoiceField(choices=CHOICES_EXP, widget=forms.RadioSelect(),label='Jobs by Experience')
    sal = forms.ChoiceField(choices=CHOICES_SAL, widget=forms.RadioSelect(),label='Jobs by Salary')
    loc = forms.ChoiceField(choices=CHOICES_LOC, widget=forms.RadioSelect(),label='Jobs by Location')enter code here
suhailvs
  • 20,182
  • 14
  • 100
  • 98
  • i stored a variable in views.py which define whether the radio button is checked or not ie 0 in `(['l1','Less Than 1 year',0],` then use it in template like ` – suhailvs Jun 26 '13 at 08:11

1 Answers1

1

if i understand correctly, you want to let users change the content of the page, based on a few options you show them. i dont see yet what jQuery has to do here.

To show a form with previously checked boxes, you need to instantiate the form with this data. Same as edit views.

For example, if i have following form:

from django import forms

class MyForm(forms.Form):
    choice1 = forms.BooleanField(label='Choice A')
    choice2 = forms.BooleanField(label='Choice B')
    choice3 = forms.BooleanField(label='Choice C')

In view, i would instantiate it like this:

form = MyForm(initial={'choice1': True}

now when rendering in template:

<form action="" method="post">
    {{form.as_table}}
    <input type='submit' value='Submit'>
</form>

it will show choice1 as checked.

As you can see no jQuery was needed.

A few more things: you can't store in view previously checked boxes. View is just a function, and all its variables are local. Look up python variable scope to understand what that means. If you want to store a choice, you can either have a model that will keep track of users' choices, or save it in session, like this:

if form.is_valid():
  request.session['user_choice']=form.cleaned_data

This way you can use request.session['user_choice'] as initial data to instantiate the form.

This post has a very good explanation how to check what was checked using jQuery.

Anyways, it will much easier for you to have different view for each option, this way the code will be simpler, easier to maintain and test. And this way you can show buttons and not a form, no need to post, validate, instantiate.

Community
  • 1
  • 1
Neara
  • 3,693
  • 7
  • 29
  • 40