我不能在我的项目中使用django的会话

时间:2017-08-23 06:42:08

标签: django

当我登录时,我想在会话中存储用户名。 但是当我在views.py中设置会话时,却得到以下错误:

Internal Server Error: /cookie/login1/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/deprecation.py", line 142, in __call__
    response = self.process_response(request, response)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/contrib/sessions/middleware.py", line 58, in process_response
    request.session.save()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/contrib/sessions/backends/db.py", line 81, in save
    return self.create()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/contrib/sessions/backends/db.py", line 50, in create
    self._session_key = self._get_new_session_key()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/contrib/sessions/backends/base.py", line 164, in _get_new_session_key
    if not self.exists(session_key):
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/contrib/sessions/backends/db.py", line 46, in exists
    return self.model.objects.filter(session_key=session_key).exists()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/db/models/query.py", line 670, in exists
    return self.query.has_results(using=self.db)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/db/models/sql/query.py", line 517, in has_results
    return compiler.has_results()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/db/models/sql/compiler.py", line 845, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/db/models/sql/compiler.py", line 886, in execute_sql
    raise original_exception
OperationalError: no such table: django_session

我测试了我的代码,没有找到原因。

urls.py

urlpatterns = [

    ...
    url(r'^login1/$', views.login1),
    url(r'^index1/$', views.index1),
]

views.py中,我有两种方法,如果login1成功,我将重定向到index1.html:

def login1(request):
    if request.method == 'GET':
        return render(request, 'cookie/login1.html')
    elif request.method == 'POST':

        username = request.POST.get('username')
        pwd = request.POST.get('pwd')

        print ('username:'+username, 'pwd:'+pwd)

        if username == 'root' and pwd == '123':

            request.session['username'] = username  
            return redirect('/cookie/index1/')
        else:
            return render(request, 'cookie/login1.html')

def index1(request):

    return HttpResponse('index1')

1 个答案:

答案 0 :(得分:1)

来自追溯:

  

OperationalError:没有这样的表:django_session

数据库现在没有django_session表,所以你不能在该表中设置session,django store session,你应该在终端中执行bellow命令,先对它进行概括:

python manage.py makemigrations 
python manage.py migrate