{"id":2606,"date":"2018-11-04T13:59:59","date_gmt":"2018-11-04T13:59:59","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=2606"},"modified":"2018-11-07T13:13:07","modified_gmt":"2018-11-07T13:13:07","slug":"how-to-install-django-on-centos-7","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/04\/how-to-install-django-on-centos-7\/","title":{"rendered":"How to Install Django on CentOS 7"},"content":{"rendered":"<p>Django is a free and open source high-level Python web framework designed to help developers build secure, scalable and maintainable web applications.<\/p>\n<p>There are different methods to install Django, depending on your needs. It can be installed system-wide or in a Python virtual environment using pip. Django packages are also included in the CentOS repositories and can be installed using the yum package manager but they are outdated.<\/p>\n<p>In this tutorial, we will be installing and configuring the latest stable version of Django on a CentOS 7 machine inside a Python virtual environment.<\/p>\n<p>The main purpose of Python virtual environments is to create an isolated environment for different Python projects. This way you can have multiple different Django environments on a single computer and install a specific version of a module on a per project basis without worrying that it will affect your other Django installations. If you install Django into the global environment then you can install only one Django version on your computer.<\/p>\n<h2>Installing Django on CentOS 7<\/h2>\n<p>The following sections provide a step by step instructions about how to install Django in a <a href=\"\/post\/how-to-install-python-3-on-centos-7\/\">Python virtual environment<\/a> on CentOS 7.<\/p>\n<h3>1. Installing Python 3<\/h3>\n<p>We\u2019ll be installing Python 3.6 from the Software Collections (SCL) repositories.<\/p>\n<p>CentOS 7 ships with Python 2.7.5 which is a critical part of the CentOS base system. SCL will allow you to install newer versions of python 3.x alongside the default python v2.7.5 so that system tools such as yum will continue to work properly.<\/p>\n<p>Start by enabling SCL by installing the CentOS SCL release file which is included in the CentOS extras repository:<\/p>\n<p>sudo yum install centos-release-scl<\/p>\n<p>Once the repository is enabled install Python 3.6 with the following command:<\/p>\n<p>sudo yum install rh-python36<\/p>\n<p>Once Python 3.6 is installed we are ready to create a virtual environment for our Django application.<\/p>\n<h3>2. Creating Virtual Environment<\/h3>\n<p>Starting from Python 3.6, the recommended way to create a virtual environment is to use the venv module.<\/p>\n<p>Navigate to the directory where you would like to store your Python 3 virtual environments. It can be your home directory or any other directory where your user has read and write permissions.<\/p>\n<p>Create a new directory for your Django application and cd into it:<\/p>\n<p>mkdir my_django_app<br \/>\ncd my_django_app<\/p>\n<p>To access Python 3.6 you need to launch a new shell instance using the scl tool:<\/p>\n<p>scl enable rh-python36 bash<\/p>\n<p>Run the following command to create a new virtual environment:<\/p>\n<p>The command above creates a directory called venv, which contains a copy of the Python binary, the <a href=\"\/post\/how-to-install-pip-on-centos-7\/\">Pip package manager<\/a>, the standard Python library and other supporting files. You can use any name you want for the virtual environment.<\/p>\n<p>To start using this virtual environment, you need to activate it by running the activate script:<\/p>\n<p>Once activated, the virtual environment\u2019s bin directory will be added at the beginning of the $PATH variable. Also your shell\u2019s prompt will change and it will show the name of the virtual environment you\u2019re currently using. In our case that is venv:<\/p>\n<h3>3. Installing Django<\/h3>\n<p>Now that the virtual environment is activated, you can use the Python package manager pip to install Django:<\/p>\n<p>Within the virtual environment, you can use the command pip instead of pip3 and python instead of python3.<\/p>\n<p>To verify the installation use the following command which will print the Django version:<\/p>\n<p>python -m django &#8211;version<\/p>\n<p>At the time of writing this article the latest official Django version is 2.1.2<\/p>\n<p>Your Django version may differ from the version shown here.<\/p>\n<h3>4. Creating a Django Project<\/h3>\n<p>To create a new django project named mydjangoapp use the django-admin command-line utility:<\/p>\n<p>django-admin startproject mydjangoapp<\/p>\n<p>The command above will create a mydjangoapp directory in your current directory.<\/p>\n<p>mydjangoapp\/<br \/>\n|&#8211; manage.py<br \/>\n`&#8211; mydjangoapp<br \/>\n|&#8211; __init__.py<br \/>\n|&#8211; settings.py<br \/>\n|&#8211; urls.py<br \/>\n`&#8211; wsgi.py<\/p>\n<p>Inside that directory you will find the main script for managing projects named manage.py and another directory including database configuration, and django and application-specific settings.<\/p>\n<p>Let\u2019s migrate the database and create an administrative user.<\/p>\n<p>Start by navigating to the mydjangoapp directory:<\/p>\n<p>By default Django uses SQLite database. For production applications, you can use <a href=\"\/post\/how-to-install-postgresql-on-centos-7\/\">PostgreSQL<\/a>, <a href=\"\/post\/install-mariadb-on-centos-7\/\">MariaDB<\/a>, Oracle or <a href=\"\/post\/install-mysql-on-centos-7\/\">MySQL<\/a> Database.<\/p>\n<p>Run the following command to migrate the database:<\/p>\n<p>The output will look something like the following:<\/p>\n<p>Operations to perform:<br \/>\nApply all migrations: admin, auth, contenttypes, sessions<br \/>\nRunning migrations:<br \/>\nApplying contenttypes.0001_initial&#8230; OK<br \/>\nApplying auth.0001_initial&#8230; OK<br \/>\nApplying admin.0001_initial&#8230; OK<br \/>\nApplying admin.0002_logentry_remove_auto_add&#8230; OK<br \/>\nApplying admin.0003_logentry_add_action_flag_choices&#8230; OK<br \/>\nApplying contenttypes.0002_remove_content_type_name&#8230; OK<br \/>\nApplying auth.0002_alter_permission_name_max_length&#8230; OK<br \/>\nApplying auth.0003_alter_user_email_max_length&#8230; OK<br \/>\nApplying auth.0004_alter_user_username_opts&#8230; OK<br \/>\nApplying auth.0005_alter_user_last_login_null&#8230; OK<br \/>\nApplying auth.0006_require_contenttypes_0002&#8230; OK<br \/>\nApplying auth.0007_alter_validators_add_error_messages&#8230; OK<br \/>\nApplying auth.0008_alter_user_username_max_length&#8230; OK<br \/>\nApplying auth.0009_alter_user_last_name_max_length&#8230; OK<br \/>\nApplying sessions.0001_initial&#8230; OK<\/p>\n<p>Once the database is migrated, create an administrative user so that you can use the Django admin interface:<\/p>\n<p>python manage.py createsuperuser<\/p>\n<p>The command will prompt you for a username, an email address, and a password for your administrative user.<\/p>\n<p>Username (leave blank to use &#8216;linuxize&#8217;): admin<br \/>\nEmail address: <a href=\"\/cdn-cgi\/l\/email-protection\">[email protected]<\/a><br \/>\nPassword:<br \/>\nPassword (again):<br \/>\nSuperuser created successfully.<\/p>\n<h3>5. Testing the Development Server<\/h3>\n<p>Start the development web server using the manage.py script followed by the runserver option:<\/p>\n<p>python manage.py runserver<\/p>\n<p>You\u2019ll see the following output:<\/p>\n<p>Performing system checks&#8230;<\/p>\n<p>System check identified no issues (0 silenced).<br \/>\nOctober 20, 2018 &#8211; 11:16:28<br \/>\nDjango version 2.1.2, using settings &#8216;mydjangoapp.settings&#8217;<br \/>\nStarting development server at http:\/\/127.0.0.1:8000\/<br \/>\nQuit the server with CONTROL-C.<\/p>\n<p>If you installed Django on a <a href=\"\/post\/how-to-install-virtualbox-on-centos-7\/\">virtual machine<\/a> and you want to access Django development server then you\u2019ll need to edit the settings.py file add the server IP address inside the ALLOWED_HOSTS list.<\/p>\n<p>Open http:\/\/127.0.0.1:8000 in your web browser and you will be presented with the default Django landing page:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/lxer.com\/post\/how-to-install-django-on-centos-7\/centos-django-landing-page.jpg\" alt=\"\" \/><\/p>\n<p>You can access the Django admin interface, by adding \/admin\/ to the end of the URL (http:\/\/127.0.0.1:8000\/admin\/). This will take you to the admin login screen:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/lxer.com\/post\/how-to-install-django-on-centos-7\/centos-django-login-page.jpg\" alt=\"\" \/><\/p>\n<p>Enter your username and password and you will be redirected to the Django admin page:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/lxer.com\/post\/how-to-install-django-on-centos-7\/centos-django-admin-page.jpg.jpg\" alt=\"\" \/><\/p>\n<p>To stop the development server type CTRL-C in your terminal.<\/p>\n<h3>6. Deactivating the Virtual Environment<\/h3>\n<p>Once you are done with your work, deactivate the environment, by typing deactivate and you will return to your normal shell.<\/p>\n<h2>Conclusion<\/h2>\n<p>You have learned how to create a Python virtual environment and install Django on your CentOS 7 machine. To create additional Django development environments repeat the steps we outlined in this tutorial.<\/p>\n<p>If you are new to Django, visit the <a href=\"https:\/\/docs.djangoproject.com\/en\/2.1\/intro\/\">Django documentation<\/a> page and learn how to develop your first Django app.<\/p>\n<p>If you are facing any problem, feel free to leave a comment.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=262439\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django is a free and open source high-level Python web framework designed to help developers build secure, scalable and maintainable web applications. There are different methods to install Django, depending on your needs. It can be installed system-wide or in a Python virtual environment using pip. Django packages are also included in the CentOS repositories &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/04\/how-to-install-django-on-centos-7\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Install Django on CentOS 7&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2606","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=2606"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2606\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2606\/revisions\/2736"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=2606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=2606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=2606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}