【问题标题】:When trying to run a Django project I get the NoReverseMatch error尝试运行 Django 项目时出现 NoReverseMatch 错误
【发布时间】:2015-11-23 19:38:42
【问题描述】:

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

我的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$9%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>

无法找出问题所在。请帮忙。谢谢。

【问题讨论】:

    标签: django python-2.7 web-applications django-views


    【解决方案1】:

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

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

    然后使用:

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

    【讨论】:

    • 谢谢!我将代码编辑如下: from django.conf.urls import include, url from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns from .导入视图 urlpatterns = [ url(r'^courses/', include('courses.urls', namespace='courses')), url(r'^admin/', include(admin.site.urls)), url (r'^$', views.hello_world, name='hello_world'), Home ] urlpatterns += staticfiles_urlpatterns() 但它给出了一个现在语法错误。
    • 谢谢。是的,我做到了..正如上面评论中提到的那样。但它不起作用
    • @ledzee 这必须放在你的 html 文件中。替换当前的href 标签。
    • 谢谢。这是我现在的 layout.html 文件 {% load static from staticfiles %} ........ {% block content %}{% endblock %}
    【解决方案2】:

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

    【讨论】:

    • 谢谢。但这没有帮助。
    【解决方案3】:

    根据上面 Rohits 的建议,我进行了更改并且效果很好。 提示:不要注释掉更改,删除它们!引用 Rohit “Django 将在呈现 HTML 页面之前尝试解析 {% url %} 标记。因此,即使您将其注释掉,它仍会尝试反转 url”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2018-11-23
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      相关资源
      最近更新 更多