我是Django / Python的新手,并且开发了一个webapp,它根据主页上的选择/下拉菜单查询汽车数据库,并将用户发送到他们选择的汽车的详细信息页面。这是通过'制作'模型'和'修剪'来完成的。下拉框。我在理解和处理错误方面遇到的问题是,在点击提交表单的按钮时提交修剪ID。我知道需要发生什么我不知道如何使用Django视图和模板这样做。
通过IndexView返回的查询集填充 make 框。 模型框通过jQuery / Ajax填充,具体取决于在make框中选择的make。 修剪框的方式相同,根据模型框中选择的内容进行填充。我现在需要能够提交表格(实际上只需提交装饰ID,因为装饰是指定的车辆),以显示用户选择的车辆的详细信息。
这是我得到的错误:
Reverse for 'search' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?P<trim>\\d+)/$']
这基本上就是我的主页模板:
<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>
{% for 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 with 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 all trims for each make/model with 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>
&#13;
网址:
# 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 to change the model/trim boxes. Use make
# and model instead of pk to pass them into the view fxns.
url(r'^getmodels/(?P<make>\d+)/', views.getModels, name='getmodels'),
url(r'^gettrims/(?P<model>\d+)/', views.getTrims, name='gettrims'),
查看:
class IndexView(generic.ListView):
template_name = 'search/index.html'
# Name of the template varible.
context_object_name = 'allMakes'
def get_queryset(self):
# Return what will be listed in index.html
return Make.objects.all()
class DetailView(generic.DetailView):
model = Trim
template_name = 'search/detail.html'
# Take in the request and the ID for the selected Make
# and return something that we can work with in JS!
def getModels(request, make):
selectedMake = Make.objects.get(id=make)
# Get the models based on what was selected as the make.
models = Model.objects.all().filter(make=selectedMake)
# Translate to JSON
jsonModels = serializers.serialize("json", models)
# Return the 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 I make this redirect to the DetailView with the ID for the
# trim to 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)
答案 0 :(得分:1)
那个错误:
Reverse for 'search' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['search/(?P<trim>\\d+)/$']
表示您没有给出id参数(您将其称为trim)。它应该是参数或关键字参数的一部分。
我认为这是因为模板中没有修剪对象:
<form action="{% url 'search:search' trim.id %}"
该模板是否通过DetailView呈现?如果是这种情况,则可能会有以下情况:
<form action="{% url 'search:search' object.id %}"
答案 1 :(得分:0)
我在网址模式中撒谎的问题的解决方案。取出搜索网址的ID部分,然后更改视图:
def search(request):
trim = request.GET['selectedTrim']
selectedTrim = get_object_or_404(Trim, id=trim)
context = {'trim': selectedTrim}
return render(request, 'search/detail.html', context)
和我的表格行动:
action="{% url 'search:search' %}"
我能够通过请求方法将选定的修剪成功传递给视图,并通过 url 正则表达式变量不,这导致由于所选的错误修剪ID实际上不是通过提供给模板的上下文存在(我认为)。