This article is to make a model used to save string label in site (e.g. site_title
), like this:
1 | class Label(models.Model): |
And, make a form like this to update it:
1 | Site Configuration |
There are some package like jezdez/django-constance to do this task, but I would rather not to use too many 3rd part package in project. Though, I use
django-crispy-forms
.
settings.py
Define your labels in settings.py
:
1 | LABELS = [ |
models.py
1 | from django.conf import settings |
forms.py
We define save()
method to save request.POST
into our model.
1 | from django import forms |
views.py
1 | from pages.models import Label |
label_settings.html
This page is used to edit/save our labels.1
2
3<form method="post" action="">
{% crispy form %}
</form>
How To Use In Template?
templatetags/common_tags.py
1 | from django import template |
Now, we can use it!
1 | {% load common_tags %} |