问题
I would like to create a single page in the admin site of django where I can change some global variables of the website (title of the website, items in the navigation menu, etc). At the moment I have them coded as context processors but I would like to make them editable. Something similar to what happens in WordPress.
Is this possible?
- I can store the data in the databse, but can I have a link in the admin site that goes straight to the first document record and doesnt allow the creation of multiple records (they wouldnt make sense)
- Instead of creating a model in the database, would it be possible to change some context_processor from the admin site (I think this would be best)
回答1:
django-preferences does exactly what you are looking for. The implementation is a bit hacky (particularly the setting of __module__
on the model class to trick Django into thinking it was loaded from a different app), but it works.
回答2:
This sounds like what the sites framework is intended to help with.
http://docs.djangoproject.com/en/dev/ref/contrib/sites/
"It’s a hook for associating objects and functionality to particular Web sites, and it’s a holding place for the domain names and “verbose” names of your Django-powered sites."
The docs make it sound like it's only good for multiple sites, but it's a great place to put stuff in a single-site-per-django model too.
回答3:
There's an app called django-values that allows you storing of specific settings in the database.
来源:https://stackoverflow.com/questions/3868939/django-admin-site-how-to-create-a-single-page-for-global-settings