[英]Django Dropdown with Foreign key in Bootstrap Form
我正在开发我的第一个Django网站,我想在Bootstrap表单中使用外键下拉。 我可以通过手动输入外键号(例如“1”)来添加外键,表单将起作用。 但我无法为链接下拉列表插入正确的语法。 在下面找到我当前的models.py / views.py和html
我有以下客户模型;
class Customer(models.Model):
customer_type = models.ForeignKey(CustomerType)
customer_name = models.CharField(max_length=120, null=True, blank=True)
def __unicode__(self):
return smart_unicode(self.customer_name)
使用CustomerType
class CustomerType(models.Model):
customer_type = models.CharField(max_length=120, null=True, blank=True)
def __unicode__(self):
return smart_unicode(self.customer_type)
请参阅下面的views.py;
def customeradd(request):
form = CustomerAddForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/customeroverview/')
return render_to_response("customer-add.html",
locals(),
context_instance=RequestContext(request))
最后是我的HTML;
<div class="col-lg-12">
<form class="form-horizontal" method="POST" action=''> {% csrf_token %}
<div class="form-group">
<label for="customer_name" class="col-sm-2 control-label">Customer Name</label>
<div class="col-sm-10">
<input id="customer_name" name="customer_name" type="text" class="form-control" >
</div>
</div>
<div class="form-group">
<label for="customer_type" class="col-sm-2 control-label">Customer Type</label>
<div class="col-sm-10">
<select class="form-control" id="customer_type" name="customer_type"></select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10"></div>
<div class="col-sm-2">
<button type='submit' class="btn btn-success btn-block">Add Customer</button>
</div>
</div>
</form>
</div>
任何帮助深表感谢。
首先,我想建议使用django-crispy-forms ,你不会后悔,它与bootstrap配对非常好。 我换了之后,我不明白我如何使用内置的django表格这么久。
话虽这么说,这里有一个jsfiddle演示如何使用bootstrap下拉列表作为输入项。
下拉按钮w / .form-control <div class="panel panel-default"> <div class="panel-body"> <div class="btn-group"> <button type="button" class="btn btn-default dropdown-toggle form-control" data-toggle="dropdown"> <span data-bind="label">Select One</span> <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu"> {% for t in types %} <li><a href="#">{{ type }}</a></li> {% enfor %} </ul> </div> </div> </div>
然后在您的视图中确保传入types = CustomerType.objects.all()
要在您的下拉列表中添加客户类型,您必须从表中收集所有客户类型:
def customeradd(request):
form = CustomerAddForm(request.POST or None)
customer_types = CustomerType.objects.all()
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/customeroverview/')
return render_to_response("customer-add.html",
{'customer_types': customer_types},
context_instance=RequestContext(request))
(不要忘记在模型中包含CustomerType)
然后你必须将它们包含在你的元素中
<div class="col-lg-12">
<form class="form-horizontal" method="POST" action=''> {% csrf_token %}
<div class="form-group">
<label for="customer_name" class="col-sm-2 control-label">Customer Name</label>
<div class="col-sm-10">
<input id="customer_name" name="customer_name" type="text" class="form-control" >
</div>
</div>
<div class="form-group">
<label for="customer_type" class="col-sm-2 control-label">Customer Type</label>
<div class="col-sm-10">
<select class="form-control" id="customer_type" name="customer_type"> {%for type in customer_types%}<option value="{{type}}">{{type}}</option>{%endfor%}</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10"></div>
<div class="col-sm-2">
<button type='submit' class="btn btn-success btn-block">Add Customer</button>
</div>
</div>
</form>
</div>
我变了
<option value="{{type}}">{{type}}</option>
至
<option value="{{type.id}}">{{type}}</option>
现在它保存了Key但显示了ValueText
html部分的最终结果
<div class="form-group">
<label for="customer_type" class="col-sm-2 control-label">Customer Type</label>
<div class="col-sm-10">
<select class="form-control" id="customer_type" name="customer_type">
{%for type in customer_types%}<option value="{{type.id}}">{{type}}</option>{%endfor%}
</select>
</div>
</div>
views.py的最终结果
def customeradd(request):
form = CustomerAddForm(request.POST or None)
customer_types = CustomerType.objects.all()
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/customeroverview/')
return render_to_response("customer-add.html",
{'customer_types': customer_types},
context_instance=RequestContext(request))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.