What's 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 these are totally same.
Any difference?
There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.
Django Redirects: A Super Simple Example In Django, you redirect the user to another URL by returning an instance of HttpResponseRedirect or HttpResponsePermanentRedirect from your view. The simplest way to do this is to use the function redirect() from the module django.shortcuts .
Abid covers it, render is usually used to load a template and a context, while HttpResponse is usually for data. As it's bad practice to "respond" with html. Render is essentially a shortuct for HttpResponse , It provides a more effective way to modify templates, and load data dynamically.
Rendering means interpolating the template with context data and returning the resulting string. The Django template language is Django's own template system.
-Redirect is used to tell the browser to issue a new request. Whereas, render only works in case the controller is being set up properly with the variables that needs to be rendered.
redirect()Returns an HttpResponseRedirect to the appropriate URL for the arguments passed. The arguments could be: A model: the model's get_absolute_url() function will be called. A view name, possibly with arguments: reverse() will be used to reverse-resolve the name.
The render 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 given url.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With