0

I am trying to read the file content using URL and return that the content as response in html format but getting the following 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 am providing my code below:

site = 'http://127.0.0.1:8000/'
    my_list = []
    my_list.extend(['home', 'view_reactor'])
    if request.GET.get('file') is not None and 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):
    """ This function for to get 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 I am passing the the value in query string like this http://127.0.0.1:8000/createfile/?file=view_reactor and finally I need the response of http://127.0.0.1:8000/view_reactor page in html format. But in my code I am getting this error.

3
  • 1
    Share the complete stack trace Commented Sep 11, 2017 at 6:15
  • @arjun27 : I pasted my complete stack trace with required code. Commented Sep 11, 2017 at 6:18
  • your session dict does contains id "pk=request.session['id']" Commented Sep 11, 2017 at 6:20

1 Answer 1

1

The error is saying that there is no id key in your session dictionary. In the following line:

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

And to me it looks redundant to get a user like this. You should be able to get a user simply by doing this:

pers = request.user

Provided that you have auth middleware installed.

Second option (not tested though):

pers = User.objects.get(pk=request.session['_auth_user_id'])
Sign up to request clarification or add additional context in comments.

7 Comments

I did as per you but its throwing this AttributeError: 'AnonymousUser' object has no attribute 'uname' error.
@satya it means that the user was not authenticated before using this view. You should authenticate him when he logs in.
@satya please see the second option in my updated answer
http://127.0.0.1:8000/view_reactor has the id in session because I can open this directly but here i am passing the same in query string and also content_type="text/plain" is working fine.
No, its throwing this error Exception Value: u'_auth_user_id' . Actually after login i am setting the ID in session.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.