Django is a free, open source web framework written in high-level Python, designed to help developers quickly create secure, scalable, and maintainable web applications. Installing Django Ubuntu can be done in two ways. It can be installed directly on the operating system or in a Python virtual environment using the pip command. The Django package is included in the official Ubuntu repositories and can be installed using the apt package manager. This is the easiest way to install Django on Ubuntu 18.04, but it is not as flexible as installing with pip. Also, the version included in the repos is always lagging behind the latest version of Django. The main purpose of Python virtual environments is to create multiple environments for different projects. This way, you can have several different Django environments on the same machine and install the required module version for each project without worrying about other Django installations being affected. If you install Django in the global environment, you can only install one version of the web framework on your computer. The content of the article: Installing Django on Ubuntu 18.04
Creating a Django project
Disable virtual environment
Conclusion Installing Django on Ubuntu 18.04 The following article provides step-by-step instructions for installing Django in a Python virtual environment on Ubuntu 18.04. Let’s first look at the Python 3 installation and the venv virtual environment. Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by typing: python3 -V The output should look like this: Python 3.6.6 Since Python 3.6, the recommended way to create a virtual environment is to use the venv module. To install the python3-venv package, run the following command: sudo apt install python3-venv After installing the module, we need to create a virtual environment for our Django application. Change to the directory where you want to store the Python 3 virtual environments. This can be your home directory or any directory where your user has read / write permissions. Create a new directory for your Django app and navigate to it: mkdir my_django_app cd my_django_app Once in the created directory, run the following command to create a new virtual environment: python3 -m venv venv The above command creates a directory named venv which contains a copy of the Python binary, Pip package manager, Python standard library, and other supporting files. You can use any name for the virtual environment. To start using this virtual environment, you need to activate it by running the activation script: source venv / bin / activate Upon activation, the bin directory of the virtual environment will be added to the beginning of the $ PATH variable. In addition, your shell prompt will change to reflect the name of the virtual environment you are currently using. In our case, this is venv. Now that the virtual environment is activated, you can use the pip package manager command to install Django: pip install django In a virtual environment, you can use pip instead of pip3 and python instead of python3. To verify that the installation was successful, use the following command, which will display the Django version: python -m django –version At the time of this writing, the last official version of Django is 2.1.2. Your Django version may differ from the version shown here.
Creating a Django project Use the django-admin command line utility to create a new Django project named mydjangoapp: django-admin startproject mydjangoapp This command creates a directory called mydjangoapp in your current directory. tree mydjangoapp / Inside this directory is the main project management script named manage.py and a directory that includes the database configuration, Django and application settings. Let’s move the database and create an administrator. Start by navigating to the mydjangoapp directory: cd mydjangoapp By default, Django uses a SQLite database. You can use other databases like PostgreSQL, MariaDB, Oracle or MySQL Database. Run the following command to transfer the database: python manage.py migrate After migrating the database, create a user with admin privileges so you can use the Django admin interface: python manage.py createsuperuser The command will ask you for a username, email address and password.
Testing Django Start the development web server with the manage.py script followed by the runserver option: python manage.py runserver You will see the following command results: If you have installed Django on a virtual machine and want to access the Django development server, you need to edit the settings.py file and add the server IP to the ALLOWED_H list