Jquery 多值错误。异常值:';蒂波';
我试图通过过滤器将按钮的值发送到搜索页面。此页面由多个URL组成,每个URL用于使用一种过滤器进行查询。My view POST收集“type”的值并将其发送到getType视图,但当它接收到该值时,会得到一个多值KeyError。如何将类型筛选器提交到搜索页面 views.py index.html /url.pyJquery 多值错误。异常值:';蒂波';,jquery,django,filter,django-views,Jquery,Django,Filter,Django Views,我试图通过过滤器将按钮的值发送到搜索页面。此页面由多个URL组成,每个URL用于使用一种过滤器进行查询。My view POST收集“type”的值并将其发送到getType视图,但当它接收到该值时,会得到一个多值KeyError。如何将类型筛选器提交到搜索页面 views.py index.html /url.py 来自django.contrib导入管理 从django.url导入路径 从django.conf.url导入url,包括 从wallacar_app.views导入索引,Coch
来自django.contrib导入管理
从django.url导入路径
从django.conf.url导入url,包括
从wallacar_app.views导入索引,Cochedetales,完成
从django.conf导入设置
从django.conf.url.static导入静态
URL模式=[
路径('admin/',admin.site.url),
路径(“”,Index.as_视图(template_name=“wallacar\u app/Index.html”),name=“Index”),
路径(“wallacar\u app/”,包括((“wallacar\u app.url”,“wallacar\u app”),namespace=“wallacar\u app”),
路径('detalles/',cochedetales.as_view(),name=“detalles”),
路径('done/',done,name=“done”)
]
tipo不是任何表单输入名称的名称。请先检查你的邮寄表格。我已经检查过,但没有找到任何名为“tipo”的表单控件。这是真的,我有type\u car,而不是“tipo”,但是如果我在视图中将“tipo”更改为“type\u car”,我会遇到同样的问题。
class Index(ListView):
model = Coche, CocheBrand, CocheModel, CocheType, CocheDoors, CocheGearshift, Localidad
context_object_name='coche_list'
form_class = IndexSearch
def get_queryset(self):#
coches = Coche.objects.filter(reserved=False, sold=False).order_by('-precio')[:2]
total_coches = Coche.objects.filter(reserved=False, sold=False).count()
queryset = {'coches':coches,'total_coches':total_coches}
return queryset
def get_context_data(self):
context = super(Index, self).get_context_data()
context['marca'] = CocheBrand.objects.all()
context['modelo'] = CocheModel.objects.all()
context['tipos'] = CocheType.objects.all()
return context
def CocheList(request): #LOAD SEARCH PAGE
return render(request,'wallacar_app/cars.html',{} )
class CocheListing(ListAPIView):
pagination_class = StandardResultsSetPagination
serializer_class = CocheSerializers
def get(self, request):
if request.POST['tipo']:
getType(request)
return render(request, 'wallacar_app/cars.html')
def get_queryset(self):
queryList = Coche.objects.all()
brand = self.request.query_params.get('brand',None)
model_name = self.request.query_params.get('model_name',None)
type_car = self.request.query_params.get('type_car',None)
if brand:
queryList = queryList.filter(brand = brand)
if model_name:
queryList = queryList.filter(model_name = model_name)
if type_car :
queryList = queryList.filter(type_car = type_car)
def getType(request):
if request.POST['tipo'] and not request.method == 'GET':
type_car = Coche.objects.filter(type_car=request.POST['tipo']).order_by('type_car').values_list('type_car').distinct()
type_car = [i[0] for i in list(type_car)]
data = {'type_car':type_car}
return JsonResponse(data,status=200)
# return render(request, 'wallacar_app/cars.html', data)
if request.method == "GET" and request.is_ajax() and not request.POST['tipo']:
type_car = Coche.objects.exclude(type_car__isnull=True).exclude(type_car__exact='').order_by('type_car').values_list('type_car').distinct()
type_car = [i[0] for i in list(type_car)]
data = {'type_car':type_car}
return JsonResponse(data,status=200)
{% block content %}
<div class="site-section pt-0 pb-0 bg-light">
<div class="row">
<div class="col-12">
<form action="wallacar_app/lista/tipo" method="POST" class="trip-form">
{% csrf_token %}
<div class="row align-items-center mb-4">
<div class="col-md-6">
<h3 class="m-0">TIPOS DE COCHES</h3>
</div>
</div>
<div class="row">
<div class="form-group" style="width: 100%;">
<label for="tipos">Tipo</label><br>
{% for tipo in tipos %}
<button name="type_car" value="{{ tipo.type_car }}" style="width: 25%;" id="{{ tipo.type_car }}" class="form-control px-3 btn-primary btn">{{ tipo.type_car }}</button>
{% endfor %}
</div>
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-2 col-2">
<div class="form-group">
<label for="type_car">TIPO DE COCHE</label>
<select class="form-control" id="type_car"
url = "{%url 'wallacar_app:get_type' %}">
<option value='all' selected>TODOS LOS TIPOS</option>
</select>
</div>
</div>
getAPIData();
getType();
$('#type_car').on('change', function () {
// get the api data of updated variety
if(this.value)
send_data['type_car'] = this.value;
else
if(this.value == "all")
send_data['type_car'] = "";
else
send_data['type_car'] = this.value;
getAPIData();
});
function putTableData(result) {
// creating table row for each result and
// pushing to the html cntent of table body of listing table
let row;
if(result["results"].length > 0){
$("#no_results").hide();
$("#list_data").show();
$("#listing").html("");
$.each(result["results"], function (a, b) {
if (b.reserved === true && b.sold === false){
row = /* is a test */
'<li>'+
'<span>Tipo: </span>'+
'<span class="spec">'+ b.type_car+'</span>'+
'</li>'
function getAPIData() {
let url = $('#list_data').attr("url")
$.ajax({
method: 'GET',
url: url,
data: send_data,
beforeSend: function(){
$("#no_results h5").html("Cargando...");
},
success: function (result) {
putTableData(result);
},
error: function (response) {
$("#no_results h5").html("Algo no funciona....");
$("#list_data").hide();
}
});
}
function getType() {
// fill the options of provinces by making ajax call
// obtain the url from the provinces select input attribute
let url = $("#type_car").attr("url");
// makes request to getProvince(request) method in views
$.ajax({
method: 'GET',
url: url,
data: {},
success: function (result) {
type_option = "<option value='all' selected>TODOS LOS TIPOS</option>";
$.each(result["type_car"], function (a, b) {
type_option += "<option>" + b + "</option>"
});
$("#type_car").html(type_option)
},
error: function(response){
console.log(response)
}
});
}
from django.urls import path
from wallacar_app.views import *
urlpatterns = [
path('lista/', CocheList),
path('lista/tipo', tipoIndex),
path("listado_coches/", CocheListing.as_view(), name = 'listing'),
path("ajax/marca/", getBrand, name = 'get_brand'),
path("ajax/model/", getModel, name = 'get_model'),
path("ajax/location/", getLocation, name = 'get_location'),
path("ajax/type/", getType, name = 'get_type'),
]
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from wallacar_app.views import Index, CocheDetalles, done
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',Index.as_view(template_name="wallacar_app/index.html"),name="index"),
path("wallacar_app/",include(("wallacar_app.urls","wallacar_app"), namespace = "wallacar_app")),
path('detalles/<str:pk>/',CocheDetalles.as_view(),name="detalles"),
path('done/<str:pk>/', done, name="done")
]