Django Unique properties in admin
I'm building a generic template that will be deployed for several sites, one of the customizing options we'd like to allow would be a custom font for the title text. I'd like to add this property to the admin interface. Is there a better technique then creating a model for these properties and doing a Model.obje开发者_StackOverflow社区cts.get() to retrieve 1 instance.
Thanks in advance
I'd suggest using a context processor, and only hitting the database on first request, or if you forcefully trigger a reload of the settings. For instance:
from project.theme.models import Theme
THEME_SETTINGS = Theme.objects.values().get(id=1) # hits db first request
def theme(request):
if 'reset_theme' in request.GET: # or some other mechanism
THEME_SETTINGS = Theme.objects.values().get(id=1) # reset theme settings
return THEME_SETTINGS
To use, just add the context processor to TEMPLATE_CONTEXT_PROCESSORS
in your settings. Context processors are fairly simple, they take a RequestContext
and return a dict used to populate the RequestContext
.
I've currently implemented django-chunks from github It allows the use of
{% load chunks %}
{% chunk "key" %}
where the chunk is a key,value set.
I've used this technique to insert "dynamic css", (my admin can provide the css required to upload a custom font)
Example: subtitle_font_css
<style>
@font-face {
font-family: "impact";
src: url('/static/fonts/impact.ttf');
}
</style>
subtitle_font
font-family: impact;
Now for the html:
<head>
{% load chunks %}
{% chunk "subtitle_font_css" %}
</head>
<body>
{% load chunks %}
<span style="{% chunk "subtitle_font %}"> Title </span>
</body>
精彩评论