Django 2.0,使用 check_method 导致最大递归的 URL 路由

Posted

技术标签:

【中文标题】Django 2.0,使用 check_method 导致最大递归的 URL 路由【英文标题】:Django 2.0, URL Routes Causing Max Recursion with check_method 【发布时间】:2018-12-18 21:16:25 【问题描述】:

我对 Django 完全陌生,将 Django“应用程序”添加到创建的 Django“项目”时遇到问题。我正在使用 Docker 和 Docker-Compose,当我尝试使用添加的“文档”应用程序构建和启动我的实例时,它会引发最大的递归错误。

如果我从项目中删除文档应用程序,则不会出现此问题,因此显然我的应用程序配置错误。

是否有人看到我在将默认 URL ('') 设置为文档中的索引视图时做错了什么?

目录结构

app/
  |-django_nlp/
    |-settings.py
    |-urls.py
    |-wsgi.py
  |-documents/
    |-templates/
      |-index.html
    |-apps.py
    |-views.py
  |-Dockerfile
  |-docker-compose.yml
  |-manage.py

django_nlp/settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# /usr/src/app

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ixoz#2d4=m#k#%1#!hhr2ei82t4x$$e)n9oxrq66mzq556k59@'

# 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',
    'documents'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'django_nlp.urls'

#Get the absolute path of the settings.py file's directory
BASE_PATH = os.path.dirname(os.path.realpath(__file__ ))
# /usr/src/app/django_nlp


TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or
    # "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

    #Add Templates to the absolute directory
    # os.path.join(BASE_PATH, "templates")
)

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR,
            BASE_PATH
        ],
        '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 = 'django_nlp.wsgi.application'


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

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



# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    ,
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/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.11/howto/static-files/

STATIC_URL = '/static/'

django_nlp/urls.py

from django.conf.urls import url
from django.contrib import admin

# from documents import views
# import documents
from documents.views import Index


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', Index.as_view())
    # url(r'$^', documents.views.index, name='index')
]

documents/views.py

from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView


# def index(request):
    # return HttpResponse('Hello, welcome to the index page.')

class Index(TemplateView):
    template_name = "text-form.html"
    data = 'text': ''

    def get(self, request, *args, **kwargs):
        form = self.form_class(data=self.data)
        return render(request, self.template_name, data)

感谢您的宝贵时间!

【问题讨论】:

你应该粘贴错误日志 【参考方案1】:

如果您使用 Django 2.0,您应该阅读此文档:https://docs.djangoproject.com/en/2.0/ref/urls/,此版本中的 url 配置已更改。但是,如果你真的想在 Django 2.0 中使用正则表达式,你可以更改:

from django.conf.urls import url

到;

from django.urls import re_path

你的例子;

from django.conf.urls import url

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', Index.as_view())
    # url(r'$^', documents.views.index, name='index')
]

到;

from django.urls import re_path

urlpatterns = [
    re_path(r'^admin/', admin.site.urls),
    re_path(r'^$', Index.as_view()),
]

【讨论】:

谢谢!我错过了迁移到 2.0 的变化。 这解决了我的问题的一部分,我遇到的另一个问题是使用表单数据的基于类的视图的错误实现。我还需要从 django 导入表单添加并通过“self.variableName”将我的变量传递给我的模板。现在它起作用了!非常感谢!

以上是关于Django 2.0,使用 check_method 导致最大递归的 URL 路由的主要内容,如果未能解决你的问题,请参考以下文章

Django 2.0 url() 到 path()

Django 2.0 使用脚本填充数据库

我无法从 Django 1.8 升级到 2.0

AJAX 路由到 Django URL(使用 Django 2.0“路径”urlpatterns)

使用电子邮件注册,Django 2.0

在 Django 2.0 中使用 Slack RTM API