我有一个dropdwon列表。.当我单击该列表时,我得到的值类似于某些名称。.现在,我希望该名称作为Django中的url传递
Views.py
@csrf_exempt
def forecasting(request):
Category = category.objects.all()
return render(request, "myapp/casting.html",{'Category':Category})
@csrf_exempt
def Prediction(request,name):
Category1 = category.objects.get(Category=name)
print(Category1.Category)
return render(request, "myapp/casting.html", {'Category1': Category1})
Models.py
class category(models.Model):
Category = models.CharField(max_length=50)
def __str__(self):
return self.Category
HTML文件
<form id="myform" method="post" name="myform" action="/Prediction/{{Category1.Category}}">
<div class="col-lg-3 col-md-3">
<h3> Select Category</h3>
</div>
<div class="col-lg-3 col-md-3">
{%csrf_token%}
<select id="Category" name="catagery_id" onchange="myform.submit()">
<option value="">Select</option>
{% for Category in Category %}
<option value="{{ Category.Category }}">{{ Category.Category }}</option>
{% for message in messages %}
<div>
<p>{{message|safe}}</p>
</div>
{% endfor %}
{% endfor %}
</select>
</div>
<ul class="messages">
{% for message in messages %}
<p
{% if message.tags %} class="{{ message.tags }}" {% endif %}>{{ message }}</p>
{% endfor %}
</ul>
<div class="col-lg-3 col-md-3">
</div>
</form>
我不知道该怎么做。.因为当我提交表格时会有价值。.我是新来的,请帮帮我
答案 0 :(得分:0)
我认为您希望传递表单操作下拉菜单中选择的值,您可以使用jquery进行尝试
$("#Category").on("change",function(){
action_url = '/Prediction/'+$(this).val()
$('#myform').attr('action', action_url);
$('#myform').submit()
})
在这种情况下,您必须通过删除jquery来提交表单
onchange="myform.submit()"
替换以下代码
{% for Category in Category %}
{% if Category.Category == Category1.Category %}
<option value="{{ Category.Category }}" selected>{{
Category.Category }
</option>
{% else%}
<option value="{{ Category.Category }}">{{ Category.Category }
</option>
{% endif %}
{% for message in messages %}
<div>
<p>{{message|safe}}</p>
</div>
{% endfor %}
{% endfor %}