Tarun Gaba
Tarun Gaba

Reputation: 1113

Django : Customizable templates

I am working on creating a web portal and I want to offer the users the feature of making changes to there profile/dashboard like changed background. etc. Can anybody please guide me to an efficient approach to achieve this? Thanks

Upvotes: 3

Views: 97

Answers (1)

rantanplan
rantanplan

Reputation: 7450

This is trickery that has more to do with css and javascript than with django templates.

The only thing that is django related here is the actual storing of these preferences.

e.g. the filepaths of the actual background images.

After that you will do something similar to what is described in this answer:

how to change html background dynamically

EDIT

I don't see why you need different directories for every user. Django templates give you more than enough power to do what you want.

For example, let's say that each user can upload his own background picture. Also I assume that you follow this popular django pattern for storing additional information about your users. https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

So we have this UserProfile model:

class UserProfile(models.Model):
    CHOICES = (
        ('vertical', 'Vertical'),
        ('horizontal', 'Horizontal'),
    )
    user = models.OneToOneField(User)
    background_image = models.ImageField(upload_to='images')
    dashboard_layout  = models.CharField(max_length=10, choices=CHOICES)

You can pass this extra information to your javascript context(either with Ajax or without) and then change the background image for every individual user.

Also we could do special layout at the template level like this:

{% extends "base.html" %}
{% block main_body %}
    {% if request.user.get_profile.dashboard_layout == 'vertical' %}
        {% include "layouts/vertical.html" %}
    {% else %}
        {% include "layouts/horizontal.html" %}
    {% endif %}
{% endblock main_body %}

Upvotes: 2

Related Questions