Getting form data in Django middlewareHow to get the current time in PythonWhat is a “slug” in Django?Getting the class name of an instance?Does Django scale?Getting the last element of a list in PythonHow to debug in Django, the good way?How do I get the number of elements in a list in Python?How to check Django versiondifferentiate null=True, blank=True in djangoHow to view a particluar list of contacts or only my contacts in Django application?

What will be the temperature on Earth when Sun finishes its main sequence?

Word describing multiple paths to the same abstract outcome

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Are taller landing gear bad for aircraft, particulary large airliners?

Pronouncing Homer as in modern Greek

Does "Dominei" mean something?

What was required to accept "troll"?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Can somebody explain Brexit in a few child-proof sentences?

Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)

What do you call the infoboxes with text and sometimes images on the side of a page we find in textbooks?

Could solar power be utilized and substitute coal in the 19th century?

Indicating multiple different modes of speech (fantasy language or telepathy)

Invariance of results when scaling explanatory variables in logistic regression, is there a proof?

Why are on-board computers allowed to change controls without notifying the pilots?

Was the picture area of a CRT a parallelogram (instead of a true rectangle)?

Do all polymers contain either carbon or silicon?

Stereotypical names

Lifted its hind leg on or lifted its hind leg towards?

Can the harmonic series explain the origin of the major scale?

Should a half Jewish man be discouraged from marrying a Jewess?

Female=gender counterpart?

Calculating the number of days between 2 dates in Excel

What if somebody invests in my application?



Getting form data in Django middleware


How to get the current time in PythonWhat is a “slug” in Django?Getting the class name of an instance?Does Django scale?Getting the last element of a list in PythonHow to debug in Django, the good way?How do I get the number of elements in a list in Python?How to check Django versiondifferentiate null=True, blank=True in djangoHow to view a particluar list of contacts or only my contacts in Django application?













0















What I am basically trying to do is capture data into my middleware from front end, so that I can track the users activity. User activity refers to buttons clicked and data entered into a form.
So I am done with the button part but stuck on getting data filled in the form by the user. I need help with that.



Below is my html and my middleware build in python:



middleware.py:



class TodoappMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if request.method == "GET":
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
name = request.GET.get('name')
if not name == 'None':
f.write(str(name))
f.write("n")
f.close()

elif request.method == 'POST':
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
if request.POST.get("Login"):
nm = request.POST.get("Login")
if not nm == 'None':
f.write("Loginn")
elif request.POST.get("Authorize"):
f.write("Authorizen")
elif request.POST.get("Assign"):
f.write("Assignn")
elif request.POST.get("delete_task"):
f.write("Delete taskn")
elif request.POST.get("mark_complete"):
f.write("Mark as completen")
elif request.POST.get('authorise_users'):
form = AuthUserCheckbox(request.POST)
if form.is_valid():
ch = form.cleaned_data.get('choice')
print "User then chose "
f.write(ch)

f.close()


html:



% extends 'todoapp/base.html' %

% block title %Authorize users% endblock %

% block content %
<h2>Select users to authorize</h2>
<form method="post" action="% url 'auth_users' %" id="authorise_users">
% csrf_token %
form.choice
<br/><input type="submit" value="Authorize" name="Authorize">
&nbsp;&nbsp;<button onclick="location.href='%url 'dashboard' %?name=Go back'" type="button">Go back</button>
</form>
% endblock %


All the request.POST.get are buttons except for authorise_users, it is a checkbox. I am not able to get the user entered value for that. All buttons clicks I have got but I am not able to get the selected value for authorise_users










share|improve this question
























  • You have to tell us whats wrong. What is the error you are getting? Were are you stuck? Does it write to the file at all?

    – Ralf
    Mar 7 at 10:40











  • Its not writing to the file. It'll write that user went to the intended page but not the choices he made. Error i am getting is AttributeError at /auth_users/ 'WSGIRequest' object has no attribute 'choice

    – Vai
    Mar 7 at 10:44












  • have you tried print(request.POST) and print(form.cleaned_data) to see what values it actually contains? Maybe it contains something different than you expect?

    – Ralf
    Mar 7 at 11:34















0















What I am basically trying to do is capture data into my middleware from front end, so that I can track the users activity. User activity refers to buttons clicked and data entered into a form.
So I am done with the button part but stuck on getting data filled in the form by the user. I need help with that.



Below is my html and my middleware build in python:



middleware.py:



class TodoappMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if request.method == "GET":
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
name = request.GET.get('name')
if not name == 'None':
f.write(str(name))
f.write("n")
f.close()

elif request.method == 'POST':
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
if request.POST.get("Login"):
nm = request.POST.get("Login")
if not nm == 'None':
f.write("Loginn")
elif request.POST.get("Authorize"):
f.write("Authorizen")
elif request.POST.get("Assign"):
f.write("Assignn")
elif request.POST.get("delete_task"):
f.write("Delete taskn")
elif request.POST.get("mark_complete"):
f.write("Mark as completen")
elif request.POST.get('authorise_users'):
form = AuthUserCheckbox(request.POST)
if form.is_valid():
ch = form.cleaned_data.get('choice')
print "User then chose "
f.write(ch)

f.close()


html:



% extends 'todoapp/base.html' %

% block title %Authorize users% endblock %

% block content %
<h2>Select users to authorize</h2>
<form method="post" action="% url 'auth_users' %" id="authorise_users">
% csrf_token %
form.choice
<br/><input type="submit" value="Authorize" name="Authorize">
&nbsp;&nbsp;<button onclick="location.href='%url 'dashboard' %?name=Go back'" type="button">Go back</button>
</form>
% endblock %


All the request.POST.get are buttons except for authorise_users, it is a checkbox. I am not able to get the user entered value for that. All buttons clicks I have got but I am not able to get the selected value for authorise_users










share|improve this question
























  • You have to tell us whats wrong. What is the error you are getting? Were are you stuck? Does it write to the file at all?

    – Ralf
    Mar 7 at 10:40











  • Its not writing to the file. It'll write that user went to the intended page but not the choices he made. Error i am getting is AttributeError at /auth_users/ 'WSGIRequest' object has no attribute 'choice

    – Vai
    Mar 7 at 10:44












  • have you tried print(request.POST) and print(form.cleaned_data) to see what values it actually contains? Maybe it contains something different than you expect?

    – Ralf
    Mar 7 at 11:34













0












0








0


1






What I am basically trying to do is capture data into my middleware from front end, so that I can track the users activity. User activity refers to buttons clicked and data entered into a form.
So I am done with the button part but stuck on getting data filled in the form by the user. I need help with that.



Below is my html and my middleware build in python:



middleware.py:



class TodoappMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if request.method == "GET":
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
name = request.GET.get('name')
if not name == 'None':
f.write(str(name))
f.write("n")
f.close()

elif request.method == 'POST':
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
if request.POST.get("Login"):
nm = request.POST.get("Login")
if not nm == 'None':
f.write("Loginn")
elif request.POST.get("Authorize"):
f.write("Authorizen")
elif request.POST.get("Assign"):
f.write("Assignn")
elif request.POST.get("delete_task"):
f.write("Delete taskn")
elif request.POST.get("mark_complete"):
f.write("Mark as completen")
elif request.POST.get('authorise_users'):
form = AuthUserCheckbox(request.POST)
if form.is_valid():
ch = form.cleaned_data.get('choice')
print "User then chose "
f.write(ch)

f.close()


html:



% extends 'todoapp/base.html' %

% block title %Authorize users% endblock %

% block content %
<h2>Select users to authorize</h2>
<form method="post" action="% url 'auth_users' %" id="authorise_users">
% csrf_token %
form.choice
<br/><input type="submit" value="Authorize" name="Authorize">
&nbsp;&nbsp;<button onclick="location.href='%url 'dashboard' %?name=Go back'" type="button">Go back</button>
</form>
% endblock %


All the request.POST.get are buttons except for authorise_users, it is a checkbox. I am not able to get the user entered value for that. All buttons clicks I have got but I am not able to get the selected value for authorise_users










share|improve this question
















What I am basically trying to do is capture data into my middleware from front end, so that I can track the users activity. User activity refers to buttons clicked and data entered into a form.
So I am done with the button part but stuck on getting data filled in the form by the user. I need help with that.



Below is my html and my middleware build in python:



middleware.py:



class TodoappMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if request.method == "GET":
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
name = request.GET.get('name')
if not name == 'None':
f.write(str(name))
f.write("n")
f.close()

elif request.method == 'POST':
f = open("/home/gblp250/Desktop/log", 'a')
f.write("User clicked ")
if request.POST.get("Login"):
nm = request.POST.get("Login")
if not nm == 'None':
f.write("Loginn")
elif request.POST.get("Authorize"):
f.write("Authorizen")
elif request.POST.get("Assign"):
f.write("Assignn")
elif request.POST.get("delete_task"):
f.write("Delete taskn")
elif request.POST.get("mark_complete"):
f.write("Mark as completen")
elif request.POST.get('authorise_users'):
form = AuthUserCheckbox(request.POST)
if form.is_valid():
ch = form.cleaned_data.get('choice')
print "User then chose "
f.write(ch)

f.close()


html:



% extends 'todoapp/base.html' %

% block title %Authorize users% endblock %

% block content %
<h2>Select users to authorize</h2>
<form method="post" action="% url 'auth_users' %" id="authorise_users">
% csrf_token %
form.choice
<br/><input type="submit" value="Authorize" name="Authorize">
&nbsp;&nbsp;<button onclick="location.href='%url 'dashboard' %?name=Go back'" type="button">Go back</button>
</form>
% endblock %


All the request.POST.get are buttons except for authorise_users, it is a checkbox. I am not able to get the user entered value for that. All buttons clicks I have got but I am not able to get the selected value for authorise_users







python django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 11:20







Vai

















asked Mar 7 at 10:29









VaiVai

426




426












  • You have to tell us whats wrong. What is the error you are getting? Were are you stuck? Does it write to the file at all?

    – Ralf
    Mar 7 at 10:40











  • Its not writing to the file. It'll write that user went to the intended page but not the choices he made. Error i am getting is AttributeError at /auth_users/ 'WSGIRequest' object has no attribute 'choice

    – Vai
    Mar 7 at 10:44












  • have you tried print(request.POST) and print(form.cleaned_data) to see what values it actually contains? Maybe it contains something different than you expect?

    – Ralf
    Mar 7 at 11:34

















  • You have to tell us whats wrong. What is the error you are getting? Were are you stuck? Does it write to the file at all?

    – Ralf
    Mar 7 at 10:40











  • Its not writing to the file. It'll write that user went to the intended page but not the choices he made. Error i am getting is AttributeError at /auth_users/ 'WSGIRequest' object has no attribute 'choice

    – Vai
    Mar 7 at 10:44












  • have you tried print(request.POST) and print(form.cleaned_data) to see what values it actually contains? Maybe it contains something different than you expect?

    – Ralf
    Mar 7 at 11:34
















You have to tell us whats wrong. What is the error you are getting? Were are you stuck? Does it write to the file at all?

– Ralf
Mar 7 at 10:40





You have to tell us whats wrong. What is the error you are getting? Were are you stuck? Does it write to the file at all?

– Ralf
Mar 7 at 10:40













Its not writing to the file. It'll write that user went to the intended page but not the choices he made. Error i am getting is AttributeError at /auth_users/ 'WSGIRequest' object has no attribute 'choice

– Vai
Mar 7 at 10:44






Its not writing to the file. It'll write that user went to the intended page but not the choices he made. Error i am getting is AttributeError at /auth_users/ 'WSGIRequest' object has no attribute 'choice

– Vai
Mar 7 at 10:44














have you tried print(request.POST) and print(form.cleaned_data) to see what values it actually contains? Maybe it contains something different than you expect?

– Ralf
Mar 7 at 11:34





have you tried print(request.POST) and print(form.cleaned_data) to see what values it actually contains? Maybe it contains something different than you expect?

– Ralf
Mar 7 at 11:34












1 Answer
1






active

oldest

votes


















0














Based on the error you are getting (mentioned in a comment to the question), you probably need to change the line



if request.choice.is_valid():


to



if form.choice.is_valid():


or



if form.is_valid():


Does that help to solve the error?






share|improve this answer























  • Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

    – Ralf
    Mar 7 at 10:48











  • I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

    – Vai
    Mar 7 at 10:52











  • Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

    – Ralf
    Mar 7 at 10:59











  • that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

    – Vai
    Mar 7 at 11:05












  • I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

    – Ralf
    Mar 7 at 11:08










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55041542%2fgetting-form-data-in-django-middleware%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Based on the error you are getting (mentioned in a comment to the question), you probably need to change the line



if request.choice.is_valid():


to



if form.choice.is_valid():


or



if form.is_valid():


Does that help to solve the error?






share|improve this answer























  • Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

    – Ralf
    Mar 7 at 10:48











  • I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

    – Vai
    Mar 7 at 10:52











  • Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

    – Ralf
    Mar 7 at 10:59











  • that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

    – Vai
    Mar 7 at 11:05












  • I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

    – Ralf
    Mar 7 at 11:08















0














Based on the error you are getting (mentioned in a comment to the question), you probably need to change the line



if request.choice.is_valid():


to



if form.choice.is_valid():


or



if form.is_valid():


Does that help to solve the error?






share|improve this answer























  • Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

    – Ralf
    Mar 7 at 10:48











  • I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

    – Vai
    Mar 7 at 10:52











  • Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

    – Ralf
    Mar 7 at 10:59











  • that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

    – Vai
    Mar 7 at 11:05












  • I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

    – Ralf
    Mar 7 at 11:08













0












0








0







Based on the error you are getting (mentioned in a comment to the question), you probably need to change the line



if request.choice.is_valid():


to



if form.choice.is_valid():


or



if form.is_valid():


Does that help to solve the error?






share|improve this answer













Based on the error you are getting (mentioned in a comment to the question), you probably need to change the line



if request.choice.is_valid():


to



if form.choice.is_valid():


or



if form.is_valid():


Does that help to solve the error?







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 10:46









RalfRalf

6,86841437




6,86841437












  • Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

    – Ralf
    Mar 7 at 10:48











  • I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

    – Vai
    Mar 7 at 10:52











  • Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

    – Ralf
    Mar 7 at 10:59











  • that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

    – Vai
    Mar 7 at 11:05












  • I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

    – Ralf
    Mar 7 at 11:08

















  • Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

    – Ralf
    Mar 7 at 10:48











  • I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

    – Vai
    Mar 7 at 10:52











  • Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

    – Ralf
    Mar 7 at 10:59











  • that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

    – Vai
    Mar 7 at 11:05












  • I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

    – Ralf
    Mar 7 at 11:08
















Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

– Ralf
Mar 7 at 10:48





Also, you are not closing your file at the end of the middleware. That is not a good idea. Maybe use with open(... to avoid that kind of easily overlooked mistake.

– Ralf
Mar 7 at 10:48













I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

– Vai
Mar 7 at 10:52





I have placed my form = AuthUserCheckbox(data=request.POST, user=request.user) in Authorize. Is that correct or do I need to create a new request.POST.get elif branch for it ?

– Vai
Mar 7 at 10:52













Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

– Ralf
Mar 7 at 10:59





Remember that if request.POST.get("Login") and elif request.POST.get("Authorize") will be True if the value retrieved from request.POST evaluates to True. Maybe you need to do a bit stricter checking, for example if request.POST.get("Login") == 'something'.

– Ralf
Mar 7 at 10:59













that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

– Vai
Mar 7 at 11:05






that's right. Based on this only I've detected all the button clicks but I am not able to get the data entered by the user in a form. Any help with that ?

– Vai
Mar 7 at 11:05














I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

– Ralf
Mar 7 at 11:08





I don't understand what you mean. Can you add more info in your question, for example some outputs of print statements before the parts that do not seem to be working? (comments are not the right place to add a lot of info, because they have only limited formatting)

– Ralf
Mar 7 at 11:08



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55041542%2fgetting-form-data-in-django-middleware%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко