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
- RadioSelectWithHelpText by moxypark 1 year, 5 months ago
- GeoDjango maps in admin TabularInlines by alanB 1 year, 4 months ago
- Return to change_list with filter after change by graveyboat 5 months, 4 weeks ago
- Additional Change List Columns by sansmojo 4 years, 6 months ago
- Dynamic Models Revisited by Ben 4 years, 4 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.
#