Opened 8 years ago

Closed 8 years ago

#8361 closed (invalid)

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

Reported by: jeverling Owned by: nobody
Component: File uploads/storage Version: master
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: UI/UX:


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

but rather by


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\ line 55


should become something like that:

            return ""

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

Attachments (1) (143 bytes) - added by jeverling 8 years ago.
diff for /django/db/models/fields/

Download all attachments as: .zip

Change History (2)

Changed 8 years ago by jeverling

diff for /django/db/models/fields/

comment:1 Changed 8 years ago by jeverling

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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