007
https://dpaste.de/MBJ3
007
I am able to get form value... But not variable
007
I am facing problem in views.py
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
007
Yes
007
I want to print choice variable value in views. Py
Mirco
I want to print choice variable value in views. Py
What does your console print on console.log(choice) ?
Mirco
none
Not python, browser console
007
Not python, browser console
It print option value in console
Mirco
It print option value in console
So you can get its value in the correct way 😁
Mirco
If u print into your view just request.POST, what do you get ?
007
Is it correct way :- request.POST.get('choice')
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?
Anders (izzno / gooood) 🇳🇴
In Django and flask integration is possible?
Depends what you mean... You couild run them in a docker stack...
Mirco
It print user, password but not choice
Print all request.POST and tell me what it gets 😁
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
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
It doesn't show choice... Maybe because of choice is variable which store select tag value.
Mirco
So it's normal you get None by calling your choice
007
Yes
007
So it's normal you get None by calling your choice
What should i do to get choice variable value. Because my ajax request perform correctly
Anders (izzno / gooood) 🇳🇴
https://gist.github.com/izznogooood/207271d5b3445c508f553a0003feab13
Anders (izzno / gooood) 🇳🇴
This might help you.
Anders (izzno / gooood) 🇳🇴
Its a dictionary.
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) 🇳🇴
Where form is pointed by $(this)
I dont know where you are now.
Anders (izzno / gooood) 🇳🇴
Or what you are talking about.
007
Or what you are talking about.
This is my code :- https://dpaste.de/MBJ3
Anders (izzno / gooood) 🇳🇴
This is my code :- https://dpaste.de/MBJ3
ah, this is jquery, I dont know that.
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
007
Use console.log to see the values.
In console.log it printed correctly
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
And e.preventDefault() and return false does the same thing.
Something new to learn 😄😄
Anders (izzno / gooood) 🇳🇴
Something new to learn 😄😄
is_ajax() <— this was new to me. I dont use jquery ;)
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.
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
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
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
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()
Yeah... That may be correct way... I am not sure that variable is passing using serialize ()
Anders (izzno / gooood) 🇳🇴
I'm pretty sure your not getting itt
Anders (izzno / gooood) 🇳🇴
the value that is
007
Yeah... You are right
Anders (izzno / gooood) 🇳🇴
Kaushal https://dpaste.de/Q1cp
007
Kaushal https://dpaste.de/Q1cp
Is that written is js es6 ?
Anders (izzno / gooood) 🇳🇴
yes
007
Ohhk... Actually I don't have any knowledge of es6 ,that's why i always prefer jQuery😅😅