如何在Django的FileField / CharField / BooleanField / Upload按钮上使用bootstrap

时间:2015-09-30 02:00:31

标签: python html css django twitter-bootstrap

我有以下Django设置。

models.py

class Method1(models.Model):
    inputfile_param = models.FileField()
    species_param   = models.CharField(max_length=20, choices=(('mouse', 'Mouse'), ('human','Human')))
    norm_mode_param = models.CharField(max_length=20, choices=(('genecount_norm', 'Gene Count'), ('totalscore_norm','Total Score')))
    logscale_param  = models.BooleanField(default=False)

views.py

from .forms import Method1Form
def method1_input(request):
    if request.method == 'POST':
        form = Method1Form(request.POST, request.FILES)
        # Handle file upload
        if form.is_valid():
            q = form.save()
            q.save()
            # This is where we run code
            # with the given parameters
            q.run_code1()

            # This will show the method1_result to be executed.
            return HttpResponseRedirect(reverse('method1-result', kwargs={'id': q.id }))

    else:
        form = Method1Form()

    return render(request, 'mycool_app/method1.html', {'form':form})

forms.py

from .models import Method1
class Method1Form(forms.ModelForm):
    class Meta:
        model = Method1
        # Exclude means what not to show
        # when form is displayed
        exclude = ['state','clustering_state','ip_address','creationtime']

HTML

设置mycool_app/method1.html文件:

<!DOCTYPE html>
<html>
<body>
      <form  action="{% url 'method1-input' %}" method="post" enctype="multipart/form-data">
          {% csrf_token %}
          <p> {{form.inputfile_param.errors}} </p>
          <p> {{form.inputfile_param}} </p>
          <p> {{form.species_param }} </p>
          <p> {{form.norm_mode_param }} </p>
          <p> Log-scaling {{form.logscale_param}}</p>

      <p><input type="submit" value="Upload" /></p>
</body>
</html>

最后它看起来像这样:

enter image description here

我想用Bootstrap渲染它。我该怎么办?

1 个答案:

答案 0 :(得分:-1)

您需要在表单中执行此操作 这样的事情: form.py state_list = State.objects.filter()。order_by(&#39; name&#39;) class MyForm(forms.ModelForm): def __init __(self,* args,** kwargs):     super(MyForm,self).__ init __(* args,** kwargs)     self.fields [&#39; state&#39;] = forms.ModelChoiceField(         需要=真,         error_messages = {&#39; required&#39;:&#39;州/省是必需的!&#39;},         标签=&#39;状态&#39 ;,         插件= forms.Select(ATTRS = {             &#39; class&#39;:&#39; form-control dropdown-toggle input-lg&#39;,             &#39;数据切换&#39;:&#39;下拉&#39;,             &#39; aria-expanded&#39;:&#39; false&#39;,             &#39;角色&#39;:&#39; menu&#39;,             &#39;必需&#39;:&#39;必需&#39;,             &#39; oninvalid&#39;:&#34; this.setCustomValidity(&#39;州/省是必需的!&#39;)&#34;,             &#39; onchange&#39;:&#34; this.setCustomValidity(&#39;&#39;)&#34;,         }),         查询集= state_list,         empty_label =&#39;选择州/省&#39;,     )     ....更多的领域 类Meta:     model = MyModel     fields = [         &#39; MyField的&#39 ;,     ] template.html &lt; div class =&#34; form-group&#34;&gt;   &lt; label for =&#34; state&#34; class =&#34; col-sm-4 control-label&#34;&gt; State&lt; / label&gt;   &lt; div class =&#34; col-sm-8&#34;&gt;     {{form.state}}   &LT; / DIV&GT; &LT; / DIV&GT; 你可以在这里看到在Bootstrap中完成的相同渲染的Django表单