Why does the Django render () function need a query argument?

Sorry for what might be a dumb question, but why is the request argument required in the render() function?

+8
source share
3 answers

render() shortcut displays templates with the request context . The template context processors take the request object and return the dictionary that is added to the context.

A generic context context handler is an ach context handler that accepts a request object and adds a registered user to the context.

If you do not need to render template with a request context, you can use request=None .

 def my_view(request): return render(None, "my_template.html", {'foo': 'bar'}) 
+7
source

In django, render is used to load templates. Therefore, for this we

 import-from django.shortcuts import render 

its template shortcut. Rendering is the process of collecting data (if any) and loading related templates

0
source

To render a template outside the view context (i.e. without a request object), you can use render_to_string() :

 from django.template.loader import render_to_string render_to_string('path/to/template.html', context={'key': 'val'}) 
0
source

Source: https://habr.com/ru/post/1201710/


All Articles