我在用django。
如果我检查单选按钮并点击submit,我的网页就会这样工作。它重定向到同一个页面,任务被重新定义,在此基础上检查无线电按钮。我的问题是after loading the page none of the radio buttons are checked。
我想先检查单选按钮,然后再单击submit
代码
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 %}更新:
这对我起了作用:
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发布于 2013-06-26 08:37:16
如果我正确理解,您希望让用户根据您显示的几个选项更改页面的内容。我还不知道jQuery在这里要做什么。
若要显示带有以前复选框的窗体,需要使用此数据实例化表单。与编辑视图相同。
例如,如果我有以下形式:
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')因此,我会像这样实例化它:
form = MyForm(initial={'choice1': True}现在,在模板中呈现时:
<form action="" method="post">
{{form.as_table}}
<input type='submit' value='Submit'>
</form>它将显示选中的choice1。
如您所见,不需要jQuery。
还有几件事:以前的复选框不能存储在视图中。视图只是一个函数,它的所有变量都是本地的。查找python变量范围以了解这意味着什么。如果您想存储一个选择,您可以有一个将跟踪用户选择的模型,或者将其保存在会话中,如下所示:
if form.is_valid():
request.session['user_choice']=form.cleaned_data通过这种方式,您可以使用request.session['user_choice']作为初始数据实例化表单。
This post很好地解释了如何检查使用jQuery检查的内容。
无论如何,对每个选项都有不同的视图会更容易,这样代码就会更简单、更易于维护和测试。这样就可以显示按钮而不是表单,不需要发布、验证、实例化。
https://stackoverflow.com/questions/17314473
复制相似问题