2014-01-05 103 views
0

我有一个完全可用的SQLite应用程序,现在用于生产,我不得不将它配置为使用MySQL。我安装了MySQL和Python客户端,改变了我的settings.py到:Django:匹配查询不存在和django.core.exceptions.ImproperlyConfigured

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 

我想检查我的数据库中存在此列,所以在终端,我走进蟒蛇外壳和类型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) 
+0

您做了更改后是否syncDB? –

+0

@IanClark是的,我确实 仍然是相同的查询错误:( – lulu

回答

1
  1. 关于ImproperlyConfigured例外,你是在执行命令from logins.models import *形式原料蟒蛇壳,或者Django的壳呢?

    确保您使用开始的./manage.py shell外壳,然后执行上面的命令

  2. 关于DoesNotExist例外,它似乎是与登录的代码有问题。这种异常通常发生在表中不存在请求的数据时,并且在代码中不处理这种情况。它不必对表格列做任何事情。

    请将代码粘贴到/logins/后面的问题中以获取更多意见。

+0

喔,那是我的这样一个愚蠢的错误!你是对的,我用来代替Django的壳蟒蛇。我的坏。感谢没收。 以及有关DoesNotExist,我编辑了我的问题,并发布了登录代码 – lulu

+0

错误似乎来自'custom_user = CustomUser.objects.get(user = u)'行,我不认为有这种需要。 custom_user = request.user'应该完成这个工作。另外请参考[Custom User Docs](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model) ) –

+0

非常感谢您的帮助! – lulu