Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Links to Views | Templates
Django: First Dive

bookLinks to Views

The DTL allows us to create links to Views directly. What is meant by this? The URL addresses to Views can be changed while the development process. DTL finds the using URL automatically through the URL pattern name.

Look at the implemented URL pattern inside the urls.py file:

app_name = "NewApp"

urlpatterns = [
    path("", hello_world_view, name="hello-world"),
    path("posts/", post_list, name="post-list"),
    path("posts/<int:pk>/", post_get, name="post-get"),
    path("posts/<int:pk>/update/", post_update, name="post-update"),
    path("posts/<int:pk>/delete/", post_delete, name="post-delete"),
    path("posts/create/", post_create, name="post-create"),
]

The DTL has tools that insert links from the URL pattern through the pattern name into the HTML page. Such operations are performed using the following syntax:

{% url 'AppName:pattern_name' %}

For example, the link to the post list:

<a href="{% url 'NewApp:post-list' %}">Post List</a>

Note

The quotes inside the url function and the href parameter should be different.

Result

Now we have this link in the main page in the browser:

The HTML document structure in browser:

The link opens the http://127.0.0.1:8000/posts/ URL address in your browser.

Thus, you can create links to any endpoint of your application, and changing the URL address will not affect the pages, which protects you from additional changes in the HTML pages.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 5

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 3.45

bookLinks to Views

Glissez pour afficher le menu

The DTL allows us to create links to Views directly. What is meant by this? The URL addresses to Views can be changed while the development process. DTL finds the using URL automatically through the URL pattern name.

Look at the implemented URL pattern inside the urls.py file:

app_name = "NewApp"

urlpatterns = [
    path("", hello_world_view, name="hello-world"),
    path("posts/", post_list, name="post-list"),
    path("posts/<int:pk>/", post_get, name="post-get"),
    path("posts/<int:pk>/update/", post_update, name="post-update"),
    path("posts/<int:pk>/delete/", post_delete, name="post-delete"),
    path("posts/create/", post_create, name="post-create"),
]

The DTL has tools that insert links from the URL pattern through the pattern name into the HTML page. Such operations are performed using the following syntax:

{% url 'AppName:pattern_name' %}

For example, the link to the post list:

<a href="{% url 'NewApp:post-list' %}">Post List</a>

Note

The quotes inside the url function and the href parameter should be different.

Result

Now we have this link in the main page in the browser:

The HTML document structure in browser:

The link opens the http://127.0.0.1:8000/posts/ URL address in your browser.

Thus, you can create links to any endpoint of your application, and changing the URL address will not affect the pages, which protects you from additional changes in the HTML pages.

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 5
some-alt