007
https://dpaste.de/MBJ3
007
I am able to get form value... But not variable
007
I am facing problem in views.py
Mirco
007
Which one ?
https://dpaste.de/MBJ3
In this code I'm trying to pass choice variable to views.py using ajax but can't get it's value
In view.py
Mirco
007
Yes
007
I want to print choice variable value in views. Py
007
Mirco
none
Not python, browser console
007
Mirco
If u print into your view just request.POST, what do you get ?
007
Is it correct way :-
request.POST.get('choice')
007
007
For choice it print None
Eugene
"Daniel Roy Greenfeld, Audrey Roy Greenfeld - Two Scoops of Django 1.11 Best Practices for the Django Web Framework"
this book is actual now?
Perumal
In Django and flask integration is possible?
Mirco
007
request.POST.get(' user ') :- admin request.POST.get(' admin ') :- admin request.POST.get(' choice ') :- None but choice variable value is printed correctly in console.log()
Mirco
again please just do this
print(request.POST)
Mirco
And share what u get
Mirco
The key 'choice' does not exist
If u print all request.POST or better request.POST.keys() you will.know how to get your choice
007
007
Mirco
So it's normal you get None by calling your choice
007
Yes
Anders (izzno / gooood) ๐ณ๐ด
https://gist.github.com/izznogooood/207271d5b3445c508f553a0003feab13
Anders (izzno / gooood) ๐ณ๐ด
This might help you.
Anders (izzno / gooood) ๐ณ๐ด
Its a dictionary.
Mirco
007
Its a dictionary.
data: form.serialize() is it correct way to pass form value and variable
007
Where form is pointed by $(this)
Anders (izzno / gooood) ๐ณ๐ด
Anders (izzno / gooood) ๐ณ๐ด
Or what you are talking about.
Anders (izzno / gooood) ๐ณ๐ด
Anders (izzno / gooood) ๐ณ๐ด
Use console.log to see the values.
007
In this code you will see name, password and select tag where value of select tag i have stored in one variable. I want to pass that variable to views.py and i have used ajax for whole process
Anders (izzno / gooood) ๐ณ๐ด
But if I understand this correctly you are using jquery to send the form ?
007
Yeah.. when user click on submit button value is store in variable and ajax is activated because form action and ajax url is same .. so it will trigger to views.py function
Anders (izzno / gooood) ๐ณ๐ด
Yes, but this will fail becaouse the form will come without a crsf token
Anders (izzno / gooood) ๐ณ๐ด
And e.preventDefault() and return false does the same thing.
007
But in views.py :-
If request.is_ajax():-
Print ("ajax request...!")
007
Printed successfully
Anders (izzno / gooood) ๐ณ๐ด
well i dont know what that means...
007
Ok... Thanks buddy 😇โ๏ธ
Anders (izzno / gooood) ๐ณ๐ด
https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html
007
007
😇😇👍👍👍
Anders (izzno / gooood) ๐ณ๐ด
But again, why dont you use plain JS ? What you are trying to do you can just use the code example I gave you.
007
Anders (izzno / gooood) ๐ณ๐ด
var choice = $("#my-select").val(); <โ- does not seem to do anything except define variables
Slava
G'day, I'm new in Django. Can you give me useful links?
Anders (izzno / gooood) ๐ณ๐ด
Which code brother
https://gist.github.com/izznogooood/207271d5b3445c508f553a0003feab13
Mirco
Slava
Official docs
it's a good solution for fixing problems. But do u know any tutorials for beginners?
Anders (izzno / gooood) ๐ณ๐ด
Which code brother
var choice = $("#my-select").val(); <โ seems to me like you are not adding this information to your form variable.
007
007
007
When user select any option it's value store in variable
Anders (izzno / gooood) ๐ณ๐ด
Yes, but your JS code. you define it, getting the values from the html. But where do you add it to the form you serialize()
007
Then i get that valu in views.py , I'll compare that value to my existing model and if it matched then I'll store user and password
Info to DB
Anders (izzno / gooood) ๐ณ๐ด
add console.log(form) to your code below e.preventDefault()
007
Anders (izzno / gooood) ๐ณ๐ด
I'm pretty sure your not getting itt
Anders (izzno / gooood) ๐ณ๐ด
the value that is
007
Yeah... You are right
Mirco
Anders (izzno / gooood) ๐ณ๐ด
Kaushal
https://dpaste.de/Q1cp
007
Anders (izzno / gooood) ๐ณ๐ด
yes
007
Ohhk... Actually I don't have any knowledge of es6 ,that's why i always prefer jQuery😅😅