How can I set the email address in Django's comment form with a value stored in a session variable?
Asked
Active
Viewed 131 times
1
-
One might find some clues to accessing session variables in a form over here: http://stackoverflow.com/questions/1057252/django-how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-cle – Peter G Mar 28 '12 at 01:43
1 Answers
1
I think you need to create an app for comments customisation: https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/
As the article explains you will need to read your session value inside your form's get_comment_create_data
method.

akonsu
- 28,824
- 33
- 119
- 194
-
I'm not sure how to do this using Django's comment form framework. The forms themselves typically don't appear in the views or forms code, only in the template. – SixDegrees Sep 21 '11 at 14:33
-
Yes, I've read that article several times and haven't found it helpful. As you originally pointed out, it seems that the session variable would have to be passed to the form through the view, but there is no indication of how to do that with a framework-supplied comment form. – SixDegrees Sep 21 '11 at 15:04
-
I have edited my answer again :) I do not see why you need to deal with views here. – akonsu Sep 21 '11 at 15:10
-
How am I supposed to get the session value into the form? Form creation/initialization is completely opaque, hidden away in template tags; I never explicitly instantiate a comment form anywhere in my code. – SixDegrees Sep 21 '11 at 15:59
-
If I understand your question correctly, you need to declare your form class and implement methods on it that comments framework will call. this way when it creates your form you will be able to read your session values. If this does not help, please clarify your question. – akonsu Sep 21 '11 at 16:04
-
Have you ever actually used the comments framework that comes with Django? Because you don't seem to understand how it is used. You never create the comment form explicitly; it is implicitly created by template tags. There is ALREADY an email field in the form; all I want to do is use a session variable to initialize it, but there doesn't seem to be anyway to do that because there is no apparent way to pass any such value to the form when it is created within the tag. – SixDegrees Sep 21 '11 at 16:31
-
according to the document that I have linked in my answer, you need to create \_\_init__.py file in your application's directory which should contain a couple of methods, namely, get_form and get_model, which is how you create your form. please RTFM. – akonsu Sep 21 '11 at 16:35
-
Never mind. You obviously don't understand the problem or the framework. – SixDegrees Sep 21 '11 at 16:36