尝试 运行 Django 项目时出现 NoReverseMatch 错误

When trying to run a Django project I get the NoReverseMatch error

我正在尝试在本地服务器上 运行 这个项目,但出现以下错误:

我的url文件如下:

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', namespace='courses')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.hello_world, name='hello_world'),
]

urlpatterns += staticfiles_urlpatterns()

这是视图文件

from django.shortcuts import render


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

这是设置文件

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '&s5%_8r2y%pbnph*xy*%v^a_!vc0bmbqz%(+l#pc@k7n2r)+'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'courses',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'learning_site.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'learning_site.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Los_Angeles'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'assets'),
)

And this is the home.html file

{% extends "layout.html" %}

{% block title %}Well hello there!{% endblock %}

{% block content %}
<h1>Welcome!</h1>
{% endblock %}

这是模板文件:

{% load static from staticfiles %}

<!doctype html>
<html>
    <head>
        <title>{% block title %}{% endblock %}</title>
        <link rel="stylesheet" href="{% static 'css/layout.css' %}">
    </head>
    <body>
        <div class="site-container">
            <nav>
                <a href="{% url 'views.hello_world' %}">Home</a>
                <a href="{% url 'courses:list' %}">Courses</a>
            </nav>
            {% block content %}{% endblock %}
        </div>
    </body>
</html>

这是 layout.html 文件

{% load static from staticfiles %}

<!doctype html>
<html>
    <head>
        <title>{% block title %}{% endblock %}</title>
        <link rel="stylesheet" href="{% static 'css/layout.css' %}">
    </head>
    <body>
        <div class="site-container">
            <nav>
                <!-- <a href="{% url 'views.hello_world' %}">Home</a> -->
                <a href="{% url 'hello_world' %}">Home</a>
                <a href="{% url 'courses:list' %}">Courses</a>
            </nav>
            {% block content %}{% endblock %}
        </div>
    </body>
</html>

无法弄清楚问题出在哪里。请帮忙。谢谢。

您正在尝试为 'views.helloworld' 反转 url。但是,那不是您定义的 url 的名称。那是视图名称。将您的 urls.py 文件更改为:

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

然后使用:

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

我认为问题可能出在 urls.py 文件中 您可以尝试将视图导入为
from courses import views

根据 Rohits 的上述建议,我进行了更改并且成功了。 提示:不要注释掉更改,删除它们!引用 Rohit "Django will try to resolve the {% url %} tags before rendering the HTML page. So, even though you comment that out, it still tries to reverse the url"