You are not logged in.

Change Language:

Board >> Django >> Middleware >> session and error 500

Page: 1

Hello,

I want to use session on my django website I've this view :
def check_pass(request):
    if request.session.get('logged_in', False):
        return render_to_response('message.html',{'is_connected':True,})
    
    if request.method != 'POST':
        return render_to_response('message.html',{'is_connected':False,'message':"what are you doing ?"})
    
    form = PasswordForm(request.POST)
    if form.is_valid()==False:
        return render_to_response('home.html',{'form':form,})
        
    # check the password
    # if wrong 
        return render_to_response('message.html',{'is_connected':False,'message':"wrong password"})
    # otherwise
    request.session['logged_in'] = True
    return render_to_response('message.html',{'is_connected':True,})


if it pass all the test, I've got an 500 error for the "request.session['logged_in'] = True" (I tried with "assert False", the error is there)
in /var/log/httpd/error.log I've this :
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1] Traceback (most recent call last):
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 245, in __call__
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     response = middleware_method(request, response)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/contrib/sessions/middleware.py", line 36, in process_response
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     request.session.save()
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/db.py", line 52, in save
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     session_key = self.session_key,
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py", line 152, in _get_session_key
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     self._session_key = self._get_new_session_key()
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py", line 144, in _get_new_session_key
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     if not self.exists(session_key):
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/db.py", line 25, in exists
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     Session.objects.get(session_key=session_key)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 120, in get
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     return self.get_query_set().get(*args, **kwargs)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 300, in get
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     num = len(clone)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     self._result_cache = list(self.iterator())
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     for row in self.query.results_iter():
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     for rows in self.execute_sql(MULTI):
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     cursor.execute(sql, params)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 19, in execute
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     return self.cursor.execute(sql, params)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]   File "/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py", line 193, in execute
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1]     return Database.Cursor.execute(self, query, params)
[Sat Jan 16 11:41:29 2010] error [client 127.0.0.1] OperationalError: no such table: django_session


I've also tried to do my own 500 view
# from urls.py
handler500 = 'custom_500'

# from views.py
def custom_500(request):
    return render_to_response('error500.html')

but I still arrive on the apache's error page

thanks
what exactly is your problem? :) you need to run 'syncdb' so the session table is created in your database...
no I still have the page error 500

edit I'm trying another way not more successful : with authentication

if in a terminal, I do
$ python manage.py shell
Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib import auth
>>> user = auth.authenticate(username='user', password='password')

no problem

if I do
# views.py
...
from django.contrib import auth
...

def check_pass(request):
    ...
    user = auth.authenticate(username='other', password=password)
    ...


I've got an error "no such table: auth_user"
Environment:

Request Method: POST
Request URL: http://localhost/check_pass/
Django Version: 1.1.1
Python Version: 2.6.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/mart/programmation/python/django/martfiles/views.py" in check_pass
  66.         user = auth.authenticate(username='other', password='password')
File "/usr/lib/python2.6/site-packages/django/contrib/auth/__init__.py" in authenticate
  37.             user = backend.authenticate(**credentials)
File "/usr/lib/python2.6/site-packages/django/contrib/auth/backends.py" in authenticate
  18.             user = User.objects.get(username=username)
File "/usr/lib/python2.6/site-packages/django/db/models/manager.py" in get
  120.         return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in get
  300.         num = len(clone)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in __len__
  81.                 self._result_cache = list(self.iterator())
File "/usr/lib/python2.6/site-packages/django/db/models/query.py" in iterator
  238.         for row in self.query.results_iter():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py" in results_iter
  287.         for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.6/site-packages/django/db/models/sql/query.py" in execute_sql
  2369.         cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py" in execute
  19.             return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py" in execute
  193.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /check_pass/
Exception Value: no such table: auth_user



edit : answer here

--- Last Edited by n2turtles at 2010-01-18 13:24:03 ---


Page: 1



Please login to post a reply.



Powered by Sphene Community Tools