python - How to use bootstrap on Django's FileField/CharField/BooleanField/Upload button -
i have following django setup.
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() # run code # given parameters q.run_code1() # show method1_result 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 not show # when form displayed exclude = ['state','clustering_state','ip_address','creationtime']
html
setting mycool_app/method1.html
file:
<!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>
finally looks this:
i'd render bootstrap. how can it?
you need in forms
something this:
form.py
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 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 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>
you can see same rendered django form done in bootstrap here
Comments
Post a Comment