What is the difference between render () and redirect () in Django?

What's the difference between

def post(self, request, *args, **kwargs): if form.is_valid(): order = form.save(commit=False) order.user = request.user order.save() return redirect('orders:success') 

and

 def post(self, request, *args, **kwargs): if form.is_valid(): order = form.save(commit=False) order.user = request.user order.save() return render( request, 'orders/success.html', {} ) 

I think they are exactly the same.

Any difference?

+5
source share
2 answers

Rendering Function Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.

You request a page, and the render function returns it.

The redirect function sends another request to the specified URL.

+3
source

Both are completely different where the redirection gives an HttpResponseRedirect for the argument you passed.

Example

 return redirect('https://example.com/') 

will take you to https://example.com/

  return render(request,'/result.html',{'foo':'bar'}) 

maps the context dictionary to the template "result.html" and returns an HttpResponse object with this rendered text

result.html ... {} Foo ... where foo will be replaced by a bar For more details see django docs

+3
source

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


All Articles