English 中文(简体)
django-imagekit——显示违约形象的更好方式?
原标题:django-imagekit - better way of showing a default image?

I m using django-imagekit,将我的用户批量转成册,现在有权显示一个违约ava(如果用户确实卸下了他的ava)。 我这样做:

<>views.py

    try:
        usr_avatar = UsrAvatar.objects.get(user=request.user.id)        
    except UsrAvatar.DoesNotExist: 
        usr_avatar = UsrAvatar.objects.get(id= 0 )  

template.html>

<img src="{{ usr_avatar.avatar_image.url }}" >

这部工程是罚款的,但每当一个用户卸下其瓦tar一米,打上数据库,以防弹 image图像。

如果用户确实有ava形象,把缺省图像连接到usr_avatar<>/code>时,是否有办法消除数据库的碰撞。 或只是在模板中做一些事情。 html? 谢谢!

最佳回答

您提问的用户名!

http://blog.madpython.com/204/07/django-context-processors-best-practice/“rel=“nofollow”>context processor,为每个模板提供违约ava,并且简单地确保背景处理者 ca照图像

http://www.py

TEMPLATE_CONTEXT_PROCESSORS = (
...
 myapp.context_processors.default_avatar ,
...
)

myapp/context_processors.py

from django.core.cache import cache 
def default_avatar(request):
    default_avatar = cache.get( default_avatar , False)
    if not default_avatar:
        default_avatar = UsrAvatar.object.get(id= 0 )
    return {
         default_avatar  : default_avatar
    }

现在,每个模板都有可变的缺漏_:

{% if usr_avatar %}
    {{ usr_avatar }}
{% else %}
    {{ default_avatar }}
{% endif %}

替代 在你最初的询问中公正地使用切身:

try:
    usr_avatar = UsrAvatar.objects.get(user=request.user.id)        
except UsrAvatar.DoesNotExist: 
    usr_avatar = cache.get( default_avatar , False)
    if not usr_avatar:
        usr_avatar = UsrAvatar.objects.get(id= 0 )  

But 最后,甚至最好避免将违约ava留在数据库中,而只写上上文提到的背景处理器,而不要从亚洲开发银行获得违约ava,而只是静态的ur。

from django.conf import settings
def default_avatar(request):
    return {
         default_avatar  :  %simages/default_avatar.jpg  % settings.STATIC_URL
    }
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...