Sins97
Sins97

Reputation: 387

Difference between render( ) and redirect( ) in django?

what is exactly the difference between render( ) and redirect( ) in django? I know redirect will send another request to the URL and render will render the template with the given context. But still something makes me not fully understand it. Maybe anyone can explain it well can help me a lot. Do I have to first render the template before using redirect function. For eg: I have two templates home.html and signin.html.

def home(request):
   return render(request, 'home.html')

def logout(request):
   return redirect('signin')

or

without writing def home can I redirect to home.html like below


def logout(request):
   return redirect('signin')

Upvotes: 0

Views: 658

Answers (1)

Mahammadhusain kadiwala
Mahammadhusain kadiwala

Reputation: 3635

In django render() methods used for serve HTML pages using HTTP request also render backed data using context dictionary

Ex -

def home(request):
   contex = {}
   return render(request, 'home.html',context)

Here home() function return render() method it's means it is surve home.html page. you can pass data dictionary as a context which surve dynamic data from home() function


And redirect() method used for after fulfilled request freshly redirect on specific url which passed in redirect() methods.

You cannot pass context in redirect() method.

Ex -

def logout(request):
   return redirect('signin')

After successfully logged out redirect on signin url, signin url called signin() function which is render signin.html page

Upvotes: 2

Related Questions