0

I currently have a javascript variable called myVariableToSend that contains a single string and I need to send to my views where I can make raw SQL queries to gather corresponding data from the database and bring it back to my javascript. Here is what I have:

Javascript:

function scriptFunction(myVariableToSend){

        $.getJSON("http://127.0.0.1:8000/getData/", myVariableToSend, function(serverdata){
        window.alert(serverdata);
});

Views.py:

def getData(request):
    some_data = request.GET(myVariableToSend)
    cursor = connection.cursor()
    cursor.execute("SELECT Car_ID FROM cars WHERE Carname = %s ", [some_data])
    row = cursor.fetchall()
    return JsonResponse(row, safe = False)

Urls.py:

url(r'^admin/', include(admin.site.urls)),
url(r'^$', startpage),
url(r'^getData/$', getData ),

I don't think my server side script(views.py) is working because when I run my server, I get a http500 error. Any help would be appreciated. Thank you.

UPDATE:

I have found that when I comment out my entire Views.py and only put

def getData(request):

return JsonResponse({"hello":"World"}, safe = False)

I get no problems and the AJAX request works. But when I have my original getData, it doesn't work. When I add this line in my views.py:

some_data = request.GET(myVariableToSend)

, I get an error and the data isn't displayed

13
  • Why are using a raw SQL query? Haven't you figured out Django's core feature ORM, yet? Then you should really do the tutorials. Commented Aug 17, 2016 at 19:38
  • Yes I know about Django's ORM, but I want to do it this way. Its for learning purposes. Commented Aug 17, 2016 at 19:41
  • You should set DEBUG to True. If you did, you'd see you get a NameError in the first line of that function, because the variable myVariableToSend is not defined. Commented Aug 17, 2016 at 19:42
  • myVariableToSend is defined by within another javascript function that calls this function scriptFunction. Commented Aug 17, 2016 at 19:44
  • No, the Python function. Commented Aug 17, 2016 at 19:49

2 Answers 2

1

If u want to send ur variables to function in view, u can capture it with url, like this:

$.getJSON('http://127.0.0.1:8000/getData/' + myVariableToSend +'/', function (serverdata) { //do ur work}

Then in urls.py you have:

url(r'getData/(?P<my_var>\w+)/$', views.get_data, name='get_data')

Then views.py:

def get_data(request, my_var):
#do ur work here
Sign up to request clarification or add additional context in comments.

4 Comments

Isn't it bad practice to send data through URL instead of through request?
If ur data is sensitive, then maybe it is. I use this solution in my project, but the data which I send through URL is not private.
Is there a way to go about this by passing data with request instead of url? I appreciate your answer, but I just would like to see how the other way would be done. Thank you.
I changed everything to what you put and I am getting a 404 error now.
1

Answering the original question:

Your server is failing probably because bad syntax in views.py

some_data = request.GET(myVariableToSend)

myVariableToSend is undefined here. So you should get it like this:

some_data = request.GET['myVariableToSend']

Besides the original question:

You'll get a lot of headaches if you try to set up your django app like this.You can query your database way easier if you use django's ORM. Read about it here.

Also, if you want to send the data in your models to your javascript code, you can save yourself lots of time by using a framework like Django REST Framework.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.