“在include()中指定一个名称空间而不提供app_name”。 HTML文件不在应用程序中,而是在项目目录中

时间:2018-07-31 19:42:03

标签: python django

我尝试这样做tutorial,但是我有一个问题。它引起了另一个版本的Django。我在论坛上看到过很多类似的话题,但我无法将它们与我的情况相匹配。

当尝试在以下文件中启动“ http://127.0.0.1:8000/accounts/login/”时,我会在消息“ / accounts / login /”中看到消息“ NoReverseMatch”。

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/login/

Django Version: 2.0.7
Python Version: 3.7.0
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrap3',
 'reviews')
Installed Middleware:
[]


Template error:
In template C:\Users\tymot\Desktop\aktualny_projekt-Podejscie_2\env\my_app\winerama\templates\base.html, error at line 21
   'auth' is not a registered namespace
   11 :         </div>
   12 :         <div id="navbar" class="navbar-collapse collapse">
   13 :             <ul class="nav navbar-nav">
   14 :                 <li><a href="{% url 'reviews:wine_list' %}">Wine list</a></li>
   15 :                 <li><a href="{% url 'reviews:review_list' %}">Home</a></li>
   16 :             </ul>
   17 :             <ul class="nav navbar-nav navbar-right">
   18 :             {% if user.is_authenticated %}
   19 :                 <li><a href="{% url 'auth:logout' %}">Logout</a></li>
   20 :                 {% else %}
   21 :                 <li><a href=" {% url 'auth:login' %} ">Login</a></li>
   22 :                 {% endif %}
   23 :             </ul>
   24 :         </div>
   25 :     </nav>
   26 :     
   27 : 
   28 :     <h1>{% block title %}(no title){% endblock %}</h1>
   29 : 
   30 :     {% bootstrap_messages %}
   31 : 


Traceback:

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\base.py" in reverse
  75.                 extra, resolver = resolver.namespace_dict[ns]

During handling of the above exception ('auth'), another exception occurred:

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = response.render()

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  175.                     return self._render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
  314.                 return nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\base.py" in reverse
  86.                     raise NoReverseMatch("%s is not a registered namespace" % key)

Exception Type: NoReverseMatch at /accounts/login/
Exception Value: 'auth' is not a registered namespace

我的urls.py:

urlpatterns = [
    path('reviews/', include('reviews.urls', namespace="reviews")),
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls',)) 

]

我的settings.py:

import os

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ')g@^(l6+_uc*ckf%rj2i+89v^4^r3k!$l76husfgc74nk1#&g+'

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

ALLOWED_HOSTS = []


# Application definition

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

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 = 'winerama.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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 = 'winerama.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 = 'UTC'

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/'

我的目录:

-templates
--base.html
--registration
---logged_out.html
---login.html
-winerama
--__init__.py
--settings.py
--urls.py
--wsgi
-reviews(all files from applications that work well)

然后我尝试在以下位置更改urls.py中的代码:

from django.contrib import admin
from django.urls import path, include

app_name = 'auth'

urlpatterns = [
    path('reviews/', include('reviews.urls', namespace="reviews")),
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls', namespace='auth'))

]

但是CMD返回一条消息:

'Specifying a namespace in include() without providing an app_name '

我试图更改login.html和urls.py中的代码,但效果也不佳。

'<li><a href="{% url 'login' %}">Login</a></li>' (login file)
'path('accounts/', include('django.contrib.auth.urls', namespace=auth))' (urls file)

我认为该错误是由应用程序的名称引起的('app_name ='auth')。我如何给他们正确的名称(如果我的HTML文件不在任何应用程序中间)?或者我该如何解决我的错误?

编辑(下一个测试1)

好吧,下面我写了我的步骤:

1。)我在base.html中对此进行了更改:

<li><a href="{% url 'logout' %}">Logout</a></li>

这:

<li><a href="{% url 'login' %}">Login</a></li>

2。)我将使用http://127.0.0.1:8000/accounts/login/

进入服务器

3。)他向我返回了信息:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/login/

Django Version: 2.0.7
Python Version: 3.7.0
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrap3',
 'reviews')
Installed Middleware:
[]

Template error:
In template C:\Users\tymot\Desktop\aktualny_projekt-Podejscie_2\env\my_app\winerama\templates\base.html, error at line 8
   'auth' is not a registered namespace
   1 : {% load bootstrap3 %}
   2 : 
   3 : {% bootstrap_css %}
   4 : {% bootstrap_javascript %}
   5 : 
   6 : {% block bootstrap3_content %}
   7 : <div class="container">
   8 :     <na v class="navbar navbar -default">
   9 :         <div class="navbar-header">
   10 :             <a class="navbar-brand" href="{% url 'reviews:review_list' %}">Winerama</a>
   11 :         </div>
   12 :         <div id="navbar" class="navbar-collapse collapse">
   13 :             <ul class="nav navbar-nav">
   14 :                 <li><a href="{% url 'reviews:wine_list' %}">Wine list</a></li>
   15 :                 <li><a href="{% url 'reviews:review_list' %}">Home</a></li>
   16 :             </ul>
   17 :             <ul class="nav navbar-nav navbar-right">
   18 :             {% if user.is_authenticated %}


Traceback:

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\base.py" in reverse
  75.                 extra, resolver = resolver.namespace_dict[ns]

During handling of the above exception ('auth'), another exception occurred:

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = response.render()

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  175.                     return self._render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
  167.         return self.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
  447.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\base.py" in reverse
  86.                     raise NoReverseMatch("%s is not a registered namespace" % key)

Exception Type: NoReverseMatch at /accounts/login/
Exception Value: 'auth' is not a registered namespace

编辑(下一个测试2)

好吧,如果显示html更改后出现错误('auth'未注册),我尝试添加'namespace ='auth'。

1.1)我正在从以下方式更改urls.py文件:

urlpatterns = [
...
    path(r'accounts/', include('django.contrib.auth.urls'))
]

对此:

urlpatterns = [
...
    path(r'accounts/', include('django.contrib.auth.urls', namespace='auth' ))
]

1.2)尝试启动服务器和CMD(命令行)会返回信息

'Specifying a namespace in include() without providing an app_name '

1.3)好吧,我尝试添加app_name:

app_name='django.contrib.auth' #or only 'auth'
urlpatterns = [
...
    path(r'accounts/', include('django.contrib.auth.urls', ))
]

1.4)她仍然返回“在include()中指定一个名称空间而不提供app_name”。在这里,我不知道该怎么办。

0 个答案:

没有答案