簡體   English   中英

如何在Django的FileField / CharField / BooleanField / Upload按鈕上使用引導程序

[英]How to use bootstrap on Django's FileField/CharField/BooleanField/Upload button

我有以下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})

表格

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>

最后看起來像這樣:

在此處輸入圖片說明

我想用Bootstrap渲染它。 我該怎么做?

您需要以表格形式進行

像這樣的東西:

表格

state_list = State.objects.filter().order_by('name')

class MyForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)

    self.fields['state'] = forms.ModelChoiceField(
        required=True,
        error_messages={'required': 'State / Province is Required!'},
        label='State',
        widget=forms.Select(attrs={
            'class': 'form-control dropdown-toggle input-lg',
            'data-toggle': 'dropdown',
            'aria-expanded': 'false',
            'role': 'menu',
            'required': 'required',
            'oninvalid': "this.setCustomValidity('State / Province is Required!')",
            'onchange': "this.setCustomValidity('')",
        }),
        queryset=state_list,
        empty_label='Select State / Province',
    )

    .... more fields 

class Meta:
    model = MyModel
    fields = [
        'myfield',
    ]

template.html

<div class="form-group">
  <label for="state" class="col-sm-4 control-label">State</label>
  <div class="col-sm-8">
    {{ form.state }}
  </div>
</div>

你可以看到在引導做了同樣的渲染Django的形式在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM