django - django 表单值为空
问题描述
我的主页显示表单值有问题。尽管在搜索页面上的值很好。这里可能是什么问题?我将相同的表单从搜索页面复制到主页,但仍然无效,值为空。任何提示表示赞赏,谢谢!
搜索页面:
<form action="{% url 'search' %}">
<div class='flx around px'>
<input type='text' id='search-keyword' class='search-keyword form-control'
placeholder='keywords (pool,garage)'>
<select name='area' id='search-area' class='form-control'>
<option selected="true" disabled="disabled" selected>Area(All)</option>
{% for key,value in area_choices.items %}
<option value="{{ key }}" {% if key == values.area %} selected {% endif %}>{{ value }}</option>
{% endfor%}
</select>
</div>
<div class='flx around'>
<select name="bedrooms" class="form-control">
<option selected="true" disabled="disabled" selected>Bedrooms(All)</option>
{% for key,value in bedroom_choices.items %}
<option value = "{{ key }}"
{% if key == values.bedrooms %}
selected
{% endif %}
>{{ value }}</option>
{% endfor %}
</select>
<select name="price" class="form-control ">
<option selected="true" disabled="disabled" selected>Price(All)</option>
{% for key,value in price_choices.items %}
<option value = "{{ key }}"
{% if key == values.price %}
selected
{% endif %}
>{{ value }}</option>
{% endfor %}
</select>
</div>
<button type = "submit" class='btn fntmk my2 size'>Search <i class="fas fa-search size"></i></button>
</form>
主页:
<form action="{% url 'search' %}">
<div class = 'flx around px iphone'>
<input name = 'keywords' type = 'text' id = 'search-keyword' class = 'search-keyword form-control' placeholder = 'keywords (pool, garage)'>
<select name = 'area' id = 'search-area' class = 'form-control'>
{% for key,value in area_choices.items %}
<option value = "{{ key }}">{{ value }}</option>
{% endfor %}
</select>
</div>
<div class = 'flx around iphone'>
<select name="bedrooms" class="form-control">
<option selected="true" disabled="disabled" selected>Bedrooms(All)</option>
{% for key,value in bedroom_choices.items %}
<option value = "{{ key }}">{{ value }}</option>
{% endfor %}
</select>
<select name="price" class="form-control ">
<option selected="true" disabled="disabled" selected>Price(All)</option>
{% for key,value in price_choices.items %}
<option value = "{{ key }}">{{ value }}</option>
{% endfor %}
</select>
</div>
<button type = "submit" class = 'btn fntmk my2 size'>Search <i class="fas fa-search size"></i></button>
</form>
看法:
from .choices import price_choices, bedroom_choices, area_choices
def search(request):
queryset_list = Oglas.objects.order_by('-list_date')
# keywords
if 'keywords' in request.GET:
keywords = request.GET['keywords']
if keywords:
queryset_list = queryset_list.filter(description__icontains = keywords)
# Area
if 'area' in request.GET:
area = request.GET['area']
if area:
queryset_list = queryset_list.filter(area__iexact = area)
# rooms
if 'bedrooms' in request.GET:
bedrooms = request.GET['bedrooms']
if bedrooms:
queryset_list = queryset_list.filter(bedrooms__lte=bedrooms)
# price
if 'price' in request.GET:
price = request.GET['price']
if price:
queryset_list = queryset_list.filter(price__lte = price)
context = {
'area_choices' : area_choices,
'bedroom_choices' : bedroom_choices,
'price_choices' : price_choices,
'listings' : queryset_list,
'values' : request.GET,
}
return render(request, 'pages/search.html', context)
解决方案
表格的处理方式完全不同。您将模板与表单处理混为一谈。先看看文档。您需要将一个表单对象添加到您的上下文中。
从提到的文档中提取:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
推荐阅读
- python - 如何用 seaborn 绘制阴影误差带?
- .net-core - MassTransit AzureServieBus InvalidOperationException
- sql - SQL拆分消费满月
- c++ - 为什么 gcc 对包含标头比 clang 更严格
- flutter - 在 Widget 的 deactivate 方法中使用 provider 方法
- sql - 如何撤消 PostgreSQL 中的更改
- mysql - vue端无法显示express res.send()自定义错误信息
- powershell - Powershell remove-item 不会删除文件
- mysql - MySql 代码用于在两个表中返回特定日期的值,然后找到两者的差异
- travis-ci - 从特定分支上的所有构建下载原始构建日志