Help me get better! If you vote (either way) please leave a comment if you have time and say what was good or bad. I appreciate any and all feedback. Thanks!
I keep finding places in my apps where I need an isolated snippet of text that can periodically be changed from the admin interface. Most often it's html but sometimes it's text, javascript, or css.
Use it like so: (Assuming this snippet lives in snippy_snip/models.py and there is a snippet named "Welcome Message" in the database)
from snippy_snip.models import snip
msg = snip("Welcome Message")
Or, you might populate a parameter hash for a template:
def showpage(request):
params = {
'welcome': snip('Welcome Message'),
'video1': snip('Video 1'),
'NavHeader': snip('Nav.SectionHeader'),
}
return render_to_response("main.html", params)
For clarity, params might look something like this:
welcome -> "Welcome to our site. Please use the menu on the left..."
video1 - > a YouTube snippet
NavHeader -> Some HTML which comprises the top of a navigation menu.
This is a very simple bit of code but I've found it very useful. It isn't intended for instant changes... Your snippets will cache like anything else, which may cause confusion if you expect immediate changes. And it's probably not great for a high traffic site, but for my moderate traffic sites and workgroup apps I've found it useful.
(This code was created for 0.96, but I'm working to bring it into alignment with the latest svn version of Django, see comments.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from django.db import models
class Snippet(models.Model):
title = models.CharField(maxlength=64, unique=True)
content = models.TextField(blank=True)
description = models.TextField(blank=True)
# For Django 0.96 and previous
def __str__(self):
return self.__unicode__()
# For Django > 0.96
def __unicode__(self):
return self.title
class Admin:
pass
class Meta:
ordering = ['title']
def snip(name):
try:
return Snippet.objects.get(title=name).content
except:
return ""
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
first of all, str method in model long ago was replaced by unicode.
This try: except: is horrible. It should looks like
Any way. this ^^ is not so smart approach.
#
err
#
@KpoH, thanks for the feedback!
I'm on 0.96, which didn't call _unicode___ by default. But I've created a workaround which I think should be forward compatible. Tell me what you think:
Works fine on 0.96, but I don't have a trunk version to test against at the moment, so I only assume it would work with the latest svn.
As for the exception handling, yeah it's crude, but I'm happy with it in this particular situation.
#
and please also add unique=True to titles ;)
#
@burly - Done. Thank you!
#
This seems kinda like (almost exactly like) gettext in the i18n module. You might also end up with your Snippets confusing you at some point in the future when you start using snip("Submit") and decide that half of your buttons should read "Send" and half "Save" (as happened to me when translating to Japanese).
I like the idea but I'm not convinced its maintainable or even sticking to the DRY principle.
Is there a reason this can't be achieved with gettext/i18n and something like the rosetta interface?
#
Hi @aarond10ster! I'm not entirely sure I understood what you're going for, but I took a shot. Let me know how bad I've misunderstood you. :)
Mechanically, I think yes you could use Rosetta. It would be a bit like using a screwdriver as a hammer though. From what I've gathered you'd have to restart the webserver every time a user changed text and the .MOs were recompiled, but technically I think it would work. Of course if you tried to use Rosetta for actual localization at the same time that would probably get awkward fast.
To put it all in perspective, I use this code on small sites where the clients/users want to change information on a page frequently and they have a limited number of spots to change.
Let me know how I did with understanding you. Thanks!
#
Oh, and I should add some more clarification. SnippySnip is intended to work more like server-side includes or a hash table than anything (like localization) at this point.
#
Please login first before commenting.