1

I have the following problem with Django. I am sending a List to Javascript and I spect to get this result in the browser. Instance of this looks like my code ignore everything what I do.

My code looks like this

view.py

import itertools

from django.shortcuts import render
from django.views.generic import TemplateView

from django.http import HttpResponse, JsonResponse
import json

def index(request):
    return render(request, 'home.html')


class UsageView(TemplateView):
    template_name = 'overview.html'


def getFTest(request):
    .....

    context = {"Features": dataFeature}
    json_context = json.dumps(context)
    return render(request, "overview.html",  {"context_array" : json_context})

url.py

from django.contrib import admin
from django.urls import path
from .views_ import index, getFTest

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path(r'^admin/', admin.site.urls),
    path('', index, name="home"),
    path('overview', LicUsageView.as_view(), name="overview"),
    path('', getFTest, name="getFTest"),
]

overview.html

var test = ["{{ context_array|safe }}"];
console.log(test);

result from the console: an empty List

enter image description here

Can somebody tell me where my error ist? Thanks very much

1 Answer 1

2

You have two paths with as path pattern '', so that means that if you visit '', it will always use the index view, and not getFTest, as a result it will thus not render the template with the context_array, and thus result in rendering an empty string.

You thus should set getFTest to render with the root path, or set up a different path pattern:

from django.contrib import admin
from django.urls import path
from .views_ import index, getFTest

urlpatterns = [
    path(r'^admin/', admin.site.urls),
    path('', getFTest, name='getFTest'),  # ← use getFTest
    path('overview', LicUsageView.as_view(), name="overview"),
]
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer. But what I do to send then this specific information to my templates overview.html when I have already a Path with ' ' for index, home.html and I have one with 'overview' to load the template as_view(). Now I need to send information to overview.html template, but "overview" is already in use between my Path() definition
@zgrizzly_7: well that is not possible. The idea is that when you fetch a URL, then you trigger one view, not multiple. A view does not per se needs to render a template, and it can render multiple templates to combine text, but the idea is that the template thus returns a HTTP response. Templates are only a tool to make it more convenient to generate text, but you thus have always one view per request.
ok, I think I understand more and more how Django is working, but any suggestion to solve this problem? 2 differents requests for one template
@zgrizzly_7: if you want to pass the data through a different channel, yes. This is basically what a lot of sites do through AJAX calls. If the data is passed together with the index, why not update the index view, and include the data to render it.
yes, exactly what I was thinking. With a AJAX call is working. Thank you very much!

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.