Accessing GET Form data from - Javascript Form in Django
Question: I'm having trouble with Django in terms of getting data from a Javascript
form. Here is my Javascript code...

    
    
        function save() {
        var form = document.createElement("form");
        console.log(form);
        form.setAttribute('method', 'get');
        form.setAttribute('action', '/quiz_score/');
        document.body.appendChild(form);
        var i = document.createElement("input");
        i.setAttribute('name', 'Score');
        i.setAttribute('value', ""+score);
        form.appendChild(i);
        var i = document.createElement("input");
        i.setAttribute('name', 'csrfmiddlewaretoken');
        i.setAttribute('value', '{{ csrf_token }}');
        form.appendChild(i);
        form.submit();
    }
    

I know using GET isn't ideal however I couldn't get POST working, it simply
wouldn't redirect to the target page.

Here is my Django Class and function...

    
    
    class QuizScoreView(TemplateView):
    template_name = "quiz_score.html"
    
    
    def quiz_score(self, request):
        # Quiz.objects.create(username= ,score= )
        print("Score: "+request.body)
    

I am simply trying to get the score variable so I can use it in python.

Please comment if you need any more details and I will add them to the
question below.


Answer: I got it to work using the following HTML/JavaScript:

    
    
    <html><body>
    <button onclick="save();">click me</button>
    <script>
    function save() {
        var form = document.createElement("form");
        console.log(form);
        form.setAttribute('method', 'get');
        form.setAttribute('action', '/quiz_score/');
        document.body.appendChild(form);
        var i = document.createElement("input");
        i.setAttribute('name', 'Score');
        i.setAttribute('value', "+score");
        form.appendChild(i);
        var i = document.createElement("input");
        i.setAttribute('name', 'csrfmiddlewaretoken');
        i.setAttribute('value', '{{ csrf_token }}');
        form.appendChild(i);
        form.submit();
    }
    </script>
    </body></html>
    

View:

    
    
    from django.shortcuts import render
    
    def quiz_score(request):
        context = {'score': request.GET['Score']}
        return render(request, 'quiz_score.html', context=context)
    

urls.py:

    
    
    url(r'^quiz_score/$', quiz_score)
    

I noticed in your JavaScript you have `i.setAttribute('value', ""+score);`.
Maybe that's supposed to be `i.setAttribute('value', "+score");` or something
similar?

I went with a straight function view. You have a interesting mix of
TemplateView and function based view. If you wanted to use a TemplateView, you
could do something like:

    
    
    from django.views.generic import TemplateView
    
    class QuizScoreView(TemplateView):
        template_name = 'quiz_score.html'
    
        def get(self, request, *args, **kwargs):
            context = self.get_context_data(**kwargs)
            context['Score'] = request.GET['Score']
            return self.render_to_response(context)
    

urls.py:

    
    
    url(r'^quiz_score/$', QuizScoreView.as_view())
    

Hope that helps!

