开发者

How to show current file in templates with Django FileFields

I am kinda stuck, I've been using Django for a while now, but I cant actually seem to find this thing out. And thats weird because it should be a simple thing.

I've been googling around and can't seem to find a solution, it maybe because it is a simple thing.

The problem is, I have a ModelForm, and it has a FileField, when I render this form with an instance of the model, and the object contains an uploaded file, I would like to show the current file, (like the admin does), but I cant seem to do that.

Neither of these techniques work:

{{ form.as_p }}
{{ field.label_tag }}

I've been searching around in the admins temp开发者_Python百科lates, but can't seem to find the magic. The form renders all the other data of the object correctly

Problem number 2 is that since the uploaded file doesn't show when rendering a change form with a instance of an object, the forms fail to validate when i try to save since it knows nothing about the file that was previously uploaded.

So how do you handle filefields in a change form in Django, how to show the current uploaded file, and how to validate the form.


You didn't look around well enough, the solution is a custom widget in contrib/admin/widgets.py, namely this one:

class AdminFileWidget(forms.FileInput):
    """
    A FileField Widget that shows its current value if it has one.
    """
    def __init__(self, attrs={}):
        super(AdminFileWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        output = []
        if value and hasattr(value, "url"):
            output.append('%s <a target="_blank" href="%s">%s</a> <br />%s ' % \
                (_('Currently:'), value.url, value, _('Change:')))
        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

You can use this (or an adapted version) using the widgets meta option of ModelForms.

As far as I know your second problem shouldn't be there at all, even without a nicer widget, let it know if it still persists after using a custom widget.


I don't think beruic's answer get enough attention as a comment, but he really got this since Django now fixes this automatically.

"A normal ClearableFileInput from Django.forms does it for me."

See example below.

models.py

class PosterForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(PosterForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Poster

        fields = ('title', 'img')

        widgets = {
            'img' : forms.ClearableFileInput(),
            'title' : forms.TextInput()
        }


I use this snippet to display ImageFields (with extra checkbox to delete them). Not sure how to display non image fields but maybe this snippet will give you some inspiration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜