Django AttributeError 'tuple' 对象没有属性 'regex'

Posted

技术标签:

【中文标题】Django AttributeError \'tuple\' 对象没有属性 \'regex\'【英文标题】:Django AttributeError 'tuple' object has no attribute 'regex'Django AttributeError 'tuple' 对象没有属性 'regex' 【发布时间】:2015-06-22 11:04:56 【问题描述】:

我正在使用 django 1.8 并且遇到了问题。我正在尝试在我的项目中导入 tinymce。当我渲染它时,它被捕获了

AttributeError: tuple' object has no attribute 'regex'

当我删除 url.py 中的 url 时,它正在工作。这是我的代码。

url.py

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

urlpatterns = [
    # Examples:
    # url(r'^$', 'hizlinot.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),

]

settings.py

"""
Django settings for hizlinot project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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 = 'b-4jipu5t(+)g(2-7g#s=1rs19dhpj-1-!x1b-*v7s85f-m%&q'

# 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',
    'edebiyat',
    'tinymce',
)

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

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'hizlinot.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/'

【问题讨论】:

【参考方案1】:

您忘记了“url

url(r'^admin/', include(admin.site.urls)),
url(r'^tinymce/', include('tinymce.urls')),

urlpatterns 应该是 url() 实例的列表

url 返回RegexURLPattern,但在您的列表中找到了一个元组。

https://docs.djangoproject.com/en/1.8/_modules/django/conf/urls/#url

【讨论】:

感谢@DTing 的快速响应我尝试在tinymce url 中添加'url' 开头,但这次它没有出现htmlField。它似乎是 textarea 而不是 HTMLField。 models.py 很好 from tinymce.models import HTMLField someHtmlEditor = HTMLField(null=True, verbose_name='Karakterlerin İncelemesi') 这超出了原始问题的范围。用 cmets 说话很难判断问题出在哪里。尝试发布另一个带有相关代码和错误的问题。 问题已解决。非常感谢@DTing。我之前更改了tinymce settings.py,但我忘记了。当我修复 Tinymce setting.py 时,我会应用您的建议它有效! :) 谢谢【参考方案2】:

我知道它与问题并不绝对相关,但有时这个错误可能比直接在 urls.py 文件中更深一些。

我收到此错误,问题的原因不在错误堆栈跟踪中。

我在使用自定义 Admin Class 浏览 Admin 时遇到了这个问题,问题出在该类的方法 get_urls() 上,该方法返回如下内容:

def get_urls(self):
    from django.conf.urls import patterns
    return ['',
            (r'^(\d+)/password/$',
             self.admin_site.admin_view(self.user_change_password))] + super(CompanyUserAdmin, self).get_urls()

修复它:

def get_urls(self):
    from django.conf.urls import patterns
    return [
            url(r'^(\d+)/password/$',
             self.admin_site.admin_view(self.user_change_password))] + \
           super(CompanyUserAdmin, self).get_urls()

不要忘记'url'的导入:

from django.conf.urls import url

【讨论】:

以上是关于Django AttributeError 'tuple' 对象没有属性 'regex'的主要内容,如果未能解决你的问题,请参考以下文章

Django 3:AttributeError:'AdminSite'对象没有属性'Register' [关闭]

Django 2.2 + AttributeError:'str'对象没有属性'decode'

AttributeError:模块'django.db.models'没有属性'DataField'[关闭]

AttributeError:模块 'django.contrib.postgres.fields' 没有属性 'JSONField'

AttributeError:'str'对象没有属性'regex'django 1.9

AttributeError: 'ManyToManyDescriptor' 对象没有属性 'all' - django