Python 带有引导形式外键的Django下拉列表
我正在开发我的第一个Django网站,我想使用一个外键下拉式引导形式。我可以通过手动输入外键号(例如“1”)来添加外键,表单将正常工作。但我无法为链接下拉列表插入正确的语法。在下面找到my current models.py/views.py和html 我有以下客户模型Python 带有引导形式外键的Django下拉列表,python,html,django,twitter-bootstrap,Python,Html,Django,Twitter Bootstrap,我正在开发我的第一个Django网站,我想使用一个外键下拉式引导形式。我可以通过手动输入外键号(例如“1”)来添加外键,表单将正常工作。但我无法为链接下拉列表插入正确的语法。在下面找到my current models.py/views.py和html 我有以下客户模型 class Customer(models.Model): customer_type = models.ForeignKey(CustomerType) customer_name = models.CharF
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)
请参见下面的my 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>
{%csrf_令牌%}
客户名称
客户类型
添加客户
非常感谢您提供的任何帮助。首先,我建议您使用,您不会后悔,它与bootstrap非常匹配。切换之后,我不明白我是如何长期使用内置的django表单的 也就是说,这里演示了如何将引导下拉列表用作输入项 下拉按钮w/.form控件
<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>
选择一个
{%t在类型%}
{%enfor%}
然后在视图中确保传入
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>
{%csrf_令牌%}
客户名称
客户类型
{%for客户类型中的类型{u类型%}{{type}{%endfor%}
添加客户
我改变了
<option value="{{type}}">{{type}}</option>
您的元素为空。你从那里剪下代码了吗?是的,那里现在没有代码了。这就是我想要调用customertype表的内容到select中的原因。但我不知道语法。请阅读有关ModelForms的内容。django站点教程非常值得花几个小时来探索。现在CustomerType中的值可以从select中获得,谢谢!但是,表单无法保存,因为我认为它试图保存customer_类型的值,而不是该值的外键。我是否需要在表单中调整/添加一些内容。py?谢谢你提供的crispy表单建议,我会研究的!
<option value="{{type.id}}">{{type}}</option>
<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>
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))