THE URLS IN DJANGO
URLS are used to map the resources of our application, and are fundamental in the Django architecture.
We create an urls.py file within the news application and include it in the project’s urls.py file. As shown in the image.
While the application’s urls.py file we modify it as follows:
We have seen that the path function is used to map a specific url to a view. In the image above you can see how angle brackets are used to define the detail of an article. We can pass the value of these angle brackets to our functions. In our case, after creating the new view articoloDetailView we pass to it the primary key, as defined in the path function to retrieve the details of an article.
Regarding the creation of the new HTML file articleDetail.html, there is a clarification to be made by looking at the project’s settings.py file.
The APP_DIRS = True entry specifies to Django whether or not it should search for HTML files within our application. So let’s create a templates folder inside the news application and let’s create our HTML file inside.
To specify the links that from the homepage will take us to the detail of the article, we use the get_absolute_url function. Let’s go into the model and create this function as shown in the image below.
articleDetail was declared in the name parameter of the application’s urls.py file. This function is used to create more robust links because they are generated by the system, if we change article/<int:pk> to articles/<int:pk> the link will not break. Let’s now go to the homepage to define these links.
Leave A Comment