0% found this document useful (0 votes)
39 views5 pages

CC Experiment 9

Uploaded by

Aamir Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

CC Experiment 9

Uploaded by

Aamir Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment – 9

Aim: Develop a hello world program web application and deploy it on the Google app
engine.
Description:

We are preparing a web application for simple “Hello world” Application and host it on
Google App Engine.

How Django works?

Django is a MVC architecture to develop a web application. MVC means Model – View –
Controller. This is a popular way to internally separate the data, logic, and display of an
application into separate components that are easier for a developer to reason about.

In the traditional MVC pattern there are three major components:

 Model: Manages data and core business logic


 View: Renders data from the model in a particular format
 Controller: Accepts user input and performs application-specific logic

A simplified version of this complete Django flow looks like this:


Steps to create Web Application:

Steps 1: Create a directory for storing project and its components using following commands

# Windows
$ cd onedrive\desktop\code
$ mkdir helloworld
$ cd helloworld

Step 2: Install the Django package on python using following command Run the following
commands

$ python -m venv .venv


$ .venv\Scripts\Activate.ps1
(.venv) $ python -m pip install django~=4.2.0
(.venv) $ python -m pip install black

Step 3: To create a new project withing this directory named “django_project”


(.venv) $ django-admin startproject django_project .

We will be having following Directory Structure created by Django


├── django_project
│ ├── __init__.py
| ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── .venv/

To check the project running or not

(.venv) $ python manage.py runserver

Than goto browser and open link


http://127.0.0.1:8000/

we will be having following screen, if it works fine.


Now we will proceed for creating web application. Follow the steps below

To add a new app go to the command line and quit the running server with Control+c. Then
use the startapp command followed by the name of our app which will be pages.

(.venv) $ python manage.py startapp pages

Following Directory structure will be created in “helloworld” directory.


├── pages
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py

1. Add “pages” at bottom in settings.py file under INSTALLED_APPS tag to let know
Django that application is created.
Even though our new app exists within the Django project, Django doesn’t “know”
about it until we explicitly add it to the django_project/settings.py file.
# django_project/settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"pages", # new
]

2. Create a view in “views.py” inside “pages” app.

view is a Python function that accepts a Web request and returns a Web response. The
response can be the HTML contents of a Web page, a redirect, a 404 error, an image, or
really anything.

Code should be in views.py file is:

from django.shortcuts import render


from django.http import HttpResponse

# Create your views here.

def homepage(request):
return HttpResponse("Hello World!")

3. Moving along we need to configure our URLs. In your text editor, create a new file
called urls.py within the pages app. Then update it with the following code:
# pages/urls.py
from django.urls import path

from .views import homepage

urlpatterns = [
path("", homepage, name="home"),
]
4. We’re almost done at this point. The last step is to update
our django_project/urls.py file. It’s common to have multiple apps within a single
Django project, like pages here, and they each need their own dedicated URL path.

# django_project/urls.py
from django.contrib import admin
from django.urls import path, include # new

urlpatterns = [
path("admin/", admin.site.urls),
path("", include("pages.urls")), # new
]

We’ve imported include on the second line next to path and then created a new URL
pattern for our pages app. Now whenever a user visits the homepage, they will first be
routed to the pages app and then to the homePageView view set in
the pages/urls.py file.

This need for two separate urls.py files is often confusing to beginners. Think of the
top-level django_project/urls.py as the gateway to various URL patterns distinct to
each app.

We have all the code we need now. To confirm everything works as expected, restart
our Django server:

(.venv) $ python manage.py runserver

If you refresh the browser for http://127.0.0.1:8000/ it now displays the text “Hello,
World!”

You might also like