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

标签 python html django forms

我正在使用 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/

相关文章:

python - 小数(0.19)会产生很长的数字吗?

Python 找到 pandas 列中日期之间的差异?

python - python 中计数器结果的最小值

Python - 操作方法 - Big Query 异步任务

javascript - 轮播 Bootstrap 在 IE 10 中不起作用

HTML, CSS - 图像中的图像,怎么做?

html - 如何设置字体系列?

python - 如何在 python 中创建 3D 高度图

python - 无法将 DateTimeField 的默认值设置为 "0000-00-00 00:00:00"

django - 在 Django 管理中使内联成为条件