Getting error while reading the file content using Python and Django -


i need help. trying read file content using url , return content response in html format getting following error.

error:

 traceback (most recent call last):   file "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner     response = get_response(request)   file "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response     response = self.process_exception_by_middleware(e, request)   file "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response     response = wrapped_callback(request, *callback_args, **callback_kwargs)   file "/opt/lampp/htdocs/rework/nuclear/rfi/secure/plant/views.py", line 217, in view_reactor     pers = user.objects.get(pk=request.session['id'])   file "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 57, in __getitem__     return self._session[key] keyerror: u'id' 

i explaining code below.

site = 'http://127.0.0.1:8000/'     my_list = []     my_list.extend(['home', 'view_reactor'])     if request.get.get('file') not none , request.get.get('file') != '':         file = request.get.get('file')         if file in my_list:             full_path = site+file             response = urllib.urlopen(full_path)             lines = response.readlines()             return httpresponse(content=lines, content_type="text/html")         else:             return render(request, 'plant/home.html', {'count': 1})     else:         return render(request, 'plant/home.html', {'count': 1})   def view_reactor(request):     """ function serch screen. """      pers = user.objects.get(pk=request.session['id'])     root = []     user_name = pers.uname     count = 1     root.append(         {'username': user_name,          'count': count          })     return render(request, 'plant/view_reactor.html',                   {'user': root, 'count': 1}) 

here passing the value in query string http://127.0.0.1:8000/createfile/?file=view_reactor , need response of http://127.0.0.1:8000/view_reactor page in html format. in code getting error. please help.

the error saying there no id key in session dictionary. in following line:

pers = user.objects.get(pk=request.session['id']) 

and me looks redundant user this. should able user doing this:

pers = request.user 

provided have auth middleware installed.

second option (not tested though):

pers = user.objects.get(pk=request.session['_auth_user_id']) 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -