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 .
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.
- Conclusion: How To Create app Inside Django Project
What is an app in Django project?
The idea of apps in Django may be confusing to new Django developers mostly because of the word app itself. We normally think of it (‘app’) as a short form for either mobile application (app), or desktop application, or any other application noun out there. But once you understand how these are related, you have already understood the app concept in Django.
An operating system, say WindowsOS or MacOS or AndroidOS or iOS comes with pretty much a lot of things for us to work with the computer but with it alone you cannot do much, if at all. Once you get a computer, one of the first things you start to do is to install apps from the apps store like browsers, messaging apps, etc, if they are not already installed. That being said most computers and mobile phones come with popular apps already installed to save your time and resources since you’re more likely to use them. So as you can see, an app is just a program that performs a specific function like messaging, or searching the web if the computer does not already do that for you.
Taking that into Django, an app becomes a folder that contains code for specific functionality, for example, you can create a Django app called
accounts that contains functionality for your app’s authentication functionality and related stuff. As you have seen above, the
startproject command seemingly does not come with any apps installed for you, that is because they are not shown as folders (the way your created apps will be shown), they are part of Django’s code. If you go to the
settings.py of the project folder, there is a list called INSTALLED_APPS where the installed apps are registered, you can see there are already some that are already registered. If you create a superuser and log in to the Django admin, you’ll also notice some already registered apps that are displayed in the panel. But we can create our own apps. Let’s learn how to create an app inside that Django project.
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, where
app_nameis 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,
pages. 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 name
- 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
staffpagesas opposed to
staff_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.
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.