简体   繁体   中英

How can i get form data from django layouts (like 'base.html') without sending forms in all page through views?

many templates extend from base.html. base.html have one newsletter form that get email from user. Is there any easy way to get the form data from 'base.html' to view. (Sending forms in all page through views is possible But I think there is A easy good Looking idea)

Use a context processor to do so:

Add a new .py file in your root project, name it context_processors.py .

context_processors.py

from app.forms import GlobalForm

def global_variables(request):
   form = GlobalForm()
   context = {'global_form':form}
   return context

Then in settings.TEMPLATES add the context processors as the last line.

# stuff
'django.contrib.messages.context_processors.messages'
'project_name.context_processors.global_variables'

So, {{global_form}} is available in all templates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM