django 右对齐无法对齐 [英] django align right cannot align

查看:62
本文介绍了django 右对齐无法对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最简单的方法来对齐右 django 十进制字段.有人可以指出我正确的方法吗?我发现了不同的帖子,但它仍然显示左对齐.我是否必须覆盖管理员,或者我直接在管理员中错过了一些简单的功能.

I'm looking for the simplest way to have aligned right django decimal fields. Can someone point me to the correct approach? I have found different post, but it still display aligned left. Do i have to override the admin, or i miss some simple feature directly in the admin.

我想要 3 列(ore_dovute、totale、ore_eccedenti),正确对齐,并根据我的需要使用正确的格式.

I would like to have the 3 columns (ore_dovute, totale, ore_eccedenti), aligned right, with correct formatting for my needs.

class TotaliManager(models.Model):
    utente_id = models.IntegerField(max_length=11,null=True)
    ore_dovute = models.DecimalField('ore dovute',max_digits=5,decimal_places=2,null=True)
    totale = models.DecimalField('ore effettuate',max_digits=5,decimal_places=2,null=True)
    ore_eccedenti = models.DecimalField('ore eccedenti',max_digits=5,decimal_places=2,null=True)


    class Meta:
        verbose_name_plural = "Totali per anno / mese"

非常感谢

推荐答案

如果您在讨论管理表单,则需要覆盖小部件.

If you are talking about the admin forms, you will need to override the widget.

Widget.attrs 允许您设置自定义 HTML 属性,以便在呈现时添加到表单字段中.您可以使用 ModelAdmin.formfield_overrides.

from django.contrib import admin
from django.db import models
from django.forms.widgets import TextInput

from myapp.models import TotaliManager

class TotaliManagerAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.DecimalField: {'widget': TextInput(attrs={'class': 'text-right'}),
    }

admin.site.register(TotaliManager, TotaliManagerAdmin)

在这种情况下,它将类 text-right 添加到输入字段.如果您使用引导程序,它是内置的,否则只需添加 css:

In this case, it adds the class text-right to the input field. If you use bootstrap, it's built in, otherwise just add the css:

.text-right {
    text-align: right;
}

这篇关于django 右对齐无法对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆