0

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>
...
  • You forget to ask a questioN. Can you explain more your question ? You can edit your question to do it :) – Elikill58 Sep 28 '21 at 09:02
  • @Elikill58 I wanted to ask how to correctly pass js variable to html template as tag in my case. I tried ajax request, but i don't know is it right. – Becker Wesley Sep 29 '21 at 12:19
  • I see. I think it's not, I suggest you to check [this](https://stackoverflow.com/questions/23879733/use-javascript-variable-in-django-template), or [this](https://stackoverflow.com/questions/51299980/how-to-pass-a-javascript-variable-in-django-template-tag) or finally [this one](https://stackoverflow.com/questions/298772/django-template-variables-and-javascript) and see if it answer to your question – Elikill58 Sep 29 '21 at 12:30

0 Answers0