Search code examples
javascriptpythondjangoget

Why is the content of the test.html file not showing up?


I am trying to send a lot of GET requests to my back-end, but can not understand what happening here

viws.py:

from django.views.generic import TemplateView

class HomePageView(TemplateView):
    template_name = "pages/home.html"

class AboutPageView(TemplateView):
    template_name = "pages/about.html"

class TestPageView(TemplateView):
    template_name = "pages/test.html"
    
    def get(self, request, *args, **kwargs):
        num = request.GET.get('num')
        result = f"test {num} - status: ok"
        return HttpResponse(result)

test.html:

{% load static %}

{% block title %}Test{% endblock title %}

{% block content %}
<!DOCTYPE html>
<html>

<body>
    <h2>Test server</h2>

    <form id="function_form">
        <button type="submit" id="start_btn">Start</button>
    </form>
    <div id="result"></div>
    <script>
        document.getElementById("function_form").addEventListener("submit", function (event) {
            event.preventDefault();
            for (let i = 1; i <= 1000; i++) {
                fetch('/test/?num=' + i)
                    .then(response => response.text())
                    .then(data => {
                        document.getElementById("result").innerHTML += data + "<br>";
                    });
            }
        });
    </script>
</body>

</html>
{% endblock content %}

urls.py:

from django.urls import path
from .views import HomePageView, AboutPageView, TestPageView

urlpatterns = [
    path("", HomePageView.as_view(), name="home"),
    path("about/", AboutPageView.as_view(), name="about"),
    path("test/", TestPageView.as_view(), name="test"),
]

After I path to /test/ or, for example, /test/?num=2 I receive a page which has only "test 2 - status: ok" and nothing common with test.html.

Why on /test there is no content that I expected to see?


Solution

  • The problem is that you send a HttpResponse as answer to your get request.

        def get(self, request, *args, **kwargs):
            num = request.GET.get('num')
            result = f"test {num} - status: ok"   # here you define the string that you see as http output
            return HttpResponse(result)  # this is the plain response type that you are sending
    

    Utilize the render function!

    from django.shortcuts import render
    
    class TestPageView(TemplateView):
        template_name = "pages/test.html"
    
        def get(self, request, *args, **kwargs):
            # num = request.GET.get('num')
            # result = f"test {num} - status: ok"
            return render(self.request, self.template_name, context=self.get_context_data())
    

    render accepts arguments: First the request, second the template, third the context. Context is defined in the method get_context_data() for class based views. You could also just pass a dictionary there with your variables.

    On a second thought:

    Actually my solution above is the default of the TemplateView. You just have to get rid of "defining the response as HttpResponse("some text")" yourself. So this should work as fine:

    class TestPageView(TemplateView):
        template_name = "pages/test.html"
    
        # delete entire get method