Django-admin & manage.py commands
When working on Django projects, developers have two choices for command line utility to perform tasks. They can use either django-admin or manage.py.
django-admin
Django admin is Django’s command line utility for administrative tasks. This utility is present in the scripts folder of the Django environment directory. The Django admin utility is executed from inside the terminal.
manage.py
Manage.py is a script that is the local version of Django admin and is located inside the project folder. It sets the Django settings module environment variable so that it points to your project settings.py file.
Django admin is a file that is created when you install Django.
WARNING
The Django admin utility should be on your system path, if you installed Django via pip, if you do not find it on your system path make sure you have your virtual environment activated, and install Django inside it using a command such as pip3 install Django.
Manage.py is a file that is automatically created each time you create a Django project, this file is specific to the virtual environment of the project. It does the same thing as Django admin but also sets the Django settings module environment variable to point to your project’s settings.py file.
Generally, when working on a single Django project, developers tend to use manage.py as the administrative tasks of Django admin can also be performed with manage.py.
However, if you need to switch between multiple Django settings files, use the Django admin command with the DJANGO_SETTINGS_MODULE or the settings command line option.
NOTE
It’s important to know that if only the project name is given, both the project directory and the project package will use the same name as the project name. Also, the project directory will be created in the current working directory. If an optional destination is provided, Django will use that existing directory as the project directory and create the manage.py file and the project package within it.
TIP
A common developer workflow is to use the period symbol to denote the current working directory, this avoids having sub-directories with the same name.
django-admin startproject my_site .
By default, the server runs on port 8000 on the IP address 127.0.0.1. However, it is possible to change this as you can pass in an IP address and port number explicitly.
NOTE
It is important to know that if you run this script as a user with normal privileges, which are recommended, you might not have access to start to port on a low port number. This is because low port numbers are reserved for the superuser or root user.
The development server works great for application development and testing. However, Django recommends that you should never use this server in production, as it has not gone through security audits or performance tests.
The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect.
However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.
App structures
A Django project is a web application that may consist of one or more sub-modules called apps.Â
In this reading, you’ll learn about the app structure, how to create an app and how to configure project settings to include the app.
What is a Django app?
As mentioned above, an app is responsible for performing one single task out of the many involved in the complete web application, represented by the Django project.Â
It implies that a project comprises many independent sub-applications, yet they may communicate among themselves.
For example, a trading organization website may have one app for managing customer data, another for suppliers, and another for stock management. However, the important feature of the Django app is that it is reusable.
Hence, a customer data management app in one project can be linked to the website project for another organization without modification.
For simplicity, you are going to uniformly use one app inside one project throughout the course, but it is good to know the utility of Django framework as a multi-app framework.
When a Django project is created with the startproject
command, it creates a container folder. Django puts a manage.py
script and the project package folder in the outer folder.
The startapp
command option of the manage.py
script creates a default folder structure for the app of that name.
Here’s how to create a demoapp
in the demoproject
folder.
(djenv) C:\djenv\demoproject>python manage.py startapp demoapp
A folder with the app’s name is created inside the parent folder. It has a few Python scripts. The folder structure looks like this:
C:\djenv\demoproject
│ db.sqlite3
│ manage.py
│
├───demoapp
│ │ admin.py
│ │ apps.py
│ │ models.py
│ │ tests.py
│ │ views.py
│ │ __init__.py
│ │
│ └───migrations
│ __init__.py
│
└───demoproject
│ asgi.py
│ settings.py
│ urls.py
│ wsgi.py
│ __init__.py
Let’s briefly explain the Python scripts created in the demoapp
folder.
views.py
In Django, a view is a user-defined function that’s called when Django’s URL dispatcher identifies the client’s request URL and matches it with a URL pattern defined in the urls.py file.
The auto-created views file is empty at the beginning.
Let’s add a view function called index() in it by saving the following snippet.
urls.py
The project package has a file of this name that defines the URL patterns for the project.Â
On similar lines, you need to provide the URL routing mechanism for the app.Â
One important thing to note here in contrast to the video you have seen earlier is that the urls.py
file can be configured at both the project and app level. In the example below, the urls.py will be configured at both the project and app-level.
The app folder doesn’t have a file of this name when created. Hence, you have to create one.
Save the following snippet as urls.py
in the demoapp folder.
Next, you need to update the urlpatterns
list in the project folder’s urls.py and include the app’s URL configurations.
The updated demoproject/urls.py
should look like this:
models.py
The data models required for processing in this app are created in this file. It is empty by default. A data model is a Python class based on django.db.modelsclass
. All the models present here are migrated to the database tables. For now, leave his file as it is without adding any models. You will learn how to work with models later.
tests.py
You’ll write the tests to be run on the app in this file. Let’s keep this file as it is without modifying it.
Update settings.py
Lastly, you need to update the list of INSTALLED_APPS in the project’s setting file. This list already contains some pre-installed apps. Add the name of demoapp
so that it looks like this:
After Performing the steps explained above, run the Django development server and visit localhost:8000/demo/ in the browser.
You have successfully created a Django app and added a view to it.
Note here that the files present in the app structure are not limited to this reading. The app directory as you will see also consists of other files such as forms.py, serializers.py among others.
In this reading, you learned to create an app, update the project settings to include it and add a view function.
Creating an App
A project represents the entire web application. An app acts like a sub-module of the project to provide specific functionality.
python -m django startapp myapp
Suppose you do not specify any destination, like in this example. In that case, the default destination of the current working directory is used.
I want to output some text to the homepage. To do this, I need to work with Django elements called routes and views.
When a user visits the applications homepage, I want to display some text. In Django, when a user navigates to a URL, the URL is routed to something called a view or controller.
For now you can just think of views as Python functions that generate the content that makes up the webpage. They can receive a request and return a response. Because this request and response will take place over the web, it’ll use two objects called HTTP request and HTTP response.
This function returns an HTTP response object containing the text Hello World.
It’s important to know that creating a function that returns an HTTP response will not do anything on its own. This is just the logic that will be executed when a user goes to the project’s homepage. To make this work, I need to map the view function to a URL. The URL you will use in your Django project are stored as a URL configuration in a file called URLs.py, which is located in the project directory. (AKA URL conf)
For now just know that it acts like a mapping chart the Django uses to determine which view functions are associated with a specific URL.
DjangoframeworkPythonstructure
Previous one → 2.Projects and Apps | Next one → 4.Web Frameworks and MVT