我正在使用 Django 来制作一个非常简单的 Web 应用程序。
我目前正在尝试拥有一系列表格,以便用户逐位完成信息。 我首先尝试只用 HTML 来做,因为我想真正掌握演示文稿。这是我的代码。
我有两个模板,create_site.html 和 create_cpe.html。我需要从第一页获取信息,以便知道在第二页要问什么。
这是我的 create_site.html
<body>
<form action="{% url 'confWeb:cpe_creation' %}" method="post" class="form-creation">
{% csrf_token %}
<div class = "form-group row">
<label for="site_name" class="col-xs-3 col-form-label">Name of theSite</label>
<div class="col-xs-9">
<input id="site_name" class="form-control" placeholder="Name" required="" autofocus="" type="text">
</div>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Créer</button>
</form>
</body>
这是我用来完成所有这些的 views.py :
def site_creation(request):
template = loader.get_template('confWeb/create_site.html')
return HttpResponse(template.render(request))
def cpe_creation(request):
if request.method == "POST" :
print(request.POST)
我想做的是在我的 View “cpe_creation”中获取表单输入的值。我尝试从“请求”对象获取信息,但我没能做到。也许我缺乏非常基本的 HTML 技能,但我认为表单信息会在请求正文中,但事实并非如此(或者看起来不像)。
我也尝试过使用 Django 表单,但问题是,我无法非常精确地控制表单的外观。如果我创建一个输入,我无法告诉它它应该采用的大小或类似的东西。
因此,我的问题如下: - 如何从提交的表单中获取数据? - 我必须用 Django 的表格来做吗?如果我这样做,我如何控制输入的 css 类?
最佳答案
模板中表单中的字段必须具有名称属性才能将其传递给您的请求。
然后你将能够像这样在你的 View 中获取它:
site_name = request.POST.get('site_name')
关于python - Django - 从不是 django.form 的表单中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39533387/