Python Django Helloworld Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will create a basic Python Django Helloworld application, this will help you get started with Python Django Framework from the basics.
Python 3.7, 3.8
Django framework 3.1
Step by step basic Django application development, let’s start from setting the virtual environment.
Using the following commands (inside terminal or command prompt) create a folder ‘Django-HelloWorld’ and within it create a virtualenv ‘env’. You can choose any name for the virtualenv (or venv).
For Linux users:
mkdir Django-HelloWorld
cd Django-HelloWorld/
python3 -m venv env
source ./env/bin/activate
For Windows users, to create and start a virtual env:
virtualenv env
env\Scripts\activate
The virtual environment is now created and activated. You should see a (env) at the beginning of the command line, indicating ‘env’ virtual environment is activated. It can be deactivated anytime using the command ‘deactivate’.
Install Django-framework within the virtualenv using the following command:
python -m pip install Django
Confirm installation using:
python -m django --version
You should get the version number ‘3.1.3’ or something similar.
We start by creating a Django project. This will auto-generate some code needed to run the application. This includes settings for running the application, database configuration, URLs patterns, debugging logs, etc.
Run the following command :
django-admin startproject helloDjango
Here, ‘helloDjango’ is the name of the project and you can name it as you want.
Running this command generates the following files:
helloDjango/
|-- helloDjango
|   |-- __init__.py
|   |-- asgi.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- manage.py
helloDjango/: root project directory
manage.py: A command-line utility that allows you to interact with the Django project in multiple ways. Also, do not edit any content of this file.
helloDjango/__init__.py: an empty to mark the directory as a python package.
helloDjango/settings.py: Contains the settings for the Django Project
helloDjango/urls.py: Contains the URL paths for the Django project
helloDjango/asgi.py: An entry-point for ASGI-compatible web servers to serve your project.
helloDjango/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
asgi.py and wsgi.py files are not required for development purposes and should be left untouched.
Now it is time to run the application. Ensure you are in the root directory (where manage.py is located) and use the command:
python manage.py runserver
There will be some migration errors, we don’t have to worry about them now in this simple application.
In a browser, open the URL localhost:8000, and you should see a welcome screen with the message “The install worked successfully! Congratulations!“.
Next, we actually start writing some code.
A Django application consists of multiple apps. These are like the sub-units or submodules of a bigger project, with each app taking care of one functionality or a feature of the entire application.
Terminate the running server using Ctrl+C.
Run this command from the root directory to create an app named ‘hello’.
python manage.py startapp hello
This will create a directory ‘hello’ with the following file structure:
hello
|-- __init__.py
|-- admin.py
|-- apps.py
|-- migrations
|   `-- __init__.py
|-- models.py
|-- tests.py
`-- views.py
models.py: Contains the database layout with some additional metadata
admin.py: Here we can register the models with django-admin
apps.py: Configuration file for the app
tests.py: Used for writing tests. Django uses its own testing framework
views.py: Contains the main logic for the app.
migrations/: This directory contains the changes that are made to create the current database schema.
Next, add our functionality for the app in the file hello/views.py
from django.http import HttpResponse
def helloWorld(request):
    return HttpResponse("Hello World")
We create the helloWorld() method, that returns an HttpResponse “Hello World”, upon receiving a request.
Now, we need to specify an URL, where we want to see our output.
For this create a file urls.py inside the app directory hello/ with the following content.
from django.urls import path
from . import views
urlpatterns = [
    path('', views.helloWorld),
]
Next, create a URL pattern for this app in the URL patterns for our project, ie. in the file helloDjango/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('hello/', include('hello.urls')),
    path('admin/', admin.site.urls),
]
Thus, whenever we go to the URL localhost:8000/hello/, the helloWorld() view (or function) in the file hello/views.py will be called.
We need to list our app in the INSTALLED_APPS list of our project.
Go to helloDjango/settings.py and add our newly created app’s name to the INSTALLED_APPS list, which should now look like this:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
]
Run your application using
python manage.py runserver

Now, navigate to http://localhost:8000/hello/ and you should see the “Hello World” text on the screen.
With this basic application, you can understand how to link the views and the URLs.
Django Official Documentation
Python Classes and Objects
Happy Learning 🙂

Python Selenium HelloWorld Example
How to Delete a File or Directory in Python
Spring MVC HelloWorld
Python – How to create Zip File in Python ?
How to get the size of a Directory in Python ?
How to Create or Delete Directories in Python ?
How to upgrade Python PIP version on Windows
Modes of Python Program
How to check whether a file exists python ?
AngularJs Directive Example Tutorials
Python Selenium Automate the Login Form
Where can I find Python PIP in windows ?
How install Python on Windows 10
Python MySQL create database and table
What are the List of Python Keywords

Python Selenium HelloWorld Example
How to Delete a File or Directory in Python
Spring MVC HelloWorld
Python – How to create Zip File in Python ?
How to get the size of a Directory in Python ?
How to Create or Delete Directories in Python ?
How to upgrade Python PIP version on Windows
Modes of Python Program
How to check whether a file exists python ?
AngularJs Directive Example Tutorials
Python Selenium Automate the Login Form
Where can I find Python PIP in windows ?
How install Python on Windows 10
Python MySQL create database and table
What are the List of Python Keywords
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list