1👍
✅
You can add some data to global context through settings TEMPLATES > OPTIONS > context_processors
– Writing your own context processors.
And for extending templates use template tag extends
(Django Docs).
1. Adding variable to context
Create yourapp/context_processors.py:
from django.conf import settings
from yourapp.forms import MyForm
def base_data(request):
data = {}
# MyForm(request.GET, user=request.user)
data["my_form"] = MyForm()
data["my_email"] = settings.MYEMAIL
return data
And add it to context_processors
in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
...
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'yourapp.context_processors.base_data',
],
},
},
]
Now you can access your this data in any template:
{{ my_form }}, {{ my_email }}
2. Extending Templates
You can also use template tag extends
(Django Docs) to extend your templates:
template.html
base.html
In template.html, the following paths would be valid:
{% extends "base.html" %}
<div class="container">
Some HTML code
</div>
Additional info TEMPLATES
(Django Docs) and OPTIONS
(Django Docs) available parameters.
UPDATE
views.py:
def contact(request):
if request.method == 'POST':
form = BaseContactForm(request.POST)
if form.is_valid():
form.save()
return redirect('Home-Page')
else:
form = BaseContactForm()
return redirect(request.META.get('HTTP_REFERER'))
urls.py:
path('contact/', views.contact, name='contact'),
👤NKSM
Source:stackexchange.com