Django - 从不是 django.form 的表单中获取数据

Lic*_*cia 6 html python forms django

我正在使用 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>
Run Code Online (Sandbox Code Playgroud)

这是我用来执行所有这些操作的 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)
Run Code Online (Sandbox Code Playgroud)

我想要做的是在我的视图“cpe_creation”中获取表单输入的值。我尝试从“请求”对象中获取信息,但我没能做到。也许我缺乏非常基本的 HTML 技能,但我认为表单信息会在请求正文中,但事实并非如此(或看起来不像)。

我也尝试过使用 Django 表单,但问题是,我无法非常精确地控制表单的外观。如果我创建一个输入,我不能告诉它它应该采用的大小或类似的东西。

因此,我的问题如下: - 如何从提交的表单中获取数据?- 我必须用 Django 的表单来做吗?如果我这样做,我如何控制输入的 css 类?

seb*_*ebb 6

模板中表单中的字段必须具有 name 属性才能将其传递给您的请求。

然后你将能够像这样在你的视图中得到它:

site_name = request.POST.get('site_name')