Algoteka
Log in to submit your own samples!

HTML Response Through Templating

Problem by oml1111
# Tech tags Title Creator Created date
1 0
Django
2022-10-02 21:56
View all samples for this language (1 verified and 0 unverified)

solution | Python | Django |

By oml1111 |
0 likes

Uses the Django template language to generate a response, using a variable from inside the server backend code for content.

Prerequisites

Code

django_sample\settings.py

...

INSTALLED_APPS = [  
    'example.apps.ExampleConfig', # Make the template documents available
    ...
]

example\urls.py

from django.urls import path  
import example.views as views  
  
urlpatterns = [
    path('template-sample', views.view_template_sample, name='template_sample'), # Responds to the /template-sample url  
]

example\views.py

from django.shortcuts import render  


def view_template_sample(request):  
    str_var = "Hello World!"
    return render(request, "sample.html", {'str_variable': str_var})

example\templates\sample.html

<document>  
    <body>
        {{ str_variable }}  
    </body>  
</document>

Further reading

Writing your first Django app, part 3 - djangoproject.com
Templates - djangoproject.com

References

functions
django.shortcuts.render docs.djangoproject.com
django.urls.path docs.djangoproject.com

Problem Description

Use a templating engine to generate an HTML response for some document. The process should use a string variable to generate some of the content.

View sample discussion (0 comments)
View problem discussion (0 comments)