Introduction
This is an article where the content will show how to be able to display or to print text as an output. In this context, the target for displaying or printing the text is a Django application. So, in order to do that, it need a running Django project. Specifically, a running Django application exist inside of it. Actually, it is a simple task as it also very important to know how to do it. Basically, it is very useful to know that simple task. Before even going on further to a more complex task. For getting on to do that simple task, the local device performing it need a suitable environment. Below are the steps for preparing that suitable environment :
-
First of all, prepare python tool or utility by installing it in to the device. For a reference on how to do that, just look at How to Install Python in Microsoft Windows and also How to Install Python in Microsoft Windows 11.
-
Continue on to the next step, just install pip tool utility. As a reference, check in How to Install pip in Microsoft Windows and also How to Install pip in Microsoft Windows 11.
-
Using python tool and also pip tool, create a python virtual environment. Check an article as a reference in How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows and also How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows. After the python virtual environment exist, just activate it.
-
After activating the python virtual environment, just install the Django library. In this context, Django library is important for creating a Django project and Django application.
-
Finally, create a Django project and also Django application by referring from an article in How to Create Django Project in Microsoft Windows using Command Line and also another article in How to Create a Django Application inside a Django Project in Microsoft Windows.
As an additional information, below is the actual structure of the Django project along with its Django application :
(env) C:\django\myproject>tree /A Folder PATH listing for volume Windows-SSD Volume serial number is CA30-19A4 C:. +---myapp | +---migrations | | \---__pycache__ | +---templates | | \---myapp | \---__pycache__ \---myproject \---__pycache__ (env) C:\django\myproject>
On the other hand, in the Django application, it will have the following basic default structure with their content respectively :
(env) C:\django\myproject> cd myapp (env) C:\django\myproject\myapp>tree /F Folder PATH listing for volume Windows-SSD Volume serial number is CA30-19A4 C:. │ admin.py │ apps.py │ models.py │ urls.py │ views.py │ __init__.py │ ├───migrations │ │ __init__.py │ │ │ └───__pycache__ │ __init__.cpython-310.pyc │ ├───templates │ └───myapp │ index.html │ └───__pycache__ __init__.cpython-310.pyc (env) C:\django\myproject\myapp>
How to Display or Print Text as an Output in Django Application
After preparing all of the suitable and necessary environment, just do and perform the following steps in order to display or text as an output :
-
First of all, for display or print text as an output, just create a URL for accessing the process to execute that. Creating an URL will be possible by adding the URL definition. Just place that URL definition in an URL file in the ‘myapp’ folder. In this context, ‘myapp’ represent the Django application folder. Consecutively, the ‘urls.py’ file exist inside of it will represent the URL for the Django application. If the file is not exist, just create it first. Below is the default content of the file :
from django.contrib import admin from django.urls import path from . import views urlpatterns = [ ]
-
Add the following line :
path('index-print-text/', views.index_print_text, name="index-print-text"),
In the above context, the URL will normally be ‘http://url_address/index-print-text/’. So, the ‘urls.py’ file content will be as follow :
from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path('index-print-text/', views.index_print_text, name="index-print-text"), ]
-
Next step, just create the function where the process will actually execute to display or to print text. According to the URL definition, it will be in ‘views.index_print_text’. In that case, the process definition will be exist in the file with the name of ‘views.py’ with the name of the function is ‘index_print_text’. So, open ‘views.py’ file which exist in the Django applicaton folder of ‘myapp’ and then add the function as follows :
from django.http import HttpResponse def index_print_text(request): return HttpResponse('This is printing a text from Django Application using a HTTP Reponse')
-
Finally, start the internal Django server from command prompt where normally it will be available in ‘http://localhost:8000’ as it exist below :
(env) C:\django\myproject>python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). December 11, 2022 - 13:37:26 Django version 4.1.2, using settings 'myproject.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK
-
Last but not least, just access the URL address as exist in the URL address definition inside ‘urls.py’ file using the URL ‘http://localhost:8000/’. Below is the appearance of the page :