Django- Creating an App
February 13th 2020 1329
Now that our environment – a “project” – is set up, we’re set to start doing work. In this tutorial, we will learn what is App in Django and we will create an account App to handle the user signup feature. So let's get started.
What is App?
An App is a Web application that does something. So for every functionality, an app can be created like a completely independent module. Let's understand App in Django with this realistic example:
Say you are building an online shopping site (e-commerce site) in Django so our Project will consist of Apps like:
Cart:- Which would have logic for users selected items for purchase.
Products:- Which would have logic for products that the site is selling.
Profile:- Which would have logic for user information.
Each application you write in Django consists of a Python package that follows a certain convention. Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories.
Benefits of using Django apps –
Django apps are reusable i.e. a Django app can be used with multiple projects.
Components are almost independent
Multiple developers can work on different components
Debugging and code organization is easy.
How to create a Django App
To create your app, make sure you’re in the same directory as
manage.py and type this command:
python manage.py startapp account
That’ll create a directory
account, which is laid out like this:
account/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
__init__.py − Just to make sure python handles this folder as a package.
admin.py − This file helps you make the app modifiable in the admin interface.
models.py − This is where all the application models are stored.
tests.py − This is where your unit tests are.
views.py − This is where your application views are (actual logic).
To consider the account app in your project you need to specify your app name in the INSTALLED_APPS list as follows in social/settings.py file.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', #add the app name here 'account', ]
INSTALLED_APPS contains the following apps, all of which come with Django:
django.contrib.admin– The admin site. You’ll use it shortly.
django.contrib.auth– An authentication system.
django.contrib.contenttypes– A framework for content types.
django.contrib.sessions– A session framework.
django.contrib.messages– A messaging framework.
django.contrib.staticfiles– A framework for managing static files.
These applications are included by default as a convenience for the common case.
Some of these applications make use of at least one database table or if our app makes use of database table (models), though, so we need to create the tables in the database before we can use them. To do that, we use the following command:
python manage.py migrate
So in this tutorial, we learn how to create an App in Django and how to configure the App inside the settings. In the next tutorial, we will learn about Models and we will write our first Model in Django.