Re: Major Trouble Understanding Sessions Documentation

46 views
Skip to first unread message

Larry Martell

unread,
Aug 2, 2012, 9:22:40 AM8/2/12
to django...@googlegroups.com
On Thu, Aug 2, 2012 at 7:06 AM, Gregory Thompson Jr.
<spockth...@gmail.com> wrote:
> I'd like to pass form data from one view to another.
>
> Here's my attempt:
>
> #Models.py
> from django import forms
>
> class Textizer(forms.Form):
> to_textize = forms.CharField(max_length=100)
>
> def __unicode__(self):
> return self.to_textize
>
> #views.py
> from textize.models import Textizer
> from django.http import HttpResponseRedirect
> from django.shortcuts import render_to_response
> from django.core.context_processors import csrf
>
> def index(request):
> if request.method == 'POST':
> form = Textizer(request.POST)
>
> if form.is_valid():
> request.session['text'] = form.cleaned_data['to_textize']
> return HttpResponseRedirect('/results')
>
> else:
> form = Textizer()
>
> c = {'form': form}
> c.update(csrf(request))
> return render_to_response('C:/Documents and
> Settings/quansai/projects/textsite/templates/index.html', c)
>
> def results(request):
> text = request.session.get('text', None)
> c = {'text' : text}
> return render_to_response('C:/Documents and
> Settings/quansai/projects/textsite/templates/results.html', c)
>
>
> I really don't understand the following, and I've read the documentation
> over and over. I've been on this for two days:
>
> How to initiate a session
> How sessions are checked
> How to retrieve form data from one page to handle the data on another.

In your template you can access the session variables as request.session.text.

Gregory Thompson Jr.

unread,
Aug 2, 2012, 9:31:31 AM8/2/12
to django...@googlegroups.com
Okay. What?

Can you explain any of the key points I was trying to understand?  I don't know what I'm doing.  

Daniel Roseman

unread,
Aug 2, 2012, 9:54:21 AM8/2/12
to django...@googlegroups.com
On Thursday, 2 August 2012 14:06:04 UTC+1, Gregory Thompson Jr. wrote:
<snip>
I really don't understand the following, and I've read the documentation over and over.  I've been on this for two days:
  • How to initiate a session
  • How sessions are checked
  • How to retrieve form data from one page to handle the data on another.
Again, I've read through the documentation. I've asked on IRC but everyone pretty much just says "RTFM" even when I tell them I have. This is absolutely frustrating as an extreme beginner.

I'd also like to add that I DO understand the core Python language. Please, unless it's absolutely necessary, don't try explaining to me what a dictionary, tuple, list, etc... is. I've already had my intelligence insulted by the users on IRC in this regard. I really just don't understand the sessions documentation.


So, you didn't understand the documentation, but you also don't want to be told what dictionaries/lists/tuples are.  How about you tell us which bit of the examples under "Using sessions in views" (which I'm not linking to because you've said you've read it) you didn't understand?
--
DR.

Gregory Thompson Jr.

unread,
Aug 2, 2012, 10:31:54 AM8/2/12
to django...@googlegroups.com
  1. def login(request):
  2.     m = Member.objects.get(username=request.POST['username'])
  3.     if m.password == request.POST['password']:
  4.         request.session['member_id'] = m.id
  5.         return HttpResponse("You're logged in.")
  6.     else:
  7.         return HttpResponse("Your username and password didn't match.")
How about that for starters?

The documentation goes from that to explaining how to set cookies.  How do they expect me to understand what's going on?
There's absolutely NO explanation of what's going on in that code.  

        request.session['member_id'] = m.id 

How the hell does that work?  

They don't tell you where the session is started, how, what the scope of the session declaration is, etc...  


See that video?  The video shows you how to use sessions very quickly.  How did the person in the video get ALL of that code from just the documentation? 

If all readers in this thread and kindly tackle each point I made, I promise you your own KFC when I start my empire.  Until then, I really just need someone to give me the step-by-step on this one.  I'm completely new to the framework and the documentation thus far has not been helpful.  I've mainly gotten by with the help of StackOverflow, IRC, and random code snippets online -- and occasionally, some outdated books.  

If you don't want to explain or aren't going to ask me progressive questions with regards to my confusion, please just skip over my plight.

Thank you all.

Marcin Tustin

unread,
Aug 2, 2012, 10:38:40 AM8/2/12
to django...@googlegroups.com
What's confusing about this? You don't explicitly create sessions, and they last as long as they are configured to last, which by default is until the session cookie is cleared.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/0czW05_b8q8J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Marcin Tustin
Tel: 07773 787 105

Gregory Thompson Jr.

unread,
Aug 2, 2012, 10:42:22 AM8/2/12
to django...@googlegroups.com
Then how do I use the session to pass data between views?  What's wrong with my code?

If it counts for anything, 'text' returns 'None' in my debug statements (in the results() view).  
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Kurtis Mullins

unread,
Aug 2, 2012, 10:42:59 AM8/2/12
to django...@googlegroups.com
Hey Gregory,

I really don't understand the following, and I've read the documentation over and over.  I've been on this for two days:

No problem! I'll try to clear things up for you.
 
  • How to initiate a session
Sessions are initiated automatically in Django using the "Session Middleware". Basically, on each request, a session is either started or continued (in simple terms, there's probably more to it than that but I haven't inspected Django's session code too deeply)

The only thing you need to do to make sure the session middleware is properly configure it. It needs some sort of a storage (such as database, file, cookies which are limited, etc...). You'll also need to make sure the session middleware is included in your middleware configuration. This configuration would exist under your settings.py and I believe is enabled by default when creating new projects; at least that's been my experience with Django 1.3+.

Keep in mind that these do use cookies, no matter what, so you will need them enabled in whatever HTTP Client (browser, or what-not) you use.

Two links for enabling and configuring sessions (both on the same page, these are anchor links).


  • How sessions are checked
I'm not quite sure what you mean, here. Sessions are checked to be valid by Django by signing them, but this is done by the middleware. If you want to check whether a session exists, you might be asking more along the lines of checking to see whether a user is authenticated. Or, you may be asking how to check whether a certain variable is available from the session. Since I'm not sure which part your looking for information on, I'll give you information on both:

To check if a particular object exists within the current session, follow the example under __contains__(key) which actually does not call "contains" but instead uses "if x in request.session": https://docs.djangoproject.com/en/dev/topics/http/sessions/?from=olddocs/#using-sessions-in-views

To check whether a current user is authenticated (logged in), you could use this example: https://docs.djangoproject.com/en/dev/topics/auth/#authentication-in-web-requests
 
  • How to retrieve form data from one page to handle the data on another.

Retrieving Form Data is actually one of Django's extremely strong points (in my opinion) although it does require a bit of a learning curve. Once you get it down, it takes off a huge burden; especially when coming from other frameworks that don't offer as much validation built in. Basically, you subclass a Form, create an instance of your form in the view while passing it the POST data, and it will validate that data. Then, you can simply do some flow-control based upon "if form.is_valid()".

Handling the data on another view is sort of a vague question. I'm not quite sure what you mean by this one but please feel free to elaborate and I'll see if I can give you some more info here.
 
Again, I've read through the documentation. I've asked on IRC but everyone pretty much just says "RTFM" even when I tell them I have. This is absolutely frustrating as an extreme beginner.

Weird. Most people just suggest reading the Tutorial. Have you given that a shot? It really does help with diving into the functionality and doesn't take too long considering the large amount of knowledge you gain about Django from it.
 

I'd also like to add that I DO understand the core Python language. Please, unless it's absolutely necessary, don't try explaining to me what a dictionary, tuple, list, etc... is. I've already had my intelligence insulted by the users on IRC in this regard. I really just don't understand the sessions documentation.

--

Sorry to hear about your frustration. I recommend trying out the tutorial if you haven't given it a shot, yet, and maybe break your questions up into smaller peices when you ask them on here. Including your shot at the code was a good show of attempt. I'm not sure what exact problems you're having with your code but it may be helpful to yourself to break it down to the smallest body of functionality that will emulate what you're attempting to see where it's going wrong. Taking out the Form and testing sessions with two views might be a good way to start.
 
Good luck!

Daniel Roseman

unread,
Aug 2, 2012, 10:51:18 AM8/2/12
to django...@googlegroups.com
On Thursday, 2 August 2012 15:31:54 UTC+1, Gregory Thompson Jr. wrote:
  1. def login(request):
  2.     m = Member.objects.get(username=request.POST['username'])
  3.     if m.password == request.POST['password']:
  4.         request.session['member_id'] = m.id
  5.         return HttpResponse("You're logged in.")
  6.     else:
  7.         return HttpResponse("Your username and password didn't match.")
How about that for starters?

The documentation goes from that to explaining how to set cookies.  How do they expect me to understand what's going on?
There's absolutely NO explanation of what's going on in that code.  

        request.session['member_id'] = m.id 

How the hell does that work?  

They don't tell you where the session is started, how, what the scope of the session declaration is, etc...  


See that video?  The video shows you how to use sessions very quickly.  How did the person in the video get ALL of that code from just the documentation? 

If all readers in this thread and kindly tackle each point I made, I promise you your own KFC when I start my empire.  Until then, I really just need someone to give me the step-by-step on this one.  I'm completely new to the framework and the documentation thus far has not been helpful.  I've mainly gotten by with the help of StackOverflow, IRC, and random code snippets online -- and occasionally, some outdated books.  

If you don't want to explain or aren't going to ask me progressive questions with regards to my confusion, please just skip over my plight.

Thank you all.


I don't understand why you think you need to know these things in order to use sessions. If you want to understand how assigning to a session is implemented, you're free to dive into the code itself. But that's literally *all you need to do* in order to use the session in the view (assuming you've followed the rest of the instructions on that page, eg added the middleware).
--
DR.

Marcin Tustin

unread,
Aug 2, 2012, 10:54:33 AM8/2/12
to django...@googlegroups.com
This is how you store data:
 request.session['member_id'] = m.id  
This is how you read it:
 request.session['member_id']  

Your view code looks fine, in respect of how you use sessions. I can't see that you have explained what problem you are having: what is going wrong?

Finally, you do have to do some minimal configuration to enable sessions. Are you sure you have done that?

To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/GIPMRuHoti8J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Gregory Thompson Jr.

unread,
Aug 2, 2012, 10:57:00 AM8/2/12
to django...@googlegroups.com
Thanks a load!  You've cleared up a lot!

Yes, I have read through the tutorial.  I even wrote a shortened version of it to act as notes as I learn:  http://polydoo.com/code/?p=48  (my blog)

Thanks again!

Kurtis Mullins

unread,
Aug 2, 2012, 11:17:46 AM8/2/12
to django...@googlegroups.com
On Thu, Aug 2, 2012 at 10:57 AM, Gregory Thompson Jr. <spockth...@gmail.com> wrote:
Thanks a load!  You've cleared up a lot!

Yes, I have read through the tutorial.  I even wrote a shortened version of it to act as notes as I learn:  http://polydoo.com/code/?p=48  (my blog)

Thanks again!

No problem! Have fun and let us know if you have any more questions! 
Reply all
Reply to author
Forward
0 new messages