Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django

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?

like image 343
user3595632 Avatar asked Oct 08 '16 08:10

user3595632


People also ask

What is difference between render and redirect?

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.

What is redirect Django?

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 .

What is the difference between render and HttpResponse?

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.

What is render in Django?

Rendering means interpolating the template with context data and returning the resulting string. The Django template language is Django's own template system.

What is the difference between redirect and render in node JS?

-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.

What does return redirect do in Django?

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.


1 Answers

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.

like image 199
kushtrimh Avatar answered Oct 07 '22 03:10

kushtrimh