[英]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.