Opened 16 years ago

Closed 16 years ago

#8361 closed (invalid)

instance.content.url should return empty string if no file associated

Reported by: Jesaja Everling Owned by: nobody
Component: File uploads/storage Version: dev
Severity: Keywords: File storage refactoring File get_FIELD_url
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello everybody.
Due to the File storage refactoring, urls of ImageFields can now be retrieved not by using

instance.get_content_url(),
but rather by

instance.content.url

At the moment, if there is no file associated with the FileField, an exception is thrown.
In my opinion, this is not optimal.
Previously, you could do something like this in the template:

{% if instance.get_content_url %}
<img src="/userimages/instance.get_content_url">
{% else %}
<img src="/userimages/default.jpg">
{% endif %}

If you try to do this with instance.content.url, you get an exception.
I think it would be better, if just an empty string would be returned.

$DJANGOPATH\db\models\fields\files.py line 55

        self._require_file()

should become something like that:

        try:
            self._require_file()
        except:
            return ""

This probably isn't the most elegant way to do this, but it seems to work...

Attachments (1)

files.py.diff (143 bytes ) - added by Jesaja Everling 16 years ago.
diff for /django/db/models/fields/files.py

Download all attachments as: .zip

Change History (2)

by Jesaja Everling, 16 years ago

Attachment: files.py.diff added

diff for /django/db/models/fields/files.py

comment:1 by Jesaja Everling, 16 years ago

Resolution: invalid
Status: newclosed

Apparently I didn't have enough sleep the last days! :)
This works just fine:

{% if instance %}
<img src="/userimages/instance.get_content_url">
{% else %}
<img src="/userimages/default.jpg">
{% endif %}

So please just ignore this ticket.
Sorry for the false report.

Note: See TracTickets for help on using tickets.
Back to Top