App in Django

A project is a sum of many applications. Every application has an objective and can be reused into another project, like the contact form on a website can be an application, and can be reused for others. See it as a module of your project.

Create an Application

We assume you are in your project folder. In our main “myproject” folder, the same folder then manage.py −

$ python manage.py startapp myapp
D:\lab\batch28\myproject>python manage.py startapp myapp

You just created myapp application and like project, Django create a “myapp” folder with the application structure −

myapp/
   __init__.py
   admin.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.

Get the Project to Know About Your Application

At this stage we have our "myapp" application, now we need to register it with our Django project "myproject". To do so, update INSTALLED_APPS tuple in the settings.py file of your project (add your app name) −

INSTALLED_APPS = (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'myapp',
)

D:\lab\batch28\myproject>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

D:\lab\batch28\myproject>

 

 

###########################################################################

Install django
pip install django

1. Create project
D:\django>django-admin startproject myproject

Dont change(edit) manage.py

D:\django>django-admin startproject website

2. D:\django>cd myproject

3. Start App

D:\django\myproject>python manage.py startapp myapp

4. Create Super user D:\django\myproject>python manage.py createsuperuser

5. At this stage we have our "myapp" application, now we need to register it with our Django project "myproject". To do so, update INSTALLED_APPS tuple in the settings.py file of your project (add your app name) −

INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', )

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

If u get the above error, run 'python manage.py migrate'

 

D:\django\myproject>python manage.py makemigrations
Hello
Migrations for 'news':
news\migrations\0001_initial.py
- Create model Article
- Create model Reporter
- Add field reporter to article

D:\django\myproject>python manage.py migrate
Hello
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, news, sessions
Running migrations:
Applying news.0001_initial... OK

D:\django\myproject>

#######################################

 

########################################################