脆皮形式的定制场地django不起作用

发布于 2025-01-25 04:19:51 字数 2385 浏览 2 评论 0原文

我想显示一个表格来创建帖子。我使用Crispy-Form,目前显示: ”在此处输入图像描述

使用html模板:

{% extends 'blog_app/base.html' %}
{% load crispy_forms_tags %}

{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Create Post</legend>
                {{ form.media }}
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
            </div>
        </form>
    </div>
{% endblock %}

我想增加标题框的大小并减小内容框的大小,以便我适合内容部分。

我尝试的是:

  1. 在模板中,将每个字段显示为带有指定CSS类的脆性字段:
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <div class="form-group col-md-8">
                {{ form.title|as_crispy_field }}
            </div>
            <div class="form-group col-md-8">
              {{ form.content|as_crispy_field }}
            </div>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
            </div>
        </form>
    </div>
{% endblock %}
  1. 在表单类中,设置助手和布局:
class PostCreateForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'content']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()

        self.layout = Layout(
            Field('title', id="form-title", css_class="col-md-8", name="title"),
            Field('content', id="form-content", css_class="col-md-8", name="title"))

在这两种方面,什么都没有改变。 有人可以给我指针吗?

更新:

对于内容框,由于我使用ckeditor的Richtextfield,因此当我将以下配置添加到settings.py时,它的大小确实会更改以适合内容部分。但是我仍然不知道如何更改标题框的大小。

CKEDITOR_CONFIGS = {
    'default': {
        'height': '100%',
        'width': '100%',
    },
}

i want to display a form to create posts. i use crispy-form and it currently displays: enter image description here

with html template:

{% extends 'blog_app/base.html' %}
{% load crispy_forms_tags %}

{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Create Post</legend>
                {{ form.media }}
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
            </div>
        </form>
    </div>
{% endblock %}

i want to increase size of the title box and reduce size of the content box so that i fits the content section.

what i tried:

  1. in template, display each field as crispy field with specified css class:
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <div class="form-group col-md-8">
                {{ form.title|as_crispy_field }}
            </div>
            <div class="form-group col-md-8">
              {{ form.content|as_crispy_field }}
            </div>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Post</button>
            </div>
        </form>
    </div>
{% endblock %}
  1. in form class, set helper and layout:
class PostCreateForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'content']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()

        self.layout = Layout(
            Field('title', id="form-title", css_class="col-md-8", name="title"),
            Field('content', id="form-content", css_class="col-md-8", name="title"))

in both ways, nothing changes.
can someone give me a pointer?

update:

for the content box, since i used RichTextField from ckeditor for it, when i add the below config to settings.py, the size of it does change to fit the content section. but i still have no idea how to change size of the title box.

CKEDITOR_CONFIGS = {
    'default': {
        'height': '100%',
        'width': '100%',
    },
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2025-02-01 04:19:51

确保您在settings.py中具有正确的模板包:

CRISPY_TEMPLATE_PACK = 'bootstrap4'

...并在布局中使用列和行(以helper的形式,不是直接以表单为单位):


from crispy_forms.layout import Layout, HTML, Row, Column


class PostCreateForm(forms.ModelForm):
    
    class Meta:
        model = Post
        fields = ['title', 'content']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
             Row(
                Column(
                    'title',
                    css_class='col-md-8'
                ),
            ),
             Row(
                Column(
                    'content',
                    css_class='col-md-8'
                ),
            )
        )

下一步,使用脆皮 TemplateTag:

<form method="POST">
    {% csrf_token %}
    <fieldset class="form-group">
        <legend class="border-bottom mb-4">Create Post</legend>
        {{ form.media }}
        {% crispy form %}
    </fieldset>
    <div class="form-group">
        <button class="btn btn-outline-info" type="submit">Post</button>
    </div>
</form>

Ensure that you have the correct template pack in your settings.py:

CRISPY_TEMPLATE_PACK = 'bootstrap4'

...and use your columns and rows in the layout (in the form helper, not directly in the form):


from crispy_forms.layout import Layout, HTML, Row, Column


class PostCreateForm(forms.ModelForm):
    
    class Meta:
        model = Post
        fields = ['title', 'content']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
             Row(
                Column(
                    'title',
                    css_class='col-md-8'
                ),
            ),
             Row(
                Column(
                    'content',
                    css_class='col-md-8'
                ),
            )
        )

Next, use the crispy templatetag:

<form method="POST">
    {% csrf_token %}
    <fieldset class="form-group">
        <legend class="border-bottom mb-4">Create Post</legend>
        {{ form.media }}
        {% crispy form %}
    </fieldset>
    <div class="form-group">
        <button class="btn btn-outline-info" type="submit">Post</button>
    </div>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文