Creating My First Django Application Part 2

Run Server to start the application

python manage.py runserver

Creating View

  1. Locate the views.py from the your app folder.
  2. Open views.py using an Editor of your choice
  3. Write the following Code
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hello(request):
text=”<h1>This is my first Application</h1>”
return HttpResponse(text)
  1. Returning a HttpResponse object containing the content of the page you request.
  2. Render a HTML file and send as a HttpResponse
  3. Raise an exception such as Http404,503 etc
  4. Rest depends on your requirement such as calling other function or requesting other urls

URL Mapping

  1. Locate the urls.py from the your project folder.
  2. Open urls.py using an Editor of your choice
  3. Write the following Code
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path(‘hello/’,views.hello)
]

Function views

  1. Add an import: from my_app import views
  2. Add a URL to urlpatterns: path(‘’, views.home, name=’home’)

Class-based views

  1. Add an import: from other_app.views import Home
  2. Add a URL to urlpatterns: path(‘’, Home.as_view(), name=’home’)

Including another URLconf

  1. Import the include() function: from django.urls import include, path
  2. Add a URL to urlpatterns: path(‘blog/’, include(‘blog.urls’))

Open Browser and open http://127.0.0.1:8000/hello

Our First Html Page

Render HTML files in views

  1. To enable templates in django, create a new folder in you app directory “templates”
  2. Django will fetch the .html file automatically from the directory
  3. Add a index.html (or any other named file in newly created directory)
<HTML><HEAD>
<TITLE>This is a Sample Django Application</TITLE>
</HEAD><BODY BGCOLOR=”FFFFFF”>
<HR>
<a href=”http://gtu.ac.in">Link Name</a>
is a link to another nifty site
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
Send me mail at <a href=”nareshkr.22@gmail.com”>
nareshkr.22@gmail.com</a>.
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
<HR>
</BODY>
</HTML>
views.py
urls.py
Flow of our Request

Open Browser and open http://127.0.0.1:8000/sample

Sample Output
  1. https://docs.djangoproject.com
  2. https://tutorialspoint.com/
  3. Django Unleashed — Book by Andrew Pinkham

--

--

--

Security Analyst making safe cyberspace 4 people #cyber4people India

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Today I am just praying for a breakthrough.

Case in point : How Flutter optimizes widget rebuilds and why it used phone number as otp

Scatter Plot using R

Why Don’t We Have A Multi-Part XR-Conglomerate Of Foundations Participating in Each-Others Goals…

The Growing Importance Of Real-Time Communication APIs In Banking

AWS Amplify: Cascade Deletion for GraphQL in a Lambda API (Part 2)

Step By Step Installation For Elasticsearch Operator on Kubernetes and Metircbeat, Filebeat and…

Kubernetes status view

IOEX Biweekly Report: Nov 16th — Nov 29th

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Naresh Kumar

Naresh Kumar

Security Analyst making safe cyberspace 4 people #cyber4people India

More from Medium

Django: Web Frame Work

Introduction To NodeJs

Integrating Flower with Celery in Django Project Using Docker

How to create a contact form in django in Django