17.7. Views in Django: URL Redirection

Página 98

In Django, views are a crucial part of the system, as they are responsible for processing HTTP requests and returning an HTTP response. One of the important features of views is the ability to redirect URLs, which is essential for effective navigation on a website. In this chapter of our course on building systems with Python and Django, we'll explore the concept of redirecting URLs in depth.

In simple terms, URL redirection is a process that sends a user from one URL to another. This can be useful in many situations. For example, you might want to redirect users to a login page if they try to access a page that requires authentication. Alternatively, you may want to redirect users to a different page if the page they are trying to access does not exist.

To get started with redirecting URLs in Django, you'll need to import the redirect function from the django.shortcuts module. This function takes one argument which is the URL you want to redirect the user to.

Here is a simple example of how you can use the redirect function:

from django.shortcuts import redirect

def my_view(request):
    ...
    return redirect('/another-url/')

In this example, the my_view function redirects the user to '/another-url/'. Note that the URL you pass to the redirect function must be a string.

An important thing to note is that the redirect function does not terminate the execution of the view. This means that any code that follows the call to redirect will be executed, even if the user is redirected to another page. Therefore, it's good practice to place the call to redirect at the end of your view.

In addition to accepting a string representing a URL, the redirect function can also accept a template object. In this case, Django will call the model object's get_absolute_url method to get the URL to redirect the user to. This can be useful if the URL you want to redirect the user to depends on some data in the model object.

from django.shortcuts import redirect

def my_view(request):
    ...
    my_object = MyModel.objects.get(pk=1)
    return redirect(my_object)

Here, the my_view function redirects the user to the URL returned by the get_absolute_url method of the my_object object.

Another way to use the redirect function is to pass the name of a view as an argument. In this case, Django will use the reverse function to determine the URL of the view and redirect the user to that URL.

from django.shortcuts import redirect

def my_view(request):
    ...
    return redirect('name-of-view')

In this example, the my_view function redirects the user to the URL of the view whose name is 'name-of-view'. Note that the view name you pass to the redirect function must be a string.

In summary, URL redirection is a powerful technique that allows you to control the flow of navigation on your website. Django provides a redirect function to facilitate URL redirection, and this function can accept a string representing a URL, a model object, or the name of a view as an argument.

We hope this chapter has given you a clear understanding of how URL redirection works in Django. In the next chapter, we'll explore another important concept in Django: models.

Now answer the exercise about the content:

What does the redirect function in Django accept as an argument for redirecting URLs?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

9917.8. Views in Django: Working with Authentication

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text