Django / Bootstrap 表单 - 输入大小 - Python

发布于 2025-01-11 00:44:25 字数 1999 浏览 0 评论 0原文

我正在学习 Django 和 Bootstrap (5.1.3),我希望让我的代码尽可能简单以便将来更新。 这就是为什么我在 Django 中使用通用视图(创建/详细信息/编辑...)。效果很好,所有功能现在都可以无缝运行。

我正在努力解决的是我的编辑/创建表单的布局。我找不到增加输入框大小的方法。

我的模型有以下代码:

    class customer(models.Model):
        created_date = models.DateTimeField(auto_now_add = True, editable = False, verbose_name = u"Créé le")
        modified_date = models.DateTimeField(auto_now = True, editable = False, verbose_name = u"Modifié le")
        
        fk_referrer = models.ForeignKey('customer', on_delete=models.CASCADE, verbose_name='Parrain', blank = True, null=True)
        surname = models.CharField('Prénom', max_length=200)
        lastname = models.CharField('Nom', max_length=200)
        phonenumber = PhoneNumberField('Téléphone', blank = True, null = True)
        email = models.EmailField('Email',max_length=100, blank = True, null = True)
        fk_interest = models.ManyToManyField(interests, verbose_name='Interêts', blank=True)
        comments = models.TextField('Commentaires', max_length=2000, blank=True, null = True)
    
        def __str__(self):
            return (self.surname + " " + self.lastname)

我确实创建了一个通用视图:

    class CustomerEditView(UpdateView):
        model = customer
        fields = "__all__"
        template_name = 'client/edition.html'
        success_url = '/client/'

并在 Edition.html 模板中添加了表单:

<form method="POST" enctype="multipart/form-data">
 
    <!-- Security token -->
    {% csrf_token %}
 
    <!-- Using the formset -->
<table>
    {{ form.as_table }}
</table>     
    <input type="submit" value="{% if object %}Mettre à jour{% else %}Créer{% endif %}">
</form>

输出如下所示:

表单布局

文本输入字段不够大。我想至少填满表格(用边框显示) 我认为我应该能够使用表单对其进行整理,并重新定义我的所有视图以使它们更加具体(而不是通用)。但是有没有办法更新视图/引导样式,以便输入字段的宽度将使用所有可用空间?也许在通用视图之上使用表单?

非常感谢您的帮助。

I'm learning Django together with Bootstrap (5.1.3), and I'd like to keep my code as simple as possible for future updates.
That's why I went for generic views in Django (Create/Detail/Edit...). That works great, all the features work seamlessly now.

What I'm struggling with is the layout of my Edit / Create forms. I can't find a way to increase the size of the input boxes.

I have the following code, for my Model:

    class customer(models.Model):
        created_date = models.DateTimeField(auto_now_add = True, editable = False, verbose_name = u"Créé le")
        modified_date = models.DateTimeField(auto_now = True, editable = False, verbose_name = u"Modifié le")
        
        fk_referrer = models.ForeignKey('customer', on_delete=models.CASCADE, verbose_name='Parrain', blank = True, null=True)
        surname = models.CharField('Prénom', max_length=200)
        lastname = models.CharField('Nom', max_length=200)
        phonenumber = PhoneNumberField('Téléphone', blank = True, null = True)
        email = models.EmailField('Email',max_length=100, blank = True, null = True)
        fk_interest = models.ManyToManyField(interests, verbose_name='Interêts', blank=True)
        comments = models.TextField('Commentaires', max_length=2000, blank=True, null = True)
    
        def __str__(self):
            return (self.surname + " " + self.lastname)

I did create a generic view:

    class CustomerEditView(UpdateView):
        model = customer
        fields = "__all__"
        template_name = 'client/edition.html'
        success_url = '/client/'

And have added the form in edition.html template:

<form method="POST" enctype="multipart/form-data">
 
    <!-- Security token -->
    {% csrf_token %}
 
    <!-- Using the formset -->
<table>
    {{ form.as_table }}
</table>     
    <input type="submit" value="{% if object %}Mettre à jour{% else %}Créer{% endif %}">
</form>

Here's what the output looks like:

Form layout

The text input fields are not large enough. I would like to at least fill the table (shown with a border)
I think I should be able to sort it out using Forms, and redefining all my views to make them more specific (not generic). But is there a way to update the view / bootstrap styles so the width of the input fields would use all the space available ? Maybe using a Form on top of a generic view ?

Thanks a lot for any help.

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

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

发布评论

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

评论(2

听风念你 2025-01-18 00:44:25

我在 Django 中使用脆皮表单。它们与 bootstrap 一起工作并且非常易于使用。请查看这个示例。
https://simpleisbetterthancomplex.com/tutorial/2018/11/28/advanced-form-rendering-with-django-crispy-forms.html

I use crispy forms with Django. They work alongside bootstrap and are very easy to use. Please check out this example.
https://simpleisbetterthancomplex.com/tutorial/2018/11/28/advanced-form-rendering-with-django-crispy-forms.html

拿命拼未来 2025-01-18 00:44:25

由于您使用引导程序手动渲染表单元素并使用引导程序表单控件大小类(即 form-control-lg),如下所示。

<div class="col-md-6">
                  <!-- col-md-6 -->
                  <strong>{{ form.email.label_tag }}</strong>{{ form.email|add_class:'form-control form-control-lg' }}
                  <text class="text-danger">{{ form.email.errors }}</text>
                  {% if form.email.help_text %}
                    <small>{{ form.email.help_text|safe }}</small>
                  {% endif %}
                </div>

Since you are using bootstrap render the form element manually and use the bootstrap form control size class (i.e. form-control-lg) like below.

<div class="col-md-6">
                  <!-- col-md-6 -->
                  <strong>{{ form.email.label_tag }}</strong>{{ form.email|add_class:'form-control form-control-lg' }}
                  <text class="text-danger">{{ form.email.errors }}</text>
                  {% if form.email.help_text %}
                    <small>{{ form.email.help_text|safe }}</small>
                  {% endif %}
                </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文