Django First Lecture
Django First Lecture
Django First Lecture
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.
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.
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
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),
]