1 - Creation of the django project

If you are using Django for the first time, you will have to take into account the initial configuration. In particular, you will need to automatically generate code that establishes a Django project, i.e. a set of parameters for an instance of Django, including database configuration, Django-specific options, and application-specific parameters.
 From the cmd command , go to the directory where you want to store your code, then run the following command:

django-admin startproject mysite

This will create a mysite directory in your current directory. Here is an overview of the content of your site that you have just created with the django-admin startproject command:

  1. The mysite /: root directory designates the container for your project. It encompasses all of the content on your site.
  2. manage.py: contains code that allows you to interact with your Django project via the command line utility.
  3. The internal mysite /: directory is the current Python package for your project. Its name is the name of the Python package that you will need to use to import anything inside of it.
  4. mysite /__ init__.py: empty file telling Python that this directory should be considered as a Python package.
  5. mysite /settings.py: contains the configuration parameters for your Django project.
  6. mysite /urls.py: contains the url addresses of your project. Each new url must be declared here in this file.
  7. mysite /wsgi.py: an entry point for WSGI compatible web servers necessary for the implementation of your project.

 2 - Starting the Django server for your project

To verify that your Django project is working correctly, go to the external mysite directory of your application, and run the following command:

python manage.py runserver

You will see the following output on the command line:

Which tells you that the server is started at http://127.0.0.1:8000
If you want to access the home page of your server, just type the address shown above on the address bar of your browser:

3 - Change the server port 8000 

The server chooses port 8000 by default! But it sometimes happens that this port is occupied by other processes, and that you should change it, it is enough for this to add the port number of your choice just after the runserver command:

Example: changing the port to 5050

python manage.py runserver 5050

In this case the server will be accessible at the address: http://127.0.0.1:5050

Server shutdown command 

To stop the server, just type the combination key: Ctrl + C

Younes Derfoufi
my-courses.net

Leave a Reply