Django:在模板中自定义FileField的当前文件
我正在使用文件字段渲染一个简单的表单。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
customClearableFileInput()
,建议采用以下步骤。覆盖
template_name
clearableFileInput的变量
类。可以以相同的形式创建一个新类。创建一个新文件
widgets/customClearableFileinput.html
在模板目录中。通过修改根据您的要求,窗口小部件模板的原始代码。
您可以更改
codeclecrearablefileinput.html
中的HREF标签,以将链接包装在Analytics_logger中。&lt; a href =“ {%url'Analytics_logger'Widget.Analytics_logger_id%}”&gt;
{{widget.value}}
&lt;/a&gt;
接下来,您可以将变量作为表单属性的一部分传递,可以访问以替换本机完整文件名并自定义URL标签。
&lt; a href =“ {%url'Analytics_logger'widget.attrs.analytics_logger_id%}”&gt;
{{widget.attrs.filename}}}
&lt;/a&gt;
建议采用以下步骤传递变量。在views.py
中启动表单时传递变量
将以下内容包含在settings.py
中
form_renderer ='django.forms.renderers.templatessetting'
添加
'django.forms'
toinstalled_apps
在settings.py
。中
现在,您可以在小部件中包括新的覆盖类。
Following steps are recommended in case of
CustomClearableFileInput()
.Override the
template_name
variable of theClearableFileInput
class. A new class can be created in the same forms.py file.Create a new file
widgets/customclearablefileinput.html
in the templates directory.Fill the
widgets/customclearablefileinput.html
file by modifing the original code of the widget template as per your requirement.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>
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>
Following steps are recommended to pass the variables. Pass the variables while initilizing the form in the views.py
Next, override the init method of the form and update the attributes of the field.
Include the following in the settings.py
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
Add
'django.forms'
toINSTALLED_APPS
insettings.py
.Now, you can include the new overridden class in the widget.