In django your main website is your project which we have created in our last tutorial. In this tutorial we are going to create django apps using startapp command. In django every website is a combination of apps.
What is app in django
In django the app terminology is very confusing. People thought that web apps are web pages or something else. But in django your whole websites is the combination of apps. In django each app is basically a standalone website. For example in above image we have a django project named MyWebSite and this project or website contains different apps such as (1,2,3) and so on. These apps might be blog, forum, music, videos and something else.
So guys basically in django your website is composed of all of these parts (forum, videos, music, about) is called an app. An app is nothing its just part of your website. Hope you guys will be understand and for the last time in django an app is the different section of your website. In our case we have a django project / website named MyWebsite and inside that we have different apps which we are going to create in this tutorial.
Django startapp
If you did not have created any project than read How to create django project.
So the question is how do we make one of this separate apps. To create a django app open up the command prompt using administrator navigate to your project directory which we have create in our previous tutorial and type python manage.py startapp your_app_name_goes_here.
App
python manage.py startapp product
The idea here is to creating productApp is that we are going to create an app that show different products. We are going to keep it pretty simple for your understanding. So for we have created our first webapp using django startapp command. Now go to your main project directory and find your newly created app, its time to explore whats inside it.
Initially here you can see that we have few extra things which we did not have in our main project.
- migrations
- init.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
Migrations
What migrations are, migrations are basically a way to hookup all of your websites to your database.init.py
As we have already discussed this in our previous tutorial what it is, basically its says python to see our app (productApp) directory as a python package not a normal directory.admin.py
Every website in the world have an admin section. A section where you can go and access database, delete users, delete posts, add users, block users. So instead to have making itself django provides this using django startapp command.apps.py
Its pretty much our configuration file or settings for our newly created (productApp) app.
from django.apps import AppConfig
class ProductappConfig(AppConfig): name = 'productApp'
models.py
Next one is models. What model is, basically a model is a blue print of your database. How you are going to store data for your app. In other words models contain your database information.tests.py
What tests are, let's suppose you have a login form and user type input to logged in. And you want to adopt proactive approach related to SQL Injection or something like this. So you changed the source code related to login and some days ago users can't login due to bug in code or something broken in registration process. So for that reason you need test. And you have two option to test that. The first one is to check every process (registration, login, logout, post etc) manually, if you have a huge website than its not an easy activity. To overcome this issue you write your test classes in this file. This file will save your lots of time to making testing.view.py
View are just functions, python function, what they do is they get some user request and they give them back some response. Ninety percents of time it returns some web page, for example user request some web page and views return them back this page or view. The rest of the time it performs some other action instead of returning a web page. For example when user want to log out or download some thing. In simple words this python function takes some user request and response in some kind of way.So the clear idea of django app is given below: