我是Django的新手,并尝试从索引页面的下拉菜单中获取用户选择的值,并在文档列表页面中使用它。我正在尝试在doclistings页面上打印该值,但它似乎没有打印任何东西。我不确定这是否可能。任何帮助将非常感激。
这是我在下拉表格中的索引模板
<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'),
答案 0 :(得分:2)
如果您没有模型,可以使用django session
来实现假设您想要基于数据库的会话(Django还提供基于文件的会话和基于缓存的会话):
Open settings.py and find MIDDLEWARE_CLASSES. Add 'django.contrib.sessions.middleware.SessionMiddleware' to the list.
Find INSTALLED_APPS in the same file and add 'django.contrib.sessions' there.
Run manage.py syncdb from the command line.
初始设置完成后,您可以在视图中使用request.session在请求之间存储信息。
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']
request.session["selection"] = request.POST['selection']
return HttpResponseRedirect('/doclistings')
else:
form = DropdownSelectionForm()
return render(request, 'meddy1/index.html', {'form': form})
在doclist视图中,
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')
print request.session["selection"]
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)
对于您可以使用request.session对象执行的其他操作,请参阅documentation。
答案 1 :(得分:2)
首先,你正在做一些艰难/错误的事情。
{{form.as_p}}
。 POST
,这在更改数据库中的内容时使用。你应该使用GET
reverse
获取网址的名称。url(r'^doclistings/(?P<group_name>\w+)/$', views.doclistings, name='doclistings')
,您的函数也将获得group_name
arg(您应该将其添加到函数signaure中)。另一种选择是将其作为GET
param /doclistings?group=xyz
传递,并在您的函数中使用request.GET来检查您是否拥有该组,如果有,则进一步过滤。更多的挑剔,咨询:
HTH