【问题标题】:Delete colon after initial_text and input_text django forms在 initial_text 和 input_text django 表单后删除冒号
【发布时间】:2021-06-15 08:59:56
【问题描述】:

我正在尝试自定义表单设计,但有些内容我不知道如何删除/更改。

forms.py

class UserProfileAvatarForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ['avatar']

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

        self.fields['avatar'].widget.attrs.update(
            {'class': 'form-control', 'type': 'file', 'id': 'formFile'})

这是我得到的: [![在此处输入图片描述][1]][1]

如何摆脱“当前”和“更改:”?

这是 html 输出:

[![在此处输入图片描述][2]][2]

我可以将display:none 赋予a 标签以使其消失,但“当前”和“更改:”没有标签。有什么解决办法吗?

它与css或forms.py有关?


AMG 帮助后更新

[![在此处输入图片描述][1]][1]

[![在此处输入图片描述][2]][2]

forms.py

class MyClearableFileInput(ClearableFileInput):
    initial_text = ''
    input_text = ''

class UserProfileAvatarForm(forms.ModelForm):

    avatar = forms.ImageField(widget=MyClearableFileInput)

    class Meta:
        model = Profile
        fields = ['avatar']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['avatar'].label_suffix = ''
        self.fields['avatar'].widget.attrs.update(
            {'class': 'form-control', 'type': 'file', 'id': 'formFile'})

【问题讨论】:

    标签: python css django django-forms django-templates


    【解决方案1】:

    您可能需要覆盖/django/forms/widgets/ClearableFileInput() 并将initial_text 设置为“”。

    看到Change default text shown for ImageField when not empty 有类似的问题,但我相信该属性曾经是template_with_initial,现在似乎是initial_text

    对同一类中的input_text 执行相同操作。

    或者,由于小部件在需要时使用gettext() 进行翻译,因此您可以使用英语 tran(https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#how-django-discovers-translations) 更新您的消息文件。

    编辑:

    创建您自己的不带“:”的自定义小部件。使用这个股票 (https://github.com/django/django/blob/76c0b32f826469320c59709d31e2f2126dd7c505/django/forms/templates/django/forms/widgets/clearable_file_input.html) 作为您的起点,将其保存在您的应用程序中,然后使用 template_name 指向您的自定义小部件模板 (https://docs.djangoproject.com/en/3.2/ref/forms/renderers/#overriding-built-in-widget-templates)。如果您这样做,您还可以撤消先前对 ClearableFileInput() 的覆盖,因为如果您从 html 模板中删除 initial_text,这将是不必要的。

    股票clearable_file_input.html 看起来像:

    {% if widget.is_initial %}{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
    <input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
    <label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
    {{ widget.input_text }}:{% endif %}
    <input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
    

    你可能想要这样的东西:

    {% if widget.is_initial %}<a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
    <input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
    <label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
    {% endif %}
    <input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
    

    【讨论】:

    • 我设法删除了Curently 和`Change`,但在每个之后我都留下了:。 label_sufix 帮我删除:aftaer 头像。知道我可以对: 做些什么吗?我用一些新照片和代码更新了帖子以查看更改
    • 我刚刚意识到我可以做其他事情。我可以加载一个干净的表单,而不是用实例加载我的表单 avatar,这不会给我curentlychange。这只是文件输入。但我学到了新东西。泰阿吉安
    【解决方案2】:

    我删除了我的实例,该实例使用clean form 加载了我的表单

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      相关资源
      最近更新 更多