Code

Opened 6 years ago

Closed 6 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:

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 jeverling 6 years ago.
diff for /django/db/models/fields/files.py

Download all attachments as: .zip

Change History (2)

Changed 6 years ago by jeverling

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

comment:1 Changed 6 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.