我對Django非常陌生,試圖從用戶從索引頁上的下拉菜單中選擇值,並將其用於doclisting頁面。我試圖在doclistings頁面上打印該值,但似乎沒有打印任何內容。我不確定這是否可行。任何幫助將非常感激。Django 1.6:將一個模板的下拉值傳遞給另一個
這裏是指數模板在那裏我已經下拉形式
<div class="signup">
<div class="form-group">
<form action="/doclistings/" method="post" >{% csrf_token %}
<select class="form-control" id="s1" name="selection">
<option><b>Find a Doctor...</b></option>
{% for value, text in form.selection.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Find Doctors</button>
這裏doclisting這裏我想從選擇
<h2>{{selection}}</h2>
這裏打印量模板是索引方法views.py
def index(request):
d = getVariables(request,dictionary={'page_name': "Home"})
if request.method == "POST":
form = DropdownSelectionForm(request.POST)
if form.is_valid():
selection = form.cleaned_data['selection']
return HttpResponseRedirect('/doclistings', 'selection')
else:
form = DropdownSelectionForm()
return render(request, 'meddy1/index.html', {'form': form})
這裏是在views.py中的文檔化方法
def doclistings(request):
d = getVariables(request)
doctors = Doctor.objects.all().order_by('-likes')
paginator = Paginator(doctors, 20) #Show 20 doctors per page
page = page = request.GET.get('page')
try:
doctors = paginator.page(page)
except PageNotAnInteger:
doctors = paginator.page(1)
except EmptyPage:
doctors = paginator.page(paginator.num_pages)
d['doctors'] = doctors
d['paginator'] = paginator
return render_to_response('meddy1/doclistings.html',d)
forms.py
MY_CHOICES = (
('Dermatologist', 'Dermatologist'),
('Dentist', 'Dentist'),
('Opthalmologist', 'Opthalmologist'),
)
class DropdownSelectionForm(forms.Form):
selection = forms.ChoiceField(choices=MY_CHOICES, widget = forms.Select)
urls.py
url(r'^index/$', views.index, name='index'),
url(r'^doclistings/$', views.doclistings, name='doclistings'),
你能告訴我的urls.py文件,並DropdownSelectionForm模式? – dhana
我已經添加了urls.py和表格 –
,但是您將'selection.save()'保存到數據庫中。什麼是型號名稱。 – dhana