“帮助我成为django的新人”
模板中的<form method="get" role="search">
<input type="text" name="search" class="form-control" placeholder="Search">
<button type="submit">submit</botton>
</form>
点击表单提交后,显示搜索结果,但文本框值为空。我想在那里搜索关键词... 如何使用java脚本实现这一点..?
答案 0 :(得分:0)
1-使用手册
只需将request.GET.search放入值中,无需在视图中更改
<form method="GET">
Search: <input type="text" name="search"
value="{{ request.GET.search }}">
<input type="submit" value="Search">
</form>
2 - 使用表格
<强> forms.py 强>
from django import forms
class SearchForm (forms.Form) :
search = forms.CharField(max_length=100)
模板
<form method="GET">
{{ form.as_p }}
<input type="submit" value="Search">
</form>
<强> views.py 强>
from django.shortcuts import render
from app_name.forms import SearchForm
def myview(request):
if request.GET.get('search'):
request.session['search'] = request.GET.get('search')
#do query searching here
form = SearchForm(initial={
'search': request.session.get('search')
})
return render(request, 'search_page.html', {
'form': form
})
答案 1 :(得分:0)
如果您正在使用表单对象,那么在您的视图中,您可以执行以下操作:
def myview(request):
if request.method=="GET":
form = SearchForm(request.GET)
return render(request, 'searchPage.html', {'form':form,})