Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Final Details url Tag

Simon Amz
Simon Amz
4,606 Points

Get an HTML link tag for different views

Hi,

I'm trying to proceed to the linking of a button 'Home' to get back to the home page with. All others pages work, except this link. Here is my urls.py from project folder (not the app):

from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('courses/', include('courses.urls')),
    path('', views.hello_world, namespace='home'),
]

urlpatterns += staticfiles_urlpatterns()

Here is the layout.html from the project folder (not the app):

{% load static from staticfiles %}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{% block title %} {% endblock %}</title>
        <link rel="stylesheet" href="{% static '/css/layout.css' %}">
    </head>
    <body>
        <div class="site-container">
            <nav>
                <a href="{% url 'views:home' %}">Home</a>
            </nav>
            {% block content %} {% endblock %}
        </div>
    </body>
</html>

Without the a link everything works perfectly, soi I'm wondering if the problem comes from the fact that layout.html is in "templates" directory whereas urls.py is outside the "templates" directory.

Thanks for your help

Jeffrey James
Jeffrey James
2,636 Points

Your namespace for the link is "home", in the URL tag you're using "views" as a name space. Read this example and I bet you figure it out https://docs.djangoproject.com/en/2.0/intro/tutorial03/#namespacing-url-names

2 Answers

Haydar Al-Rikabi
Haydar Al-Rikabi
5,971 Points

First of all, 'namespace' cannot be used with path(). It can only be used with include().

Secondly, namespacing in Django 2.0 requires that you add the following line to your app's urls.py:

app_name = your_app_name

It is not mentioned in the video because Kenneth was not using Django 2.0.

Then as a matter of option, you can add the 'namespace' attribute to a url's include() module within the project's urls.py.

Using include(path_to_app_urls_file, namespace) without having added the 'app_name' as mentioned above will cause an error.

So, now you can refer to a url either by 'app_name':

{$ url 'your_app_name:url_name' %}

or by 'namespace':

{$ url 'url_namespace:url_name' %}
Flore W
Flore W
4,744 Points

Hi Haydar Al-Rikabi thanks for the explanation! Can you give an example on how using the namespace and defining the app_name would work in this particular instance with Django 2.0? I don't get what the app_name should be and also in which file it would be written (is it inside learning_site/urls.py ?)

Simon Amz
Simon Amz
4,606 Points

Jeffrey, thanks this is ok now.

good day!