The command django-admin startproject project_name
creates a Django project called project_name
in the folder you ran the command in. The project folder structure looks like the below:
[project_name]
├── [project_name]
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
Django uses the name of your project (the one you entered in the command) as the name of your project configuration folder as well.
A much more recommended way is to start by creating a folder with the name of the project, say DjangoBlog
if you’re creating a Django blog project. Then, on your terminal, cd into that folder and create a virtual environment and activate it. Then install Django. Then after that run the following command to create a Django project. (Note: You can run this command before you install Django in the virtual environment because the command is from python that is installed on your computer)
$ django-admin startproject config .
The name config
is the name of your project folder, you can name it anything, but a preferable name is config
since it just contains the configuration files of your project (especially the settings.py). Once this is done, you now have a Django project. But you can’t really do much with it, what you can do for now is to run the Django development server just to check if everything is working fine. But for the most part, a Django project is powered by several apps that contain the functionality. So the next step here is to create a Django app.
Table of Contents
- What is an app in Django project?
- How To Create app Inside Django Project.
- Django App Naming Conventions.
- FAQs.
- Conclusion: How To Create app Inside Django Project
What is an app in Django project?
In Django, an ‘app’ is a directory dedicated to a specific set of functionalities. For instance, you might have an ‘accounts’ app that handles all user authentication processes.
How To Create app Inside Django Project.
The question of how to create an app Inside a Django project is a bit misleading since it suggests that you can also create a Django app outside of a Django project. You cannot do that. A better way will be to say, just how to create a Django app and you’ll be shown an app inside an already created Django project. That’s what I’ll show you in this section.
- On your terminal, cd into the project folder, (that contains the manage.py file)
- If you’re using a virtual environment, make sure it is activated.
- Run the command
python manage.py startapp app_name
, whereapp_name
is the name of the app. - In the settings.py file of your project folder, add the app to the list of INSTALLED APPS.
This is what your folder now looks like.
[project_name]
├── config
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── app_name # new app
│ ├── _init_.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
| ├── views.py
| ├── urls.py
| └── tests.py
└── manage.py
Check out this guide to get a closer look at what each of these files in our newly created app does.
How do I register an app in Django?
This section elaborates deeper on the fourth step of creating an app inside a Django project which is registering an app in Django. To register an app in Django is to make it recognizable as an app of your Django project. So after you run the startapp
command, you have to tell Django that you have another app in your project so that its code will be recognized as part of the whole project.
In your settings.py, you should see a list called INSTALLED_APPS. that’s where you will add your installed app.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name' # new
]
app_name should be the exact name of the app you just created.
Django App Naming Conventions.
There are a few naming conventions that you have to follow when creating a Django app. Most of these do not affect the internal working of your Python code, but they just provide you with a uniform and ordered project structure.
- Most often, they should be in plural form, for example,
accounts
,polls
,orders
orpages
. This, however, is not always the case, sometimes there are instances where naming apps in plural just do not sound or look right for example, the nameblog
. - Try to make them both short and single-worded, if you end up using more than a single word to name the Django app, just join the words as one without using underscores. Like
staffpages
as opposed tostaff_pages
. Unless it comes to the point of being unreadable then you should use underscores. - All the letters of the app name should be in lower case form.
Again, these naming conventions will not make or break your internal code. So if you rather prefer a different approach there is no problem following it.
FAQs.
Can we create app inside app in Django?
No, In Django you cannot create an app inside another app. You can only create an app inside a Django project.
How do I see installed apps in Django?
To see installed apps in Django, go to the settings.py file of your project folder. You will see the list of installed apps in the INSTALLED_APPS list. This list provides only those that have been registered to your Django project. You can also use the command line. Here is how to do it.
In your terminal, run the command:
(env) $ python manage.py shell
>>> from django.apps import apps
>>> apps.get_app_configs()
dict_values([<AdminConfig: admin>, <AuthConfig: auth>, <ContentTypesConfig: contenttypes>, <SessionsConfig: sessions>, <MessagesConfig: messages>, <StaticFilesConfig: staticfiles>, <BlogConfig: blog>])
This returns a list of the installed apps.
To learn more about Django apps, check out its guide on Applications.
How many apps can a Django project have?
In Django, you can make as many apps as you want per single project.
Conclusion: How To Create app Inside Django Project
I hope you were able to follow along this post and you now know how to create your own apps in Django. Let me know what you think about this post in the comments section below. Otherwise, see you in other Coding Gear posts.