【问题标题】:Is there a way to link input fields from HTML to the ACTUAL form fields used in DJANGO有没有办法将 HTML 中的输入字段链接到 DJANGO 中使用的 ACTUAL 表单字段
【发布时间】:2018-11-11 05:18:41
【问题描述】:

现在我被困在两件事之间:要么我有一个有效的登录表单,但并不完全是你所说的视觉吸引力,另一个是看起来完美但没有任何作用的登录表单。 (我很了解情况)

如图所示,顶部的那个显然很漂亮(讽刺*),但我希望底部的丑陋的那个起作用。

对于顶部,我只使用了{{ form }} 标记,并且在某一时刻我的代码看起来像这样:

<form method='POST' action='' enctype='multipart/form-data'>{% csrf_token %}

    {{ form.username.label_tag }}
    {{ form.username }}
    {{ form.password.label_tag }}
    {{ form.password }}

   <input type='submit' class='btn btn-primary btn-block' value='{{ title }}' />
</form>

但这又一次没有给我想要的外观。截至目前,为了让非功能性(但漂亮)的表单查看底部,我正在使用bootstrap template signin 的代码(我的基本模板中包含的signin.css)。这就是我的 form.html 现在的样子:

{% extends 'accounts/base.html' %}
{% block main_content %}
    <div class="container">
        <div class='col-sm-6 col-sm-offset-3'>
        <form method='POST' action='' enctype='multipart/form-data' class="form-signin">{% csrf_token %}
            {{ form}}
            <h2 class="h3 mb-3 font-weight-normal">Please Sign In</h2>
            <input type="username" id="inputUsername" class="form-control" placeholder="Username" required autofocus>
            <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
            <input type='submit' class='"btn btn-lg btn-primary btn-block' value='{{ title }}' />
        </form>
        </div>
    </div>
{% endblock %}

我已经看到另一个 StackOverflow 帖子 such as this one 并尝试安装 django-widget-tweaks 但由于我使用的是 cloud9-ide 它没有工作

所以,如果有一种方法可以让我以某种方式在我的 Html 中实现表单标签或成功下载 django-tweaks,请告诉我,我将不胜感激。

提前致谢。

【问题讨论】:

  • link 是什么意思?您想以自己的方式使用 html 中的表单吗?
  • 感谢@seuling 的回复,很抱歉我第一次没有说清楚。我的意思是,我想让 html 输入字段(漂亮的)成为连接到我的数据库和登录的功能,就像我的 /admin/ 页面一样。使用 {{ form }} 有效,但正如您所见,它看起来不像底部的引导程序。
  • 可以上传使用{{ form }}的结果吗?你可以在浏览器中看到。 (即开发工具)
  • 我认为它不能很好地工作,因为它没有引导类,对吧?然后你就可以在表单中添加类

标签: python html django twitter-bootstrap


【解决方案1】:

如果您希望浏览器向它们发送数据,则需要在您的 html 输入中添加 name 属性。这与 Django 无关。

【讨论】:

  • 感谢@Daniel 的反馈,非常感谢。但是,您能否更清楚地了解我在输入字段的 name="" 标记中准确输入的内容?我是否放置了{{ form.username }}{{ form.password}} 标签?再次感谢
  • 没有。如果你想使用直接的 HTML,你需要&lt;input type='whatever'.... name='username'&gt; 等。
【解决方案2】:

删除action = '',然后重试:

<form method='post'>{% csrf_token %}

    {{ form.username.label_tag }}
    {{ form.username }}
    {{ form.password.label_tag }}
    {{ form.password }}

   <input type='submit' class='btn btn-primary btn-block' value='{{ title }}' />
</form>

或者在虚拟环境中安装 django-tweaks

pip install pipenv --user
cd
. .bashrc
cd workspace/mysite
pipenv --three
pipenv shell
pipenv install django django-widget-tweaks
python manage.py runserver

另外建议你将python升级到3.6版

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2013-09-19
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多