python - Django:匹配查询不存在且 django.core.exceptions.ImproperlyConfigured

标签 python mysql django sqlite

我在 SQLite 上有一个完全运行的应用程序,现在为了生产,我必须将其配置为使用 MySQL。我安装了 MySQL 和 python 客户端,将我的 settings.py 更改为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_password',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

现在,当我运行服务器时,网站的管理部分工作正常,但是当我尝试登录实际网站时,出现以下错误:

DoesNotExist at /logins/
CustomUser matching query does not exist.
Request Method: GET
Request URL:    http://127.0.0.1:8000/logins/
Django Version: 1.6
Exception Type: DoesNotExist
Exception Value:    
CustomUser matching query does not exist.
Exception Location: /Library/Python/2.7/site-packages/django/db/models/query.py in get, line 307
Python Executable:  /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.5

我想检查我的数据库中是否存在此列,因此在终端中我进入 python shell 并输入 from logins.models import * 并收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "logins/models.py", line 1, in <module>
    from django.db import models
  File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 83, in <module>
    signals.request_started.connect(reset_queries)
  File "/Library/Python/2.7/site-packages/django/dispatch/dispatcher.py", line 88, in connect
    if settings.DEBUG:
  File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

问题是,除了数据库之外,我没有触及项目中的任何文件。我仍然有该应用程序的 SQLite 版本,我尝试从 python shell 运行 from logins.models import * ,现在得到了与 MySQL 相同的错误。之前,正如我提到的,一切都运行得非常好。

我真的很感激一些帮助!

提前致谢!

编辑: views.py 登录代码:

@login_required()
def index(request):
    u = request.user
    custom_user = CustomUser.objects.get(user=u)
    info_list =Info.objects.filter(game__in=custom_user.game.all(), department__in = custom_user.department.all()).distinct()
    #visible = Info.objects.filter(department__in=customuser.departments.all(), game__in=customuser.games.all())
    context = RequestContext(request, {
        'info_list': info_list,
    })
    template = loader.get_template('logins/index.html')

    args = {}
    args.update(csrf(request))

    args['index'] = Info.objects.all()
    return HttpResponse(template.render(context), args) 

最佳答案

  1. 关于 ImproperlyConfigured 异常,您是在原始 python shell 还是 django shell 中执行 from logins.models import * 命令吗?

    确保您使用 ./manage.py shell 启动 shell,然后执行上述命令

  2. 关于DoesNotExist异常,看来是登录代码的问题。当表中不存在请求的数据并且代码中未处理这种情况时,通常会发生此异常。它不必对表列执行任何操作。

    请将代码粘贴到问题中的 /logins/ 后面以供进一步评论。

关于python - Django:匹配查询不存在且 django.core.exceptions.ImproperlyConfigured,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20932796/

相关文章:

python - 在 Django 1.11 和 django _rest 框架中的嵌套序列化器上实现更新方法

python - Django-social auth KeyError

django - 在 Django 中为两种类型的用户子类化 AbstractUser

python - Tensorflow - 如何将模型翻译成其他语言

php - mod_cgi、mod_fastcgi、mod_scgi、mod_wsgi、mod_python、FLUP。我不知道还有多少。什么是 mod_php 等价物?

c# - 将值从 MySQL DB 加载到 datagridview 中的特定列

php - 一般问题 - 在网站中使用数据库

python - 如果列表项以 '.' 开头,为什么 python3 不从列表创建文件夹?

python - 如何执行Python文件

mysql - 无法远程连接MySQL