Django First Lecture

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Download the latest version of Python and install

Create a directory for the project

Create a project with commond

django-admin startproject HelloWorld


cd HelloWorld
ls
HelloWorld manage.py

Note that we have manage.py file and HelloWorld folder inside


HelloWorld project.

1. __init__.py: An empty file that tells Python that this directory should
be considered a Python package.
2. settings.py: Settings/configuration for this Django project. Django
settings will tell us all about how settings work. In other words, this
file will hold all apps, database settings information.
3. urls.py: The url declarations for this Django project; a "table of
contents" of our Django-powered site. This is a file to hold the urls of
our website such as "http://localhost/HelloWorldApp". In order to use
/HelloWorldApp in our HelloWorld project we have to mention this in
urls.py.
4. wsgi.py: An entry-point for WSGI-compatible web servers to serve
our project. This file handles our requests/responses to/from django
development server.

$ python3 manage.py runserver


Validating models...

0 errors found
Django version 1.6.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Creating Hello World App


To create an app (inside HelloWorld project directory), we need to run the
following command:

$ django-admin startapp HelloWorldApp


$ ls
HelloWorld HelloWorldApp manage.py
As we can see from the layout in picture above, the django-admin
startapp app created files:

1. __init__.py - this file indicates our app as python package.


2. models.py - file to hold our database informations.
3. views.py - our functions to hold requests and logics.
4. tests.py - for testing.

Edit settings.py
We need to edit settings.py under HelloWorld project directory to add our
application HelloWorldApp as shown below:

# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'HelloWorldApp',
)

Edit urls.py
How does Django know what view to send a particular request?
Django uses a mapping file called urls.py which maps html addresses to
views, using regular expressions. In other words, Django has a way to map
a requested url to a view which is needed for a response via regular
expressions.

Let's modify the urls.py which is under the project directory, HelloWorld:

Edit urls.py

from django.contrib import admin


from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myprojectApp.url')),
]

create new url.py file under HelloWorldApp and add the following code
from .views import Home
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('Home', Home),
]

Finally add in views.py


from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def Home(request):
return HttpResponse("Hello world")

Run server python3 manage.py runserver


create project

django-admin startproject HelloWorld

create new application

django-admin startapp HelloWorldApp

You might also like