To add in a CORS settings I can do something like this on a particular function:
def clear(request):
# ... something
response = HttpResponse('OK')
response["Access-Control-Allow-Origin"] = "*"
return response
However, I'm having difficulty adding it on the shortform render
method:
def function(request):
# how to modify the response header here?
return render(request, 'page.html', data)
How would I update some of the headers in the render response?
render()
method combines a given template
with a given context dictionary
and returns an HttpResponse
object with that rendered text. Refer here .
You can store the result of the render function in a variable called as response and then set cookies to it as you would normally do.
Your view function should be
def function(request):
response = render(request, 'page.html', data)
response["Access-Control-Allow-Origin"] = "*"
return response
render
also returns an HttpResponse
object:
Combines a given template with a given context dictionary and returns an
HttpResponse
object with that rendered text.
So you can do the exact same thing you did above on the 'raw' HttpResponse
object:
def function(request):
response = render(request, 'page.html', data)
response["Access-Control-Allow-Origin"] = "*"
return response
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.