13.7. Creating a Django Project: URLs and Routes

Página 65

13.7. Creating a Django Project: URLs and Routes

Creating a Django project involves many aspects, and one of the most crucial is configuring URLs and routes. In Django, URLs are used to map different parts of your site and help define how traffic is directed in your application. Routes, on the other hand, are used to determine the display and data processing logic for each URL. Let's explore these concepts more deeply.

Understanding URLs and Routes

URLs (Uniform Resource Locators) are essentially the web addresses you use to access different pages and resources on the Internet. In Django, URLs are used to map to different views in your application, allowing you to direct web traffic to different parts of your site based on the URL being accessed.

Routes, on the other hand, are a slightly more complex concept. In simple terms, a route is a pattern that is used to determine which view function should be used for a given URL. When a URL is accessed, Django uses the route pattern to determine which view function should be used to process the request.

Creating URLs in Django

The creation of URLs in Django is done in the urls.py file of each application. This file is used to define all URL mappings for the application. Each URL is defined using the path() or re_path() function, which takes a URL pattern and a view function as arguments.

For example, if you wanted to create a URL for your website's homepage, you could do it like this:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

In this example, the URL '' (which corresponds to the root of the site) is mapped to the view function home in the views module. The name argument is used to give the URL a name, which can be used to reference it in other parts of your code.

Creating Routes in Django

Creating routes in Django is done using the path() or re_path() function in the urls.py file. Each route is defined by providing a URL pattern and a preview function.

For example, if you wanted to create a route to the homepage URL, you could do it like this:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

In this example, the route '' (which corresponds to the site root) is mapped to the view function home in the views module.

Routes can also include arguments, which are passed to the view function. For example, you could create a route that includes a product ID as part of the URL, and then pass that ID to the view function so that it can display the correct product.

from django.urls import path
from . import views

urlpatterns = [
    path('product//', views.product_detail, name='product_detail'),
]

In this example, the route 'product//' matches any URL that begins with 'product/' followed by an integer. This integer is then passed to the product_detail view function as the product_id argument.

In short, creating URLs and routes is a fundamental part of developing a Django project. By understanding how these concepts work, you can create more efficient and effective websites that are easy to navigate and use.

Now answer the exercise about the content:

What is the role of URLs and routes in a Django project?

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

You missed! Try again.

Next page of the Free Ebook:

6613.8. Creating a Django Project: Forms and Validation

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