2014-01-05 146 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