0

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.

1
  • It's not clear what you're asking here. What is the quiz_score method for? What is calling it? Commented Apr 13, 2016 at 10:10

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

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.