IntroductionDjango is the top Python web development framework and for good reason. It's powerful, flexible, and doesn't get in the way of developers. It also scales incredibly well, powering sites like Instagram.
Installing a Django development environment is very simple on Linux, and Debian makes it even simpler. There are two basic ways to go about it; the virtualenv way and the Debian way. The virtualenv way keeps django and all of the other Python packages installed with it contained, allowing for multiple installations with different packages and package versions. The Debian way uses
aptto install and manage Django.
The Virtualenv WayThe virtualenv route is the best way to go if you need multiple independent Django installs or you want to work with different versions of Python than the ones Debian uses by default.
Installing the PackagesTo stat off, you need to make sure that Python is installed and install the virtualenv package to compartmentalize your Python projects.
# apt install python python3 virtualenv
Setting Up VirtualenvAfter the packages finish installing, you can use virtualenv to create a new virtual environment for your Python project.
$ virtualenv -p python3 django-projectBy running the above command, you will prompt virtualenv to create a new virtual environment using the current version of Python 3 installed on the system. If you leave off the
-p python3part and virtualenv will use the current system default, which is the 2.7 line in Debian Stretch.
In order to use the new virtual environment
cdinto the folder and activate it.
$ cd django-project $ source bin/activateYou will see the name of the folder in parenthesis at the beginning of your prompt. When you're done using the virtual environment, you can exit by typing
The Debian WayDebian has its own way of doing things, and it usually works well. If you'd prefer a system-wide Django install, you can use the packaged version of Django available form Debian's repositories.
Installing the PackagesInstalling Django this way is very simple. Just install the packages.
# apt install python python3 python-django
Setting Up DjangoNow that you have your virtual environment set up and activated, you can install Django. Virtualenv automatically adds the Python package manager
pipto each environment it creates. Pip acts exactly like any other package manager and handles installs, removals, and updates. Because you are using virtualenv, the packages that pip manages are specific to that environment and that environment alone.
You can install Django through pip just by telling it to install.
$ pip install djangoPip will handle dependency management and pull in Django. Again, it only applies to that environment.
You can now use Django's built-in utilities to create your new project.
$ django-admin.py startproject newsiteDjango will create the base project files for you at the name you specify. You can now
cdinto your new Django project. Once there, you need to set up the database by applying the basic migrations and creating your user.
$ cd newsite $ python manage.py migrate $ python manage.py createsuperuserYou will be asked to enter the information for your site's admin user. Fill it out however you'd like. This information will be entered into the development database, so it doesn't matter too much, unless you plan to import it into your production one.
You can test your new Django project. Use
manage.pyone more time to start up the development server.
$ python manage.py runserverYou can view the static start page at
localhost:8000in your browser.