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

link not showing, got a 500 error

Hello there everyone, so i got this error out of nowhere and don't understand why. Here's my code:

{% load static from staticfiles %}
<html>
    <head>
        <title>{% block title %}{% endblock %}</title>
        <link rel="stylesheet" href="{% static 'css/layout.css' %}">
    </head>
    <body>
        <nav>
            <a href="{% url 'views.hello_world' %}">Home</a>
        </nav>
        <div class="site-container">
        {% block content %}{% endblock %}
        </div>
    </body>
</html>

3 Answers

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Hi, Ammar Fatihallah ! Do you get it on your local machine? Which version of django do you use?

Since Django 1.10 using {% url 'views.hello_world' %} is deprecated. You should give your views names in urls.py, and call them by name like {% url 'viewname' %}

Name should be set in urls.py like this:

url(r"^$", views.your_view, name='viewname'),

"Namespaces are one honking great idea -- let's do more of those!"

Does it help?

I'm actually using Django 1.11 on my local machine. I tried to do what you told me but I got a NoReverseMatch at / error. It also said that Reverse for 'views.hello_world' not found. 'views.hello_world' is not a valid view function or pattern name..

"""learning_site URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views

urlpatterns = [
    url(r'^courses/', include('courses.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.hello_world, name='hello_world'),
]

urlpatterns += staticfiles_urlpatterns()
Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Ammar Fatihallah , then it's href.

urls.py is good.

There is a href to your 'hello_world' somewhere in templates, which has to look like this:

 <a href="{% url 'hello_world' %}">Hello World</a>

Find it and you're done!;)

Got it working ! thanks Alexey :)

quick question, how can i add a link for a view in another app ?

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Example (with your urls.py)

you have:

    ###
    url(r'^courses/', include('courses.urls')),
    ###

You can add namespace to courses app's views:

    ###
    url(r'^courses/', include('courses.urls', namespace='courses')),
    ###

Then name all the views in courses/urls.py:

    ###
    url(r'^$', views.course_list, name='all'),
    ###

Then you can make hrefs in templates:

 <a href="{% url 'courses:list' %}">All Courses</a>

Actually, namespaces are not necessary, you can call your views by just names, but if you have same named views in different apps - they will conflict.

Thanks ALOT for your help man ❤ Does treehouse teach these things in their videos or did you learn them from somewhere else?