简体   繁体   English

如何在 Django 中设置表单样式?

[英]How to style form in Django?

I'm trying to style an upload form in a Django site, so far it is not working.我正在尝试在 Django 站点中设置上传表单的样式,但到目前为止它不起作用。 I usually lose some part in this process or at the end the form is not working.我通常会在此过程中丢失某些部分,或者最终表格无法正常工作。

I was wondering what would be simplest way to keep all elements, but to keep it looking modern.我想知道保留所有元素的最简单方法是什么,但要让它看起来很现代。

This is how it looks now:这是它现在的样子:

当前表单的屏幕截图,显示文件浏览按钮和上传和下载按钮

This is my HTML:这是我的 HTML:

{% extends "base.html" %}
{% load static %}
{% block content %}

      <form action="{% url "rca" %}" method="post" enctype="multipart/form-data" class="dropzone">
      {% csrf_token %}
      {{ message }}
      <p>{{ form.non_field_errors }}</p>

      <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>

      <p>
            {{ form.docfile.errors }}
            {{ form.docfile }}
      </p>

      <p><input type="submit" value="Upload and Download!"/></p>
      </form>

{% endblock content %}

Please give me some simple solution suggestions.请给我一些简单的解决方案建议。

You need to customize the appearance.您需要自定义外观。 You can customize widgets in two ways: it can be via widget instance or via widget class.您可以通过两种方式自定义小部件:它可以通过小部件实例或通过小部件 class。 For this first example I'll be using widget class.对于第一个示例,我将使用小部件 class。 Basically you have to use the widget class argument, The widget class has a basic attribute attrs, We also have to add a new class called Meta and specify the name of the model this form is related to, the fields we want to have and finally the widgets for those fields. Basically you have to use the widget class argument, The widget class has a basic attribute attrs, We also have to add a new class called Meta and specify the name of the model this form is related to, the fields we want to have and finally这些字段的小部件。

File: form.py文件:form.py

class FormName(forms.ModelForm):
    class Meta:
        model = model_name
        fields = ('Browse')
        widgets = {
            'any_proof': forms.ClearableFileInput(attrs={
                'class': "form-control",
                'style':'margin-bottom:5px',
                })
        }

class here you can add bootstrap class or class names. class在这里您可以添加引导程序 class 或 class 名称。

or或者

style here you can write css code. style这里你可以写 css 代码。

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

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