python - How do I increase the size of a Django form
问题描述
This is my first project using Django. I am creating a form as follows and as you can see on the screenshot below, the form is just too narrow. I'd like the input fields to go across the whole screen which is not possible when the form is that narrow. How can I increase the width of the form?
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Row, Field
from ..models import EvilSnippet
class AddSnippet(forms.Form):
class Meta:
model = EvilSnippet
code_language = forms.ChoiceField(
label = "Select Language",
choices = (("Java", "Java"), ("Python", "Python"), ("C#", "C#")),
required = True,
)
severity = forms.ChoiceField(
label = "Severity",
choices = (("HIGH", "HIGH"), ("MEDIUM", "MEDIUM"), ("LOW", "LOW")),
required = True,
)
description = forms.CharField(
label = "Description",
required = False,
#widget=forms.TextInput(attrs={'size': '20'})
)
code = forms.CharField(
label = "Code",
required = False,
widget=forms.Textarea()
)
def __init__(self, *args, **kwargs):
super(AddSnippet, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-addSnippet'
self.helper.form_class = 'uniForms'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_snippet'
self.helper.layout = Layout(
Row(
Field('code_language', wrapper_class='col-md-6'),
),
Row(
Field('severity', wrapper_class='col-md-6'),
),
Row(
Field('description', wrapper_class='col-md-6'),
),
Row(
Field('code', wrapper_class='col-md-6'),
)
)
self.helper.add_input(Submit('submit', 'Submit'))
and
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<!-- <form action = '' method="post"> -->
<!-- Very Important csrf Token -->
{% csrf_token %}
<!-- <table> -->
{% crispy form %}
<!-- </table> -->
<!-- </form> -->
{% endblock %}
解决方案
To control the display of a webpage, you need to use CSS. For you case, you'll want to use the width property. Something like this should work for you:
form#id-addSnippet {
width: 100%;
}
This will set the width of the form with id id-addSnippet
to 100% of its containing block.
See this answer for more details about what width: 100%
means.
推荐阅读
- ruby-on-rails - 导轨计算(价格*数量)
- r - 可以将 drop=FALSE 传递给 $ 吗?
- javascript - 在 AJAX 调用中循环 PHP 时保持实时日志
- sql - 解释分析比 postgres 中的实际查询慢
- sql - 如何使用 SQL 窗口对具有条件的行求和
- mysql - 从包含较长时间段的表中选择每 N 小时间隔
- midi - 可以将 MIDI 可变长度数量强制为四个字节的长度吗?
- r - 在 r 中的 data.table 中折叠具有互补列数据的行
- swiftui - SwiftUI - 如何使用 TabView 弹出到根目录?
- java - Java NIO 多播/UDP 环回延迟