Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Web Technology by (19.9k points)

While learning Django, I can't figure out one simple thing. May be very obvious to any Django devs. I've read the docs and it seem to have to work. Here's my code:

#views.py

from django.shortcuts import render

def home(request):

    return render(request, 'radio/index.html', {'title': 'Radio'})

def function_name():

    return 'Hello, World!'

#index.html

<div>

    <h1>{{ function_name }}</h1>

</div>

Yet the result is:

<div>

    <h1></h1>

</div>

I must be missing something fundamental but can't find out what it is.

1 Answer

0 votes
by (25.1k points)

You need to pass a reference to the function in the context of your render(..) call, like:

#views.py

from django.shortcuts import render

def home(request):

    return render(

        request,

        'radio/index.html',

        {'title': 'Radio', 'function_name': function_name}

    )

def function_name():

    return 'Hello, World!'

Since otherwise, it is not in the context of the template, and hence you can not render it accordingly.

Browse Categories

...