Django:在模板中自定义FileField的当前文件

发布于 2025-02-02 21:50:57 字数 573 浏览 1 评论 0原文

我正在使用文件字段渲染一个简单的表单。

class ReviewForm(forms.ModelForm):

    class Meta:
        model = Review

        fields = [
            'file',
        ]

        widgets = {
            'file': forms.ClearableFileInput(),
        }

在渲染时,表单显示当前文件 - /xyz/xyz/xyz.pdf除了文件输入。

但是,我不允许直接访问文件。所有请求在加载文件之前都会通过Analytics_logger函数。

因此,我想从当前

文件 - < a href =“ {0}”> {1}</a> 到

< 文件 - &lt; a href =“ {%url'Analytics_logger'{0}%}”

代码>当前 文件。仅应显示文件名。

我该如何在django 3.x中做到这一点?

I am rendering a simple form with a file field.

class ReviewForm(forms.ModelForm):

    class Meta:
        model = Review

        fields = [
            'file',
        ]

        widgets = {
            'file': forms.ClearableFileInput(),
        }

While rendering, the form shows Current file - /xyz/xyz/xyz.pdf besides the file input.

However, I do not allow direct access to the files. All requests goes through the analytics_logger function before loading the file.

So, I want to customise the Current file html from

Current file - <a href="{0}">{1}</a> to

Current file - <a href="{% url 'analytics_logger' {0} %}">{1}</a>

I also don't want to display the full path of the file. Only the filename should be displayed.

How can I do it in Django 3.x?

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

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

发布评论

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

评论(1

苏辞 2025-02-09 21:50:57

如果customClearableFileInput(),建议采用以下步骤。

  1. 覆盖template_name clearableFileInput的变量类。可以以相同的形式创建一个新类。

class CustomClearableFileInput(ClearableFileInput):
  template_name = 'widgets/customclearablefileinput.html'
  1. 创建一个新文件widgets/customClearableFileinput.html在模板目录中。

  2. 通过修改根据您的要求,窗口小部件模板的原始代码

  3. 您可以更改codeclecrearablefileinput.html中的HREF标签,以将链接包装在Analytics_logger中。

    &lt; a href =“ {%url'Analytics_logger'Widget.Analytics_logger_id%}”&gt;
    {{widget.value}}
    &lt;/a&gt;

  4. 接下来,您可以将变量作为表单属性的一部分传递,可以访问以替换本机完整文件名并自定义URL标签。

    &lt; a href =“ {%url'Analytics_logger'widget.attrs.analytics_logger_id%}”&gt;
    {{widget.attrs.filename}}}
    &lt;/a&gt;

  5. 建议采用以下步骤传递变量。在views.py

    中启动表单时传递变量

     form =评论form(instance = eviewforfor_instance,shiallytics_logger_id = eviewform_instance.pk,filename = eviewform_instance.filename)
  6. 接下来,覆盖表单的 init 方法并更新字段的属性。

     self.fields ['processed_book']。
    
  7. 将以下内容包含在settings.py

    form_renderer ='django.forms.renderers.templatessetting'

  8. 添加'django.forms' to installed_appssettings.py

  9. 现在,您可以在小部件中包括新的覆盖类。

     类meta:
        模型=评论
    
        字段= [
            '文件',
        这是给出的
    
        小部件= {
            'file':casteClecreCleableFileInput(),
        }
     

Following steps are recommended in case of CustomClearableFileInput().

  1. Override the template_name variable of the ClearableFileInput class. A new class can be created in the same forms.py file.

class CustomClearableFileInput(ClearableFileInput):
  template_name = 'widgets/customclearablefileinput.html'
  1. Create a new file widgets/customclearablefileinput.html in the templates directory.

  2. Fill the widgets/customclearablefileinput.html file by modifing the original code of the widget template as per your requirement.

  3. You can change the href tag in the customclearablefileinput.html to wrap the link in the analytics_logger.

    <a href="{% url 'analytics_logger' widget.attrs.analytics_logger_id %}">
    {{ widget.value }}
    </a>

  4. Next you can pass variables as part of form attributes that can accessed to replace the native full filename and customise the url tag.

    <a href="{% url 'analytics_logger' widget.attrs.analytics_logger_id %}">
    {{ widget.attrs.filename }}
    </a>

  5. Following steps are recommended to pass the variables. Pass the variables while initilizing the form in the views.py

    form = ReviewForm(instance= reviewform_instance, analytics_logger_id=reviewform_instance.pk, filename = reviewform_instance.filename)
  6. Next, override the init method of the form and update the attributes of the field.

    self.fields['processed_book'].widget.attrs.update( {'analytics_logger_id': analytics_logger_id, 'filename': filename,})
  7. Include the following in the settings.py

    FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

  8. Add 'django.forms' to INSTALLED_APPS in settings.py.

  9. Now, you can include the new overridden class in the widget.

    class Meta:
        model = Review
    
        fields = [
            'file',
        ]
    
        widgets = {
            'file': CustomClearableFileInput(),
        }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文