Home >>Django Tutorial >Django Project

Django Project

Django Project

In this topic, we will learn step by step process to create a Django application.

To create a Django project, we uses the following command:

$ django-admin startproject projectname 

( projectname is the name of Django application)

Django Project Example

Here, we are trying to create a Django project djangoapp in the current directory.

$ django-admin startproject djangoapp  

Locate into the Project

Now, we will move to the project by changing the directory.

The Directory can be changed by using the following command:

cd djangoapp  

To see all the files and the subfolders of the Django project we can use tree command that will show us the tree structure of the application.

The Project Structure

The outer directory folder is just a container for the application. We can also rename it further. The Django project itself contains the following packages and files.

  • manage.py: It is a command-line utility that permits us to interact with the project in various ways and also used to manage applications.
  • A directory located inside is that the actual application package name. Its name is the Python package name which we will need to use to import modules inside the application.
  • __init__.py: It is an empty file that tells Python that this directory should be considered as a Python package.
  • settings.py: This file is used to configure the application settings such as database connection, static files linking, etc.
  • urls.py: This file contains the listed URLs of the application. In this file, we will mention the URLs and corresponding actions to perform the task and display the view.
  • wsgi.py: It is an entry-point for WSGI-compatible webservers to serve the Django project.

Running the Django Project

Django project has a built-in development server which is used to run application instantly with none external web server. It means we do not need Apache or another webserver to run the application in development mode.

To run the application, we can use the following command.

$ python3 manage.py runserver  

Create an Application

In our main “myproject” folder, the same folder then manage.py −

$ python manage.py startapp myapp

We have just created “myapp” application and like the project, Django creates a “myapp” folder with the application structure −

myapp/
   __init__.py
   admin.py
   models.py
   tests.py
   views.py
  • __init__.py–This is just to make sure that python handles this folder as a package.
  • admin.py−This file helps us to 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

Now, we have our "myapp" application, we need to register it with our Django project "myproject". For this we have to update INSTALLED_APPS tuple in the settings.py file of our project −


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


No Sidebar ads