javascript - Django how to properly submit form and handle request? -
i'm new django/python , working on webapp queries db of cars based on selects/dropdowns on home page , sends user details page car selected. done through 'make', 'model', , 'trim' dropdown box. i'm having trouble understanding , getting errors doing, submitting trim id upon clicking button submit form. know needs happen don't know how using django views , templates.
the make box populated via returned queryset indexview. model box populated via jquery/ajax dependent upon make selected in make box. trim box same way, populated dependent upon selected in model box. need able submit form (which needs submit trim id because trim specified car) in order show details car user has chosen.
this error getting:
reverse 'search' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?p<trim>\\d+)/$']
this homepage template looks like:
<form action="{% url 'search:search' trim.id %}" class="form-inline" id="dropdowntext"> <div class="form-group" id="dropdowngroup"> <span style="display:inline-block" class="selectbox"> <select class="form-control" id="make"> <option value='empty'></option> {% make in allmakes %} <option value="{{ make.id }}">{{ make.name }}</option> {% endfor %} </select> <label for="make" style="display:block">make</label> </span> <span style="display:inline-block" class="selectbox"> <select class="form-control" id="model" disabled> <!-- list each make's model's name jq. --> </select> <label for="model" style="display:block">model</label> </span> <span style="display:inline-block" class="selectbox"> <select class="form-control" id="trim" name="selectedtrim" disabled> <!-- list trims each make/model jq. --> </select> <label for="trim" style="display:block">trim</label> </span> </div> <div class="text-center"> <button type="submit" class="btn btn-default" id="go">go!</button> </div> </form>
urls:
# index url(r'^$', views.indexview.as_view(), name='index'), # /trimid url(r'^(?p<pk>\d+)/$', detailview.as_view(), name='detail'), # /search url(r'^search/(?p<trim>\d+)/$', views.search, name='search'), # uses ajax/json change model/trim boxes. use make # , model instead of pk pass them view fxns. url(r'^getmodels/(?p<make>\d+)/', views.getmodels, name='getmodels'), url(r'^gettrims/(?p<model>\d+)/', views.gettrims, name='gettrims'),
views:
class indexview(generic.listview): template_name = 'search/index.html' # name of template varible. context_object_name = 'allmakes' def get_queryset(self): # return listed in index.html return make.objects.all() class detailview(generic.detailview): model = trim template_name = 'search/detail.html' # take in request , id selected make # , return can work in js! def getmodels(request, make): selectedmake = make.objects.get(id=make) # models based on selected make. models = model.objects.all().filter(make=selectedmake) # translate json jsonmodels = serializers.serialize("json", models) # return json. return httpresponse(jsonmodels, content_type='application/javascript') def gettrims(request, model): selectedmodel = model.objects.get(id=model) trims = trim.objects.all().filter(model=selectedmodel) jsontrims = serializers.serialize("json", trims) return httpresponse(jsontrims, content_type='application/javascript') # can make redirect detailview id # trim show details for? def search(request, trim): selectedtrim = get_object_or_404(trim, pk=trim) context = {'trim': selectedtrim} return render(request, 'search/detail.html', context)
that error:
reverse 'search' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?p<trim>\\d+)/$']
means didn't give id parameter (you called trim). should part of arguments or keyword arguments.
i assume that's because there no trim object in template here:
<form action="{% url 'search:search' trim.id %}"
is template rendered via detailview? if that's case following work:
<form action="{% url 'search:search' object.id %}"
Comments
Post a Comment