Before you can create a Django app, you must first create the project in which is resides. This post will be referencing the project named “tutorial.” A project will have one or more apps. For each area of functionality within your project you should have an app. For instance, in a My Store project you might have one app for Customer Information named “customer,” and another app for Sales, named “sales.” Within each of these apps lots of functionality can be built for that area, and information can be shared from one area to another.
At this point we will start building two super simple apps: location and service. The purpose of the service app will be to store services to be performed, naming them “workorders.” The location app will store the name and location of customers.
create each app
From the command prompt run:
(tutorial) c:\tutorial>python manage.py startapp location
(tutorial) c:\tutorial>python manage.py startapp service
The following should now be seen in the file structure:
- tutorial
- tutorial
- location
- _init_.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
- service
- _init_.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
- manage.py
Summary of these files created for each app:
- _init_.py → provides the same functionality as that in the _init_.py file in the Django project structure. It is an empty file and does not need any modifications. It just represents that the app directory is a package.
- admin.py → used for registering the Django models into the Django administration. It is used to display the Django model in the Django admin panel. It performs three major tasks:
- Registering models
- Creating a Superuser
- Logging in and using the web application
I will not be going into detail about this, but it is certainly worth exploring. There is a lot of functionality you can get quickly by using this file.
- apps.py → is used to help the user include the application configuration for their app.
- models.py → represents the models of web applications in the form of classes. It is considered the most important aspect of the App file structure. Models define the structure of the database. It tells about the actual design, relationships between the data sets, and their attribute constraints.
- views.py → Views are also an important part when we talk about the Django app structure. Views provide an interface through which a user interacts with a Django web application. It contains all the views in the form of classes and functions.
- urls.py → works the same as that of the urls.py in the project file structure. The primary aim being, linking the user’s URL request to the corresponding pages it is pointing to. You won’t find this by default under the app files. But we will be adding it later for each app.
- tests.py → allows the user to write test code for their web applications. It is used to test the working of the app.
installed apps
Add the new apps to the list of INSTALLED_APPS in the file, tutorial\settings.py. Find the section in the file, and modify as follows:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'location',
'service',
]
create models
Models are required to create tables to store our data. Open models.py for each app and insert the indicated code.
# location\models.py
from django.db import models
class Location(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
state = models.CharField(max_length=2, default='KS')
zipcode = models.CharField(max_length=10, null=True, blank=True)
phone = models.CharField(max_length = 50, null = True, blank = True)
def __str__(self):
return (self.name) + ', ' + self.address + ', ' + self.city
# service\models.py
from django.db import models
from location.models import Location
class WorkOrderType(models.Model):
name = models.CharField(max_length=50)
code = models.CharField(max_length=10)
description = models.CharField(max_length=500)
def __str__(self):
return self.name
class Meta:
ordering = ["name"]
class WorkOrder(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
work_order_type = models.ForeignKey(WorkOrderType, on_delete=models.CASCADE, default=1)
work_order_date = models.DateField(default=today)
description = models.CharField(max_length=500)
def __str__(self):
return self.work_order_type.code + ": " + str(self.id)
create tables
To create the tables in the database, run the following from the command prompt.
(tutorial) c:\tutorial>python manage.py makemigrations
Successful results:
Migrations for 'location':
location\migrations\0001_initial.py
- Create model Location
Migrations for 'service':
service\migrations\0001_initial.py
- Create model WorkOrderType
- Create model WorkOrder
(tutorial) c:\tutorial>python manage.py migrate
Successful results:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, location, service, sessions
Running migrations:
Applying location.0001_initial... OK
Applying service.0001_initial... OK
Back in the mySql database, you will now see these additional tables:
- location_location
- service_workorder
- service_workordertype
Load tables
For a quick start, you may want to load data into your tables. To do so, run these sql statements in mySql.
insert into location_location (name, address, city, state, zipcode, phone)
values ('Jerry Jones','100 N Oak','Topeka','KS','11111','111-222-1234');
insert into location_location (name, address, city, state, zipcode, phone)
values ('Mary Malcum','100 N Main','Topeka','KS','11112','111-222-3333');
insert into location_location (name, address, city, state, zipcode, phone)
values ('Betty Thompson','100 S 23rd St','Topeka','KS','11113','111-222-4343');
insert into location_location (name, address, city, state, zipcode, phone)
values ('David Davidson','100 W Birch Ave','Topeka','KS','11114','111-222-5689');
insert into service_workordertype(name, code, description)
values ('Windows','WIN','Wash windows');
insert into service_workordertype(name, code, description)
values ('Carpet','CPT','Clean carpets');
insert into service_workordertype(name, code, description)
values ('Snow','SNOW','Shovel snow');
insert into service_workordertype(name, code, description)
values ('House','HOUSE','Clean the whole house');
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-01-04','Dust furniture, vacuum carpets and clean bathrooms',2,4);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-02-01','Dust furniture, vacuum carpets and clean bathrooms',2,4);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-03-01','Dust furniture, vacuum carpets and clean bathrooms',2,4);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-03-01','Wash all windows',2,1);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-01-11','Clear the driveway and sidewalk',3,3);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-02-12','Clean bedroom carpets',1,4);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-02-28','Clean all carpets',4,4);
insert into service_workorder(work_order_date, description, location_id, work_order_type_id)
values ('2022-03-28','Wash all windows, inside and out',4,1);
We now have a project with apps, tables, and even some data. But our project still doesn’t do anything. To take the next step, go to Create Views and Templates.
Proudly powered by WordPress