【问题标题】:Django: Matching query does not exist and django.core.exceptions.ImproperlyConfiguredDjango:匹配查询不存在和 django.core.exceptions.ImproperlyConfigured
【发布时间】:2014-01-22 20:14:27
【问题描述】:

我在 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) 

【问题讨论】:

  • 您在进行更改后是否同步了数据库?
  • @IanClark 是的,我还是犯了同样的查询错误:(

标签: python mysql django sqlite


【解决方案1】:
  1. 关于ImproperlyConfigured 异常,您是从原始python shell 还是django shell 执行from logins.models import * 命令?

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

  2. 关于DoesNotExist异常,看来是登录代码有问题。此异常通常在表中不存在请求的数据时发生,并且该情况未在代码中处理。它不必对表格列做任何事情。

    请在问题中粘贴 /logins/ 后面的代码以获取更多 cmets。

【讨论】:

  • 哦,这是我的一个愚蠢的错误!你是对的,我使用 python 而不是 django shell。我的错。谢谢你抓住那个。关于DoesNotExist,我编辑了我的问题并发布了登录代码
  • 错误似乎来自custom_user = CustomUser.objects.get(user=u) 行。我认为没有必要这样做。 custom_user = request.user 应该做这项工作。另请参阅Custom User Docs
猜你喜欢
  • 2015-01-28
  • 2022-01-05
  • 2020-10-15
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多