【发布时间】:2015-12-27 16:14:44
【问题描述】:
我有以下 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>
最后看起来像这样:
我想用 Bootstrap 渲染它。我该怎么做?
【问题讨论】:
-
Method1Form定义在哪里? -
@KlausD。在
forms.py。查看我的更新。 -
除了下面的答案之外,您还必须将引导 css 包含到您的 HTML 中。
标签: python html css django twitter-bootstrap