Setup Django with Python 3 in Ubuntu 18.x

1.Install Python3 and PIP

sudo apt-get install python3 python3-pip

Check Python version by typing python3 -V

Check PIP version by typing pip3 -V

2.Install Django

sudo apt install python3-django

Check the version of Django by typing django-admin --version

3.Create Sample Django Project

To test the newly installed Django server , we will create a demo webapp and run it. Change the directory to where you want to create the project. eg cd /var/www

Type django-admin startproject testsite

This will create a new directory called testsite and install the default Django files in it. If you do a cd testsite and list the contents, you will find another folder called testsite and files within it. Change the directory to the inner testsite and edit the settings.py file.

Change the ALLOWED_HOSTS line to something like ALLOWED_HOSTS = ["localhost"] and save the file

The value entered in ALLOWED_HOSTS is the domain url that you will call in the browser to load the site. So if you want to use the server IP address put ALLOWED_HOSTS=["x.x.x.x"]. Instead of localhost you can put 127.0.0.1 as well. but then you cannot use http://localhost to load the site. – only http://127.0.0.1

4.Migrate Site Database

Django uses SQLite as its internal database to manage websites. Go to the root folder of your site eg. cd /var/www/testsite and type python3 manage.py migrate

5.Create Admin User

Type python3 manage.py createsuperuser

Enter your username, email address and password

6.Launch The Project

Type python3 manage.py runserver 0.0.0.0:8000

The webapp is now running on port 8000. You can view it by opening http://localhost:8000 in your browser.

To view the admin site load http://localhost:8000/admin . Login using the admin credentials you created above.

7.Stop the Server

Pressing Ctrl-C in the terminal window where django is running will kill the Django server and stop the project

Be the first to comment

Leave a Reply

Your email address will not be published.


*