3 min read
Django is one of the most popular open source full-stack web development Framework written in Python. Django is a framework for perfectionists with deadlines, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.
To know more about Django read: Django – Web Framework For Perfectionists
In this article, we will go over the steps for creating and running a Django Project.
A project is a collection of settings for an instance of Django including database configuration, Django-specific options, and application-specific settings.
Before creating a Django project make sure you have Python and Django installed in your machine if not go through How To Install Django
It is recommended to create projects in virtual environments though it's optional using them will save you from a lot of headaches in the future. Read the following article to know why virtual environments are so important: How To A Create Virtual Environment for Python
Note that, I am assuming that you have activated your virtual environment where your Django installation exists. Next, run the below command.
django-admin startproject mysite
This command will invoke the django-admin.py
script, which will set up a new Django project called mysite
. A Django project name can be composed of numbers, letters, or underscores. A project name cannot start with a number, it can only start with a letter or underscore in addition special characters and spaces aren't allowed anywhere in a project name.
You’ll now notice within your workspace there is a directory set to the name of your new project, mysite
. Inside the directory, there is another directory with the same name as the project and a python script named manage.py
Inside the second level project directory, there are another 4 Python scripts. So the default Django project can be illustrated as follows.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
__init__.py - a blank Python script whose presence indicates to the Python interpreter that the directory is a Python package. This file allows Python packages to be imported from the directories they are present.
settings.py - Contains the configuration settings for the Django project.
urls.py - Contains URL patterns for the Django project.
wsgi.py - Contains WSGI configuration properties for the Django project. Basically, Python script used to help run your development server and deploy your project to a production environment.
manage.py - a command-line utility that lets you interact with your Django project in many ways. This script is responsible for all project specific tasks.
Note that, there are two directories with the project name, this might seem confusing at first however you can always change the name of the outer directory which occasionally referred to as the Base Directory. The second level project directory is hardcoded in some of the Django specific files so better not mess with it.
Now that we have created a Django project we can see it in action in the browser. Django comes with a built-in web server for development. Navigate to the Base directory where manage.py
is, and run the below command.
python manage.py runserver
Ignore all the migration errors for the moment, Now open your preferred browser and go to http://127.0.0.1:8000/
If everything went right you should see the default the page of Django.
To stop the server press ctrl + c
in the terminal window. Running the application on a different port is also possible you just need to pass the port number along with the command.
# Run the development server on the local address and port 4345 (http://127.0.0.1:4345/)
python manage.py runserver 4345
# Run the dev server on the 96.126.104.88 address and port 80 (http://96.126.104.88/)
python manage.py runserver 96.126.104.88:80
DJANGO
Latest from djangocentral
2 min read
2 min read