Creating the First Django Application

I am going to create an app named crmapp. First I change the directory to 

cd CRM
source v_env/bin/activate
cd PrjCrm
django-admin startapp crmapp

 

Add the new app to the end of INSTALLED_APPS dictionary

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

 

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

 

Every new application’s urls.py should be included to 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('crmapp/', include('crmapp.urls')),
]

 

Now we need to create a url, view and a template for our crm app.

urls.py (crmapp's urls.py)

from django.urls import path
from crmapp import views

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

 

views.py

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

def crmapp(request):
    msg = "hello"
    return render(request, 'crmapp.html', {'welcome_msg':msg})

 

Create a folder named templates in crmapp folder. In templates folder, create a file named crmapp.html. The content of crmapp.html is below:

{% block title%}
    <title>Welcome To The Crm App!</title>
{% endblock title%}

{% block content %}
    <div class="container">
    {{ welcome_msg }}
    </div>
{% endblock content %}

 

Restart Apache service

service apache2 restart

 

© selimatmaca.com. All Rights Reserved.