Best way to make content in templates

0 views
Skip to first unread message

joaqui...@gmail.com

unread,
Dec 11, 2007, 10:21:27 AM12/11/07
to Django users
Hi folks,

I am new to Django and I want to know which is the best approach to
create content in templates for example:
- I need to call a logo image for the website, the logo location in
the system is referenced in a field of a model.

I want to display the logo by getting it from the system after
consulting the DB.

Can you tell me what's the best approach?

Thank you in advance.

l5x

unread,
Dec 11, 2007, 10:39:12 AM12/11/07
to Django users
Isn't it just putting variable into template? I don't know if I
understand you correctly but for example:

models.py

Image(models.Model)
name = CharField(...)
location = ImageField(...)

YourModel(models.Model)
image = ForeignKey(Image)
user = ForeignKey(User)

(...)

index.html:

<img src="/media/{{ my_model.image.location }}"
alt="{{ my_model.image.name }}" />

views.py:

(...)
from django.template import RequestContext
def my_request_context():
my_model = MyModel.objects.get(user=request.user)
# you can get the Image : my_model.image
return my_model # or my_model.image

and then use it when you need (rememer to add it to
TEMPLATE_CONTEXT_PROCESSORS in settings, and to call the request
context everytime you need it in views in 'render_to_response')

But I don'f now if it is what you need.

You should focus on http://www.djangoproject.com/documentation/templates_python/
(everything is written there)
Reply all
Reply to author
Forward
0 new messages