django.core.exceptions.ImproperlyConfigured:“^(?$”不是有效的正则表达式:模式的意外结束

时间:2018-05-16 19:10:39

标签: python django django-urls

每次我尝试运行我的网站时,都会收到标题错误。 完整的事情如下:

Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x101c5bcf8>
Traceback (most recent call last):
  File "/Users/student/ENV/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Users/student/ENV/lib/python2.7/site-packages/django/urls/resolvers.py", line 255, in check
    warnings.extend(check_resolver(pattern))
  File "/Users/student/ENV/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Users/student/ENV/lib/python2.7/site-packages/django/urls/resolvers.py", line 172, in check
    warnings = self._check_pattern_startswith_slash()
  File "/Users/student/ENV/lib/python2.7/site-packages/django/urls/resolvers.py", line 140, in _check_pattern_startswith_slash
    regex_pattern = self.regex.pattern
  File "/Users/student/ENV/lib/python2.7/site-packages/django/urls/resolvers.py", line 93, in __get__
    instance.__dict__['regex'] = self._compile(instance._regex)
  File "/Users/student/ENV/lib/python2.7/site-packages/django/urls/resolvers.py", line 109, in _compile
    (regex, six.text_type(e))
django.core.exceptions.ImproperlyConfigured: "^(?$" is not a valid regular expression: unexpected end of pattern

我已经仔细检查了我的urls.py,但错误的来源似乎并不存在

from django.conf.urls import url 
from . import views 

#these are the urls. Observe and about are for the future, data is just gonna host the skymap and the datatables. 
#I have nothing for observe and about, leave them commented out for now plz
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    ##url(r'^observe/', views.observe, name='observatories'),
    ##url(r'^about/', views.about, name="about"),
    url(r'^data/', views.data), 

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

我还检查了我的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__)))


# 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 = 'y@rn-kswhu)c@ae@)-s952xdfxb_)7yswq+pvm+63*n8i89^2d'

# 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',
    'django.contrib.sites', 
    'data.apps.DataConfig',
]

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 = 'icecubesite.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 = 'icecubesite.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'icecube', 
        'USER': 'root', 
        'PASSWORD': 'NewPassword', 
        'HOST': '', 
        'PORT': ''
    }
}


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

Views.py

from __future__ import unicode_literals 
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic

from django.http import HttpResponse
from django.shortcuts import render 
from .models import Cubedata

# Test index 

#hey, this is a table containing just like at entire database as an object.
def table(request):  
    tables = Cubedata.objects.all() 
    template_name = "data/table.html" 
    data = { 
        'tables' : tables , 
    } 
    return render_to_response (template, data, 
        context_instance = RequestContext( request ))

我真的认为我的错误在于我有Spaghetti代码,但是我真的不确定这个错误来自哪里。我在代码中的任何地方都找不到它;我已多次梳理它。如果需要,我会发布我的所有文件,但我发布的文件最有可能是错误的。

2 个答案:

答案 0 :(得分:0)

如果您无法在** / urls.py中的任何位置找到r'^(?$'表达式,那么很可能不在那里。

r'^(?$'潜伏的下一个最佳位置是* .pyc文件。它们往往被遗忘,在极少数情况下会引起很大的麻烦。只要摆脱它们,r'^(?$'的可能性也会消失。

希望有帮助和快乐的编码!

答案 1 :(得分:-2)

问题在于正则表达式“^(?$”,您必须更正它。

您对该正则表达式的预期行为是什么?

(添加到对匹配进行分组,但括号未关闭。