Use Django subdomains to switch out the base template

I would like to add a "dev" subdomain to my Django site for publicly testing experimental layouts. All of these experiments operate on my base.html template. (For example, I might try a new CSS file coupled with some more fluid containers.) At the moment, this alternate template lives in a base_alt.html file.

I have the middleware set up so that the request object has a correct subdomain property, and I'm using the django.core.context_processors.request context processor to pass this property to the template itself. But I cannot decide on the correct place to put the logic to swap out the base file. Here are my ideas:

Logic in each template that inherits base.html:

{% ifequal request.subdomain "dev" %}
    {% extends "base_alt.html" %}
{% else %}
    {% extends "base.html" %}
{% endifequal %}

This doesn't work, for one thing. I assume I'm doing something stupid wrong. But it also seems like a terrible solution -- at least a dozen page-level templates inherit base.

The other mediocre idea I had was to put both base templates in base.html with a giant "if" statement.

Surely there's a better way?

Answers


How about different Django configuration files for each site, with different template paths configured?

If you don't want to copy all the templates, you can give the dev site two template directories - one with your modifications, then (later in the path) the live site's template directory. If you don't have a template in your dev templates directory, Django will use the live one.

You don't need to duplicate the entire configuration file - your dev configuration file should be able to do import mysite.config_live (or whatever your live config is called) then just override the one setting you want to change.


I really liked @user9876's idea, but I found a simpler way that suits my limited needs. I made a custom context processor:

def base_template(request):
    print request.subdomain
    if request.subdomain == "dev":
        return {'base_temp' : 'base_alt.html'}
    return {'base_temp' : 'base.html'}

Then I change the base template call in my page-level templates to:

 {% extends base_temp %}

This also means I no longer need to send the request vars to the template.

This does require a one-time change to all the inherited templates, but after I do so I can extend the mapping of subdomains to base templates. If my project gets more complex than that, I'll definitely move to multiple settings files. Thank you!


Need Your Help

In which namespace is the extension method EntityTypeConfiguration.MapSingleType

asp.net-mvc-3 entity-framework entity-framework-4 asp.net-mvc-4

I am trying to implement the following code to map a database to a POCO that has slightly different Property names then the corresponding columns, and a slightly different class name than the table...

Capturing System.out.println or stdout data to a swing memo (Huge Data)

java string swing list stdout

Hi I need to show the result in a memo instead using System.out.println, but isn't possible to put the stdout in a list for example and after display the contents of this list in swing memo because I

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.