Django Tutorial #2: URL Configuration

    Django Tutorial #2: URL Configuration

    More Tutorials on Web Development


    In the second part of this tutorial, we are going to talk about the URL configurations of Django, which is the entry point to all websites.

    Basic Configuration

    The URL configurations are stored in Django_31_Tutorial/urls.py, the most basic URL configuration looks like this:

    from django.urls import path
    
    from blog import views
    
    urlpatterns = [
        path('', views.home),
    ]

    It reads information from a URL and returns a viewDo not confuse this view with the view in Laravel. They are two different things, and we’ll talk about this later on.

    In this example, if the domain is not followed by any argument (http://www.mydomain.com/), Django will return the home view.

    Passing Parameter

    It is often necessary to pass some extra parameters to the view. For example, you want to show a blog post page, you’ll need to pass the post id or the slug to the view so that you can use this extra information to find the blog post you are looking for. This is how we can pass a parameter:

    from django.urls import path
    
    from blog import views
    
    urlpatterns = [
        path('post/<int:id>', views.post),
    ]

    The angle brackets will capture a part of the URL as a parameter. On the left side, the int is called a path converter. It captures an integer parameter. If a converter isn’t included, any string, excluding a / character, is matched. On the right side, it is the name of the parameter. We’ll need to use it in the view.

    The following path converters are available by default:

    • str – Matches any non-empty string, excluding the path separator, '/'. This is the default if a converter isn’t included in the expression.
    • int – Matches zero or any positive integer. Returns an int.
    • slug – Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
    • uuid – Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
    • path – Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than a segment of a URL path as with str.

    Reverse Resolution of URLs

    So far, we talked about finding a webpage using an URL, but sometimes, we need to embed an URL inside a webpage. To do that, we need to give the URL pattern a name first:

    from django.urls import path
    
    from blog import views
    
    urlpatterns = [
        path('post/<int:id>', views.post, name='post'),
    ]

    To use this URL pattern in the template:

    <a href="{% url 'post' 12 %}">Post with id "12"</a>

    When the users click on this link, they will be taken to the blog post whose id is 12. Of course, hard coding an id is meaningless in a real-life project. But don’t worry, we’ll talk about the views and templates in the next article.

    Include Other URL Configurations

    Imagine you have a Django project with 10 apps in it, if you put all URL configurations in one file, it will be really messy. When this happens, we can separate the URL configurations into different apps. For example, in our project, we can create a new urls.py inside the blog app.

    Django_31_Tutorial/urls.py

    from django.urls import path, include
    
    urlpatterns = [
        path('blog/', include('blog.urls'))
    ]

    This means if the URL is in the form of http://www.mydomain.com/blog/xxxx, Django will go to blog/urls.py and match for the rest of the URL.

    Define the URL patterns for the blog app in the exact same way:

    from django.urls import path
    
    from blog import views
    
    urlpatterns = [
        path('post/<int:id>', views.post, name='post'),
    ]

    This pattern will match the URL in the form of: http://www.mydomain.com/blog/post/123

    Leave a Reply

    Your email address will not be published. Required fields are marked *