简体   繁体   English

应用css样式在Django中形成项目

[英]Applying css styles to form items in Django

I have the following form in forms.py 我在forms.py中有以下表格

from django import forms
class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

In my templates/login.html file I have the following: 在我的templates / login.html文件中,我有以下内容:

{% load staticfiles %}

<div>
     <form action="" method="post" style="">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="SignIn">
     </form>
</div>

I want use the bootstrap tool for the ui, [ http://getbootstrap.com/examples/signin/] for which I will should load some js and css static files inside login.html 我想为ui使用bootstrap工具,[ http://getbootstrap.com/examples/signin/]我将在login.html中加载一些js和css静态文件

How to can I apply the css effects to my login form, when I get the form just with the template tag {{ form.as_p }} ? 当我使用模板标签{{ form.as_p }}获取表单时,如何将css效果应用于我的登录表单?

This is easy to do in class based forms. 这在基于班级的表格中很容易做到。 Example from my project where form-control is a css class. 我的项目示例,其中form-control是一个css类。

from django.forms import ModelForm, TextInput

class ServerForm(ModelForm):

    class Meta:
        model = Server
        fields = ['name', 'ip', 'port']
        widgets = {
            'name': TextInput(attrs={'class': 'form-control'}),
            'ip': TextInput(attrs={'class': "form-control"}),
            'port': TextInput(attrs={'class': 'form-control'}),
            }

Then the template I have looks like: 然后模板我看起来像:

{% for field in form %}
    <div class="form-group">
        {{ field.label_tag }} {{ field }}
    </div>
{% endfor %}

Which generates 哪个生成

<form method="POST">
    <input type="hidden" name="csrfmiddlewaretoken" value="xxxxxx">
    <div class="form-group">
        <label for="id_name">Name:</label>
        <input class="form-control" id="id_name" maxlength="64" name="name" type="text">
    </div>
    <div class="form-group">
        <label for="id_ip">Ip:</label>
        <input class="form-control" id="id_ip" maxlength="20" name="ip" type="text">
    </div>
    <div class="form-group">
        <label for="id_port">Port:</label> <input class="form-control" id="id_port" name="port" type="text">
    </div>
    <div class="form-group">
        <button type="submit" class="save btn btn-default">Add/Edit Server</button>
    </div>
</form>

The key is the widgets field of the Meta class. 关键是Meta类的小部件字段。 In there you can define which widget from django.forms you want to use, then can define whatever attrs you want. 在那里你可以定义你想要使用的django.forms中的哪个小部件,然后可以定义你想要的任何玩家。 Here we use 'class' and set its argument to 'form-control'. 这里我们使用'class'并将其参数设置为'form-control'。 You could easily substitute as many css classes as you wanted, ie 您可以轻松地替换所需数量的css类,即

'name': TextInput(attrs={'class': 'form-control xs my-other-css-class'}),

I use this with bootstrap frequently and it works fine, so long as you import bootstrap somewhere along the line (either from the django-bootstrap plugin, or just through static files in your base template) 我经常使用它与引导程序,并且它工作正常,只要您沿着该行某处导入引导程序(来自django-bootstrap插件,或者只是通过基本模板中的静态文件)

There is a great package used for customizing the display of form widgets called django-widget-tweaks . 有一个很棒的包用于自定义表单小部件的显示,称为django-widget-tweaks The package lets you customize css classes and attributes. 该软件包允许您自定义css类和属性。

Instead of using form.as_p you can access each item individually and use the render_field tag to add css classes. 您可以单独访问每个项目并使用render_field标记添加css类,而不是使用form.as_p

{% load widget_tweaks %}

{% for field in form %}
    {% render_field field class+="css_class_1 css_class_2" %}
{% endfor %}

Or use custom filters included in the package for compactness: 或者使用包中包含的自定义过滤器来实现紧凑性:

{% load widget_tweaks %}

{% for field in form %}
    {{ field|add_class:"my-css-class" }}
{% endfor %}

Or without a loop: 或者没有循环:

{% load widget_tweaks %}

{% render_field form.name class+="css_class_1 css_class_2" %}
{% render_field form.email class+="css_class_1 other_class" %}
{% render_field form.title class+="css_class_1 css_class_3" %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM