0% found this document useful (0 votes)
7 views2 pages

Django App 1

Uploaded by

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

Django App 1

Uploaded by

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

# Create a directory for your virtual environment

# and Django project


mkdir tut1
cd tut1

# Setup Django virtual environment


pipenv install django==3.0

# Start the virtual environment


pipenv shell

# Create a project that will hold the apps


# Make sure you use a underscore and not a dash
# in the project name
django-admin startproject tut1 .

# Create a new app called pages


python manage.py startapp pages

# A Django project contains multiple apps that provide


# an individual piece of functionality.
# A different app for each user authentication,
# displaying database information, processing payments,...

# Project default files


# 1. admin : Config file for Django admin app
# 2. apps : Config file for the app
# 3. migrations : Tracks changes to the models file
# 4. models : Define db models that Django translates
# into db tables
# 5. tests : Used to test the app
# 6. views : Handles requests and responses with user

# You must add the app to the projects settings.py file


# Make sure it is added at the end
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages.apps.PagesConfig', <---- ADDED HERE
]

# Change the timezone to New York with


# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
TIME_ZONE = 'America/New_York'

# Django apps are made up of normally 4 files being


# urls.py in the project folder along with models
# views and the HTML template in the app folder

# When a user visits your site the url is mapped to


# a view which merges the HTML template with the model
# which is data from the database

# We'll set it up so that when the user requests a URL


# that we'll respond with Hello Django

# 1. Update views.py
from django.http import HttpResponse

def HomePageView(request):
return HttpResponse('Hello Django')

# 2. Create urls.py in the pages app


from django.urls import path

# Import views by looking in the directory for views.py


from .views import HomePageView

# Match for an empty request following the website


# name, reference the view named homePageView and
# an optional URL pattern
urlpatterns = [
path('', HomePageView, name='home')
]

# 3. Update the projects urls.py file to include our app


# This line says if they go to the website and don't
# provide a directory they will be sent to our app
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]

# 4. Start the server to test the app


# http://127.0.0.1:8000/
python manage.py runserver

You might also like