Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Django: How to pass js variable to html template as tag

My task is to send the result of the test, created using javascript, to the html-template to use it in the {% if mark> = 4%} tag, in order to write to the database that the test is passed. I tried composing an ajax request but not sure if it is correct.

views.py

def practice(request, lesson_id):
form = LessonDone()
posts = get_object_or_404(Lessons, pk=lesson_id)
lessonid = Lessons.objects.get(pk=lesson_id)
mark = request.POST.get('url')
if request.method == 'POST':
    p, created = DoneLessonsModel.objects.get_or_create(
        user=request.user,
        lessons=lessonid,
        defaults={'done': True},
    )
    form = LessonDone(request.POST, instance=p)
    if form.is_valid():
        try:
            form.save()
            return redirect('practice')
        except:
            form.add_error(None, 'Ошибка')
context = {'posts': posts, 'form': form, 'mark': mark}
return render(request, 'landing/../static/practice.html', context)

urls.py

...
    path(r'lesson/<int:lesson_id>/practice/', views.practice, name='practice'),
... 

practice.html

...
        <div class="buttons ">
        <button class="restart btn-dark">Перепройти тест{{ mark }}</button>
        {% if mark >= 4 %}
        <form action="" method="POST"> {% csrf_token %}
            {{ form }}
        {% endif %}
        <button class="quit btn-dark" type="submit" value="Submit" id="send-my-url-to-django-button">Выйти</button>
        {% if mark >= 4 %}
        </form>
        {% endif %}
    </div>
...
    </script>
    <script type="text/javascript">
        $(document).ready(function() {
            var url = userScore;
            $("#send-my-url-to-django-button").click(function() {
                $.ajax({
                    url: {% url 'practice' posts.pk %},
                    type: "POST",
                    dataType: "json",
                    data: {
                        url: url,
                        csrfmiddlewaretoken: '{{ csrf_token }}'
                        },
                    success : function(json) {
                        alert("Successfully sent the URL to Django");
                    },
                    error : function(xhr,errmsg,err) {
                        alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
                    }
                });
            });
        });
    </script>
...
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!