Home » Cloud Technologies » Starting webserver using your first simple Django project

Starting webserver using your first simple Django project

As you are here, it means you are ready to get started with your first django project. This post is all about this. In this post, we will show you how to create first simple ( default ) django project and start it as a webserver.

$ mkdir django_container
$ cd django_container
$ python3 -m venv env

Note: You can set any directory for virtual environment, lets say we want to create environment in current directory, then you can use above command but if you want create environment in “/home/devlab/virtualenvs” directory, the above command will be “python3 -m venv /home/devlab/virtualenvs”

$ source env/bin/activate 
(env) $ pip install django

Now, we have all the setup done and we are ready to start our first django project, which can be started using simple command as below,

$ django-admin startproject my_django_project .

Notice: the dot ( . ) at the end of command. Above command, initialised everything you need for your first django project, as you can see below,

$ ls
env  manage.py  my_django_project
$ tree my_django_project/
my_django_project/
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

Now, we need to make sure our database is ready for the server, which can be intialised using “manage.py migrate” command as,

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

As we can see, now database “db.sqlite3” will be created with default tables.

$ ls
db.sqlite3  env  manage.py  my_django_project

Now, lets start the server,

$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
June 06, 2020 - 17:14:16
Django version 3.0.7, using settings 'my_django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

As we can see now, server is started with binding to your local loopback IP address and default port as,

Starting development server at http://127.0.0.1:8000/

Now, it we want to change the IP address and Port where you need to start the server, it can be done as,

$ python manage.py runserver YOUR_CUSTOM_IP_ADDRESS:YOUR_CUSTOM_PORT

here, you may get an error like “Solved: Django error Invalid HTTP_HOST header: ‘192.168.0.103:8080’. You may need to add ‘192.168.0.103’ to ALLOWED_HOSTS.” refer the post to change ALLOWED_HOSTS from your projects/settings.py

Finally, you are ready with your first django project and the webserver is up and running which you can verify by visiting http://127.0.0.1:8000/ and you should see the page as below,

django project

For writing the Django application, visit “Writing first Django Application”


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

3 thoughts on “Starting webserver using your first simple Django project”

Leave a Comment