Dev Bingo Dev Bingo - 1 year ago 72
Python Question

NoReverseMatch at /courses/course/1/1/

I was editing the template to include a hyperlink. But when I do I get NoReverseMatch error.

Reverse for 'views.hello_world' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

The template file:


{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/layout.css' %}">

<div class="site-container">
<a href="{% url 'views.hello_world' %}">Home</a> [**Error here**]
{% block content %}{% endblock %}


from django.conf.urls import include, url
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'^$', views.hello_world)

from django.shortcuts import render

def hello_world(request):
return render(request, 'home.html')

The line,
when removed I don't get any error. But I add, the NoReverseMatch arises. What am I doing wrong?

Answer Source

You need to give the URL a name, and refer to that name in the url tag.

url(r'^$', views.hello_world, name='hello_world')


<a href="{% url 'hello_world' %}">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download