4- Creating the First Django Application

Apache, PostgreSQL and Django Project is configured. Now we can create our first Django App. I will name this app as moviesapp.

cd Movies
source v_envCRM/bin/activate
django-admin startapp moviesapp

 

 

Add the new app to the end of INSTALLED_APPS dictionary in settings.py file

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'moviesapp',
]

 

Our new app does not have a urls.py. So, create an empty urls.py file in moviesapp folder first.

 

Every new application’s urls.py should be included in the main urls.py like below. To do that first import include class to main urls.py. Then include the new app’s urls.py as a path in urlspattern like this. 

from django.contrib import admin
from django.urls import path, include


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

 

Now we need to create a url, and a view in our moviesapp to test if everything runs correctly.

urls.py (moviesapp urls.py)

from django.urls import path
from . import views

urlpatterns = [
    path('', views.moviesapp, name='moviesapp'),
]

 

views.py

from django.shortcuts import render
from django.http import HttpResponse


def moviesapp(request):
    return HttpResponse("Hello")

 

 

Restart Apache service

sudo service apache2 restart

/moviesapp shows our hello message.