How force DJANGo choice MEDIA_URL or STATIC_URL in template?

57 views
Skip to first unread message

setivo...@gmail.com

unread,
Feb 29, 2016, 4:49:53 PM2/29/16
to Django users

I need choice base url MEDIA_URL (/media/) - for uploaded user picture, or STATIC_URL (/static/) - picture by default

I am try next:

p><img src="{% if account.accountuserinfo.picture %}MEDIA{% else %}STATIC{% endif %}_URL{{ account.accountuserinfo.picture_url }}" alt="picture_for_{{ account.name }}" id="account_picture"></p>

, but result is MEDIA_URL(STATIC_URL) and path to file.

I am next try:

{% templatetag openvariable %} url 'entry_list' {% templatetag closevariable %}

but result is {{ MEDIA_URL }} or {{ STATIC_URL }} (not working)

may by anyone known how solved this problem
---------------------------------------------------

Thanks

Jonas Svensson

unread,
Mar 1, 2016, 10:23:04 AM3/1/16
to Django users
It seems like you want to use display a default picture if the user does not have one.
    {% load static %}
    {% static "images/default_pic.jpg" as default_pic %}
    {{ account.accountuserinfo.picture|default:default_pic }}

Cheers

Seti Volkylany

unread,
Mar 1, 2016, 11:55:38 AM3/1/16
to django...@googlegroups.com
it does not help, because I am image by default 


class AccountUserInfo(models.Model):

    MAN = 'man'
    WOMAN = 'woman'
    VAGUE = 'vague'

    GENDER_CHOICES = [
        (VAGUE, 'Vague'),
        (MAN, 'Male'),
        (WOMAN, 'Female'),
    ]

    def dispatch_account_media_files(instance, filename):
        return '{0}/app_accounts/{1}'.format(instance.account.__str__(), filename)

    account = models.OneToOneField(AccountUser, on_delete=models.CASCADE)
    first_name = models.CharField('First name', max_length=50, blank=True, null=True)
    last_name = models.CharField('Last name', max_length=50, blank=True, null=True)
    picture = models.ImageField('Picture', upload_to=dispatch_account_media_files, blank=True, null=True)
    gender = models.CharField('Gender', max_length=10, choices=GENDER_CHOICES, default=GENDER_CHOICES[0][0])
    country = CountryField('Country', blank_label='(select country)', blank=True, null=True)
    birthday = models.DateField('Birthday', blank=True, null=True)


----------------------------------

    @property
    def picture_url(self):
        if self.picture and hasattr(self.picture, 'url'):
            return self.picture.url
        else:
            return '/project_static/images/default-picture.png'

But thanks for the advice


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/5ZJzHbs1sYY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ad20612d-2a90-4108-8737-d4bbe99a262c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Mar 1, 2016, 6:41:28 PM3/1/16
to django...@googlegroups.com

I need choice base url MEDIA_URL (/media/) - for uploaded user picture, or STATIC_URL (/static/) - picture by default

I am try next:

p><img src="{% if account.accountuserinfo.picture %}MEDIA{% else %}STATIC{% endif %}_URL{{ account.accountuserinfo.picture_url }}" alt="picture_for_{{ account.name }}" id="account_picture"></p>

, but result is MEDIA_URL(STATIC_URL) and path to file.
 

You should work your {% if %} statement this way:

{% if account.accountuserinfo.picture %}
    <img src="{{ MEDIA_URL }}{{ account.accountuserinfo.picture }}">
{% else %}
    <img src="{{ STATIC_URL }}{{ account.accountuserinfo.picture_url }}">
{% endif %}

Trying to cram all of that together within the template is awful to read, and doesn't work as you've seen.

It would also make more sense to have the URL path of the default image set somewhere other than inside of a property. Either as a global variable for the model class, or even within your settings.py file and brought in as part of the template context if it can be used by other areas of the system. Querying an object instance for a static value like that is not good style.

-James

setivo...@gmail.com

unread,
Mar 2, 2016, 5:56:15 AM3/2/16
to Django users
I am solved problem next variant:

in model
----------------------

@property
def account_user_picture(self):
    if self.picture:
        return self.picture.url
    else:
        return settings.STATIC_URL + 'project_static/images/default-picture.png'

in templates
------------------------------------------
<img src="{{ account.accountuserinfo.account_user_picture }}">


And all worked

Thanks

среда, 2 марта 2016 г., 1:41:28 UTC+2 пользователь James Schneider написал:
Reply all
Reply to author
Forward
0 new messages