views:

139

answers:

2

Hi,

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 templates, 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.

A: 

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.

Lukasz Dziedzia
+2  A: 

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.

KillianDS
Ah, a custom widget, why didn't i thing of that. Super, will try it out asap.
Espen Christensen