{"id":11820,"date":"2019-03-17T12:22:52","date_gmt":"2019-03-17T12:22:52","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11820"},"modified":"2019-03-17T12:22:52","modified_gmt":"2019-03-17T12:22:52","slug":"installing-and-configuring-django-web-framework-with-virtual-environments-in-centos-debian","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/installing-and-configuring-django-web-framework-with-virtual-environments-in-centos-debian\/","title":{"rendered":"Installing and Configuring Django Web Framework with Virtual Environments in CentOS\/Debian"},"content":{"rendered":"<p>Some 20 years ago when the\u00a0<strong>World Wide Web<\/strong>\u00a0was still in its infancy, having a personal or business web site was almost a rare luxury. With the subsequent development of several web technologies and the introduction of dynamic content provided by the combination of server-side programs and databases, companies could no longer be satisfied with having a static web site.<\/p>\n<div id=\"attachment_16221\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Setup-Django-Web-Framework-in-CentOS-Debian.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16221\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Setup-Django-Web-Framework-in-CentOS-Debian.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Setup-Django-Web-Framework-in-CentOS-Debian.png 720w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Setup-Django-Web-Framework-in-CentOS-Debian-620x297.png 620w\" alt=\"Setup Django Web Framework in CentOS Debian\" width=\"620\" height=\"297\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Install and Configure Django in CentOS and Debian \u2013 Part 1<\/p>\n<\/div>\n<p>Thus, web applications became a reality \u2013 programs in the full sense of the word running on top of a web server and accessible through a web browser.<\/p>\n<p>To make development easier and more effective, web frameworks were designed to aid programmers in their efforts to create applications. In short, a web framework takes care of common core functionalities in the development process such as dealing with user session management, interaction with databases, and the good practice of keeping business logic separate from display logic, to name a few examples.<\/p>\n<p>In this\u00a0<strong>3-article Django series<\/strong>, we will introduce you to\u00a0<strong>Django<\/strong>, a popular web framework based on Python. For that reason, at least a little familiarity with this programming language is suggested but if you have little to no experience with it, we\u2019ll also walk you through the basics.<\/p>\n<div id=\"exam_announcement\"><b>Part 1<\/b>:\u00a0<b>Installing and Configuring Django Web Framework with Virtual Environments in CentOS\/Debian<\/b><\/div>\n<div id=\"exam_announcement\"><b>Part 2<\/b>:\u00a0<a href=\"https:\/\/www.tecmint.com\/python-overview-and-creating-web-application-project-with-django\/\" target=\"_blank\" rel=\"noopener\">Reviewing Python Basics and Creating Your First Web Application with Django<\/a><\/div>\n<div id=\"exam_announcement\"><b>Part 3<\/b>:\u00a0<a href=\"https:\/\/www.tecmint.com\/create-mobile-friendly-web-applications-using-django\/\" target=\"_blank\" rel=\"noopener\">How to Create Mobile-Friendly Web Applications using Django Framework<\/a><\/div>\n<h3>Installing Django in CentOS and Debian Servers<\/h3>\n<p>Although you can install\u00a0<strong>Django<\/strong>\u00a0from both the Debian (<strong>v1.7.7<\/strong>: extended support will be discontinued on\u00a0<strong>December 2015<\/strong>) and Fedora EPEL (<strong>v1.6.11<\/strong>: extended support was discontinued on\u00a0<strong>April 2015<\/strong>) repositories, the available version is not the latest stable LTS (Long Term Support) release (<strong>v1.8.13<\/strong>, as of\u00a0<strong>May 2016<\/strong>).<\/p>\n<p>In this tutorial we will show you how to install\u00a0<strong>Django<\/strong>\u00a0<strong>v1.8.13<\/strong>\u00a0since its extended support is guaranteed until at least April of 2018.<\/p>\n<p>The recommended method to install\u00a0<strong>Django<\/strong>\u00a0is through\u00a0<strong>pip<\/strong>, a popular tool for managing Python packages. Also, in order to create isolated Python environments and avoid conflicts between projects that may require different versions of software dependencies, the use of virtual environments is highly encouraged.<\/p>\n<p>The tools that are used to create and manage virtual Python environments is called\u00a0<strong>virtualenv<\/strong>.<\/p>\n<p>Follow these steps to perform the installation:<\/p>\n<p><strong>1.<\/strong>\u00a0For\u00a0<strong>Fedora-based<\/strong>\u00a0distributions (except in Fedora itself), enable the\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-enable-epel-repository-for-rhel-centos-6-5\/\" target=\"_blank\" rel=\"noopener\">EPEL repository<\/a>\u00a0first:<\/p>\n<pre># yum update &amp;&amp; yum install epel-release\r\n<\/pre>\n<p><strong>2.<\/strong>\u00a0Install\u00a0<strong>pip<\/strong>\u00a0and\u00a0<strong>virtualenv<\/strong>:<\/p>\n<h5>Fedora-based distros:<\/h5>\n<pre># yum install python-pip python-virtualenv\r\nOR \r\n# dnf install python-pip python-virtualenv\r\n<\/pre>\n<h5>Debian and derivatives:<\/h5>\n<pre># aptitude update &amp;&amp; aptitude install python-pip virtualenv\r\n<\/pre>\n<p><strong>3.<\/strong>\u00a0Create a directory to store your initial project.<\/p>\n<pre># mkdir ~\/myfirstdjangoenv\r\n# cd ~\/myfirstdjangoenv\r\n<\/pre>\n<p><strong>4.<\/strong>\u00a0Create and activate a virtual environment:<\/p>\n<pre># virtualenv myfirstdjangoenv\r\n<\/pre>\n<p>The above command creates a bunch of files and subdirectories into\u00a0<strong>~\/myfirstdjangoenv<\/strong>\u00a0and basically installs a local copy of\u00a0<strong>Python<\/strong>\u00a0and\u00a0<strong>pip<\/strong>\u00a0within the current working directory. Next, we need to activate the virtual environment we just created:<\/p>\n<pre># source myfirstdjangoenv\/bin\/activate\r\n<\/pre>\n<p><strong>5.<\/strong>\u00a0Notice how the command prompt changes after the last command. It\u2019s now time to install Django:<\/p>\n<p><strong>Note<\/strong>\u00a0that the below screenshot was taken during a previous version of this tutorial, but the expected output is the same when installing\u00a0<strong>Django 1.8.13<\/strong>):<\/p>\n<pre># pip install Django==1.8.13\r\n<\/pre>\n<div id=\"attachment_16204\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Install-Django-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16204\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Install-Django-in-Linux-620x137.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Install-Django-in-Linux-620x137.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Install-Django-in-Linux.png 633w\" alt=\"Install Django in Linux\" width=\"620\" height=\"137\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Install Django in Linux: CentOS and Debian<\/p>\n<\/div>\n<p>You can check the\u00a0<strong>Django<\/strong>\u00a0version by launching a Python shell from your current working directory:<\/p>\n<pre># python\r\n&gt;&gt;&gt; import django\r\n&gt;&gt;&gt; print(django.get_version())\r\n<\/pre>\n<p>(Again, the above command should return\u00a0<strong>1.8.13<\/strong>\u00a0when checking the current Django version).<\/p>\n<p>To exit the Python prompt, type:<\/p>\n<pre>&gt;&gt;&gt; exit() \r\n<\/pre>\n<p>and press\u00a0<strong>Enter<\/strong>. Next, turn off the virtual environment:<\/p>\n<pre># deactivate\r\n<\/pre>\n<div id=\"attachment_16205\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Deactive-Django-Virtual-Environment.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16205\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Deactive-Django-Virtual-Environment.png\" alt=\"Deactive Django Virtual Environment\" width=\"572\" height=\"177\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Deactive Django Virtual Environment<\/p>\n<\/div>\n<p>Please note that while the virtual environment remains deactivated, Django is not available:<\/p>\n<div id=\"attachment_16206\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Error.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16206\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Error.png\" alt=\"Django Error\" width=\"566\" height=\"178\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django Error<\/p>\n<\/div>\n<h3>How to Create a First Project in Django<\/h3>\n<p>To create a project within the virtual environment we created earlier, it needs to be activated:<\/p>\n<pre># source myfirstdjangoenv\/bin\/activate\r\n<\/pre>\n<p>Next, the framework will create the entire directory structure to store your project. To do this, you will need to run.<\/p>\n<pre># django-admin startproject myfirstdjangoproject\r\n<\/pre>\n<p>The above command will create a directory named\u00a0<strong>myfirstdjangoproject<\/strong>\u00a0inside your current working directory.<\/p>\n<p>where you will find a file named\u00a0<strong>manage.py<\/strong>\u00a0(an utility that will help you manage your project later on) and another subdirectory (<strong>~\/myfirstdjangoenv\/myfirstdjangoproject\/myfirstdjangoproject<\/strong>). This last subdirectory will serve as the container for the project files.<\/p>\n<p>While the rest of the files will make real sense after we have reviewed some Python to start writing a real web application, it is worth and well to take note of the key files that will be found inside a project container directory:<\/p>\n<ol>\n<li><strong>myfirstdjangoproject\/__init__.py<\/strong>: This empty file tells Python that this directory should be considered a Python package.<\/li>\n<li><strong>myfirstdjangoproject\/settings.py<\/strong>: Specific settings for this Django project.<\/li>\n<li><strong>myfirstdjangoproject\/urls.py<\/strong>: a TOC (Table Of Contents) of your Django-powered site.<\/li>\n<li><strong>myfirstdjangoproject\/wsgi.py<\/strong>: An entry-point for WSGI-compatible web servers to serve your project.<\/li>\n<\/ol>\n<pre># ls \r\n# ls -l myfirstdjangoproject\r\n# ls -l myfirstdjangoproject\/myfirstdjangoproject\r\n<\/pre>\n<div id=\"attachment_16207\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16207\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Project-620x206.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Project-620x206.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Project.png 787w\" alt=\"Create Django Project\" width=\"620\" height=\"206\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Django Project<\/p>\n<\/div>\n<p>In addition,\u00a0<strong>Django<\/strong>\u00a0has a lightweight built-in web server (written in\u00a0<strong>Python<\/strong>\u00a0similar to\u00a0<a href=\"https:\/\/www.tecmint.com\/python-simplehttpserver-to-create-webserver-or-serve-files-instantly\/\" target=\"_blank\" rel=\"noopener\">Python SimpleHTTP<\/a>, what else?) that can be used to test your applications during the development process without having to deal with the task of setting a web server at this particular stage.<\/p>\n<p>However, you need to know that this is not suitable for a production environment \u2013 just for development. To launch your newly created project, change your current working directory to the container directory for your project (<strong>~\/myfirstdjangoenv\/myfirstdjangoproject<\/strong>) and run:<\/p>\n<pre># python manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<p>If you run into the following error:<\/p>\n<pre>You have unapplied migrations; your app may not work properly until they are applied.\r\nRun '<strong>python manage.py migrate<\/strong>' to apply them.\r\n<\/pre>\n<p>Do what it says:<\/p>\n<pre># python manage.py migrate\r\n<\/pre>\n<p>and then start the server again:<\/p>\n<pre># python manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<p>We will cover the concept of migrations in the next articles of this series, so you can disregard the error message for the time being.<\/p>\n<p>In any event, you can change the default port where the built-in web server will be listening. By using\u00a0<strong>0.0.0.0<\/strong>\u00a0as the network interface to listen on, we allow other computers in the same network to access the project user interface (if you use\u00a0<strong>127.0.0.1<\/strong>\u00a0instead, you will only be able to access the UI from localhost).<\/p>\n<p>You can also change the port to another one of your choosing, but you\u2019ll also need to make sure that traffic through such port is allowed through your firewall:<\/p>\n<pre># firewall-cmd --add-port=8000\/tcp\r\n# firewall-cmd --permanent --add-port=8000\/tcp\r\n<\/pre>\n<p>Of course, it goes without saying that you will need to update the allowed port if you choose to use a different one while launching the lightweight web server.<\/p>\n<p>You should see the following output in your terminal:<\/p>\n<pre># python manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<div id=\"attachment_16208\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Start-Django-HTTP-Server.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16208\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Start-Django-HTTP-Server-620x143.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Start-Django-HTTP-Server-620x143.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Start-Django-HTTP-Server.png 886w\" alt=\"Start Django HTTP Server\" width=\"620\" height=\"143\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Start Django HTTP Server<\/p>\n<\/div>\n<p>At this point, you can open your favorite web browser and navigate to the IP address of the machine where you installed Django followed by the port number. In my case, it is a\u00a0<strong>Debian Jessie<\/strong>\u00a0box with IP\u00a0<strong>192.168.0.25<\/strong>\u00a0and listening on port\u00a0<strong>8000<\/strong>:<\/p>\n<pre>http:\/\/192.168.0.25:8000\r\n<\/pre>\n<div id=\"attachment_16209\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-HTTP-Server.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16209\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-HTTP-Server-620x167.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-HTTP-Server-620x167.png 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-HTTP-Server.png 922w\" alt=\"Django HTTP Server\" width=\"620\" height=\"167\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django HTTP Server<\/p>\n<\/div>\n<p>While it\u2019s a great thing that we were able to complete the initial setup of a project, there is still much work left to do, as indicated in the message above.<\/p>\n<h3>Summary<\/h3>\n<p>In this guide we have explained how to install and configure a virtual environment for\u00a0<strong>Django<\/strong>, a versatile open source web framework based on Python.<\/p>\n<p>Regardless of whether you are an application developer or a system administrator, you will want to bookmark this article and the rest of this series because chances are that at some point or another you will need to consider the need of such tool for your daily tasks.<\/p>\n<p>In the following articles of this series we will discuss how to build on what we have already accomplished to create a simple, yet functional, web application using Django and Python.<\/p>\n<p>As we briefly commented on the last article of this series,\u00a0<strong>Django<\/strong>\u00a0is a free and open source web framework that turns application development into a faster task done in a more effective way \u2013 from the programmer\u2019s point of view.<\/p>\n<p><i class=\"fa fa-check\"><\/i>\u00a0<a href=\"https:\/\/www.tecmint.com\/install-and-configure-django-web-framework-in-centos-debian-ubuntu\/\" target=\"_blank\" rel=\"noopener\">Installing and Configuring Django Web Framework with Virtual Environments \u2013 Part 1<\/a><\/p>\n<div id=\"attachment_16370\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Applications-Using-Django.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16370\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Applications-Using-Django.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Applications-Using-Django.png 720w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Applications-Using-Django-620x297.png 620w\" alt=\"Create Web Applications Using Django\" width=\"620\" height=\"297\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Web Applications Using Django \u2013 Part 2<\/p>\n<\/div>\n<p>To do so, Django follows the\u00a0<strong>MVC<\/strong>\u00a0(<strong>Model<\/strong>\u00a0\u2013\u00a0<strong>View<\/strong>\u00a0\u2013\u00a0<strong>Controller<\/strong>) design pattern, or\u00a0<a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/faq\/general\/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names\" target=\"_blank\" rel=\"nofollow noopener\">as their FAQs state<\/a>, it can better be described as a\u00a0<strong>MTV<\/strong>\u00a0(<strong>Model<\/strong>\u00a0\u2013\u00a0<strong>Template<\/strong>\u00a0\u2013\u00a0<strong>View<\/strong>) framework.<\/p>\n<p>In Django, a \u201c<strong>view<\/strong>\u201d describes which data is presented to the user, whereas a\u00a0<strong>template<\/strong>\u00a0describes how the data is presented. Finally, the\u00a0<strong>model<\/strong>\u00a0is the source of information about data in the application.<\/p>\n<p>In this article we will review some Python basics and explain how to prepare your environment to create a simple web application in the next tutorial.<\/p>\n<h3>Learn Some Python Basics<\/h3>\n<p>As an object-oriented programming language, Python organizes things into a collection of objects with\u00a0<strong>properties<\/strong>\u00a0(also known as\u00a0<strong>attributes<\/strong>) and\u00a0<strong>methods<\/strong>\u00a0(also known as\u00a0<strong>actions<\/strong>). This allows us to define an object once and then to create multiple instances of such objects with the same structure of properties and methods without having to write everything from scratch every time. Objects are thus defined by\u00a0<strong>classes<\/strong>\u00a0that represent them.<\/p>\n<p>For example, a\u00a0<strong>Person<\/strong>\u00a0object could be defined as follows:<\/p>\n<h5>Properties:<\/h5>\n<ol>\n<li>Person.height<\/li>\n<li>Person.weight<\/li>\n<li>Person.age<\/li>\n<li>Person.ethniticity<\/li>\n<\/ol>\n<h5>Methods:<\/h5>\n<ol>\n<li>Person.eat()<\/li>\n<li>Person.sleep()<\/li>\n<li>Person.walk()<\/li>\n<\/ol>\n<p>As in most programming languages, a\u00a0<strong>property<\/strong>\u00a0is defined by the object\u2019s name followed by a\u00a0<strong>dot<\/strong>\u00a0and the attribute\u2019s name, whereas a\u00a0<strong>method<\/strong>\u00a0is indicated in the same fashion but also followed by a pair of parentheses (which may be empty or not \u2013 in the latter case, it may contain a variable upon whose value the method will act, such as\u00a0<strong>Person.eat(cake)<\/strong>\u00a0or\u00a0<strong>Person.sleep(now)<\/strong>, to name a few examples).<\/p>\n<p>To define methods in Python, you will use the\u00a0<strong>def<\/strong>\u00a0keyword, followed by the method\u2019s name and a set of parentheses, with an optional object as you will see in a minute.<\/p>\n<p>All of this will become much clearer during the next section where we will dive into a real example.<\/p>\n<h3>Creating the structure of a web application<\/h3>\n<p>As you may recall from\u00a0<a href=\"https:\/\/www.tecmint.com\/install-and-configure-django-web-framework-in-centos-debian-ubuntu\/\" target=\"_blank\" rel=\"noopener\">Part 1 of this Django series<\/a>, we said that a web application requires a database to store data. When you create an app, Django automatically sets up a\u00a0<strong>Sqlite<\/strong>\u00a0database that works just fine for small to middle size applications, and is what we will use in this case to store data for a classic first-time web app: a blog.<\/p>\n<p>To start a new application inside of a project (by the way, you can think of a project as a collection of web applications), run the following command after activating the virtual environment we set up in\u00a0<strong>Part 1<\/strong>\u00a0of this series.<\/p>\n<pre># cd ~\/myfirstdjangoenv\/\r\n# source myfirstdjangoenv\/bin\/activate\r\n# cd ~\/myfirstdjangoenv\/myfirstdjangoproject\r\n# python manage.py startapp myblog\r\n<\/pre>\n<div id=\"attachment_16362\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Application-Project-in-Django.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16362\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Application-Project-in-Django.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Application-Project-in-Django.png 838w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Web-Application-Project-in-Django-620x192.png 620w\" alt=\"Create Web Application Project in Django\" width=\"620\" height=\"192\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Web Application Project in Django<\/p>\n<\/div>\n<p><strong>Note<\/strong>\u00a0that you can change the app\u2019s name\u00a0<strong>(myblog)<\/strong>\u00a0for a name of your choosing \u2013 this is only an identifier for the application (please note that all management tasks are invoked using the\u00a0<code>manage.py<\/code>\u00a0script via the python binary \u2013 feel free to explore its source code if you have a minute):<\/p>\n<p>Now let\u2019s go inside the inner\u00a0<strong>myfirstdjangoproject<\/strong>\u00a0directory and find the file\u00a0<code>settings.py<\/code>, where we will tell Django to use myblog as an application:<\/p>\n<pre># cd ~\/myfirstdjangoenv\/myfirstdjangoproject\/myfirstdjangoproject\r\n<\/pre>\n<div id=\"attachment_16363\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/My-Django-Web-Project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16363\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/My-Django-Web-Project.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/My-Django-Web-Project.png 770w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/My-Django-Web-Project-620x86.png 620w\" alt=\"My Django Web Project\" width=\"620\" height=\"86\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">My Django Web Project<\/p>\n<\/div>\n<p>Look for the\u00a0<strong>INSTALLED_APPS<\/strong>\u00a0section and add\u00a0<strong>myblog<\/strong>\u00a0inside single quotes as shown below:<\/p>\n<pre>INSTALLED_APPS = (\r\n    'django.contrib.admin',\r\n    'django.contrib.auth',\r\n    'django.contrib.contenttypes',\r\n    'django.contrib.sessions',\r\n    'django.contrib.messages',\r\n    'django.contrib.staticfiles',\r\n    '<strong>myblog<\/strong>'\r\n)\r\n<\/pre>\n<p>(By the way, the lines beginning with\u00a0<strong>django<\/strong>\u00a0above represent other Django applications that are activated in the current project automatically when it is first created and are supposed to aid the developer in writing code related to administration, authentication, content type declarations, and so on, in his\u00a0<strong>\/<\/strong>\u00a0her application).<\/p>\n<p>Thus,\u00a0<strong>myblog<\/strong>\u00a0will become activated, along with the other built-in applications, in this Django instance.<\/p>\n<h3>Understanding about Django models<\/h3>\n<p>Up to this point, you have the basic directory structure of a web application but we still need to create the models (each model will be a separate class that represents an object in our application).<\/p>\n<p>Another thing that you need to know about Django models is that every attribute represents a database field where the corresponding value will be stored.<\/p>\n<p>Now, return to the\u00a0<strong>myblog<\/strong>\u00a0directory and edit the file\u00a0<code>models.py<\/code>, where we will define the objects for our application:<\/p>\n<pre># cd \/myfirstdjangoenv\/myfirstdjangoproject\/myblog\r\n<\/pre>\n<div id=\"attachment_16364\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Configure-Django-Models.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16364\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Configure-Django-Models.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Configure-Django-Models.png 663w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Configure-Django-Models-620x88.png 620w\" alt=\"Configure Django Models\" width=\"620\" height=\"88\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Configure Django Models<\/p>\n<\/div>\n<p>It is\u00a0<strong>VERY<\/strong>\u00a0important that you pay close attention to indentation in\u00a0<code>.py<\/code>\u00a0files. Since this is Python, the models will not work if the indentation is not set evenly and properly. Please refer to the comments below for further clarification on what we\u2019re doing:<\/p>\n<ol>\n<li>Lines starting with the\u00a0<strong>from<\/strong>\u00a0keyword indicate that we are importing modules (or predefined objects) from an existing file or library (<strong>django.db<\/strong>\u00a0and\u00a0<strong>django.utils<\/strong>, in this case, are located inside\u00a0<strong>\/root\/myfirstdjangoenv\/myfirstdjangoenv\/lib\/python2.7\/site-packages\/django\/db<\/strong>\u00a0and\u00a0<strong>utils<\/strong>, respectively.<\/li>\n<li>Each model is defined through a\u00a0<strong>class<\/strong>\u00a0that derives from\u00a0<strong>django.db.models.Model<\/strong>, which tells Django that it should be saved in the database.<\/li>\n<li>Following the class declaration, you will find the properties of each\u00a0<strong>Post<\/strong>\u00a0object and then the\u00a0<strong>def<\/strong>\u00a0keyword that defines the methods for the object.<\/li>\n<li>The\u00a0<strong>blank=False<\/strong>\u00a0and\u00a0<strong>null=False<\/strong>\u00a0directives in a field declaration indicate that such fields are required.<\/li>\n<li><strong>ForeignKey<\/strong>\u00a0indicates that the author of a post must have been created previously using the\u00a0<strong>auth.User<\/strong>module (we will show you how to do that in the next article of this series).<\/li>\n<li>Finally, the\u00a0<strong>self<\/strong>\u00a0keyword refers to the current object and represents a shortcut to accessing its properties and methods.<\/li>\n<\/ol>\n<p>You can read more about model field declarations in the\u00a0<a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/ref\/models\/fields\/\" target=\"_blank\" rel=\"nofollow noopener\">docs<\/a>.<\/p>\n<p>So here\u2019s our\u00a0<code>models.py<\/code>:<\/p>\n<pre>from django.db import models\r\nfrom django.utils import timezone\r\n# Create your models here.\r\nclass Post(models.Model):\r\n        author = models.ForeignKey('auth.User')\r\n        title = models.CharField(max_length=200, blank=False, null=False)\r\n        text = models.TextField(blank=False, null=False)\r\n        whenCreated = models.DateTimeField(default=timezone.now)\r\n        whenPublished = models.DateTimeField(blank=True, null=True)\r\n\r\n        def publish(self):\r\n                self.whenPublished = timezone.now()\r\n                self.save()\r\n\r\n        def __str__(self):\r\n                return self.title\r\n<\/pre>\n<h3>Creating Django Database and Admin User<\/h3>\n<p>As we mentioned earlier, the\u00a0<strong>Post<\/strong>\u00a0model must be migrated to the database in order to store the data associated with each instance of it. If you make changes to your model later, you will need to repeat this step:<\/p>\n<pre># cd ~\/myfirstdjangoenv\/myfirstdjangoproject\r\n# python manage.py makemigrations myblog\r\n# python manage.py migrate myblog\r\n<\/pre>\n<div id=\"attachment_16365\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16365\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Database.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Database.png 883w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Database-620x163.png 620w\" alt=\"Create Django Database\" width=\"620\" height=\"163\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Django Database<\/p>\n<\/div>\n<p>To create an administrative user for your application (this will allow us to manage our application using a web interface), do:<\/p>\n<pre># python manage.py createsuperuser\r\n<\/pre>\n<p>and enter the required information when prompted to do so:<\/p>\n<div id=\"attachment_16366\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Web-Admin-User.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16366\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Web-Admin-User.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Web-Admin-User.png 834w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Django-Web-Admin-User-620x96.png 620w\" alt=\"Create Django Web Admin User\" width=\"620\" height=\"96\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Django Web Admin User<\/p>\n<\/div>\n<p>However, in order for our application to be managed through the admin interface, it needs to be registered in\u00a0<strong>~\/myfirstdjangoenv\/myfirstdjangoproject\/myblog\/admin.py<\/strong>:<\/p>\n<pre>from django.contrib import admin\r\nfrom .models import Post\r\n# Register your models here.\r\nadmin.site.register(Post)\r\n<\/pre>\n<p>Now go ahead and start the server:<\/p>\n<pre># cd ~\/myfirstdjangoenv\/myfirstdjangoproject\r\n# python manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<p>and point your web browser to\u00a0<code>192.168.0.25:8000\/admin<\/code>. Enter the user credentials created earlier to log on:<\/p>\n<div id=\"attachment_16367\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Web-Administration.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16367\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Web-Administration.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Web-Administration.png 825w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Web-Administration-620x303.png 620w\" alt=\"Django Web Administration\" width=\"620\" height=\"303\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django Web Administration<\/p>\n<\/div>\n<p>If you followed the steps outlined above, you will be taken to the Django admin interface. Pay particular attention to the\u00a0<strong>Users<\/strong>\u00a0module, which we will use to create the objects required to define the post authors in our blog:<\/p>\n<div id=\"attachment_16368\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-User-Module.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16368\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-User-Module.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-User-Module.png 648w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-User-Module-620x259.png 620w\" alt=\"Django User Module\" width=\"620\" height=\"259\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django User Module<\/p>\n<\/div>\n<p>We will use this administration area to create objects of type\u00a0<strong>User<\/strong>\u00a0and\u00a0<strong>Post<\/strong>\u00a0in the next article. By now, you can stop the web server by pressing\u00a0<strong>Ctrl + C<\/strong>\u00a0in the same terminal window where you started it.<\/p>\n<h3>Important Points to remember<\/h3>\n<p>Through the use of models, we can create objects for our application and migrate them easily to the underlying database. Thanks to the fact that Django takes care of creating the directory structure and the files that require minimum editing we can focus in the programming side of things, which translates into time savings and equates to a happier developer.<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial we have discussed some basic concepts of object-oriented programming in Python and explained how to create the directory structure for our web application using Django.<\/p>\n<p>In the next (and final) guide of this series, we will make use of the\u00a0<strong>Post<\/strong>\u00a0model to create objects and work on the frontend of our application to display the posts in our blog in a mobile-friendly format.<\/p>\n<p>In\u00a0<strong>Part 1<\/strong>\u00a0of this series you learned how to install and configure Django in a virtual environment and you created the skeleton of your first project.<\/p>\n<p>Then in\u00a0<strong>Part 2<\/strong>\u00a0we created an application and a model for\u00a0<strong>Post<\/strong>\u00a0objects, which we later migrated to the database. Finally, we showed you how to integrate your newly created application to the Django administration user interface.<\/p>\n<p><strong>These articles are part of Django series:<\/strong><\/p>\n<p><i class=\"fa fa-check\"><\/i>\u00a0<a href=\"https:\/\/www.tecmint.com\/install-and-configure-django-web-framework-in-centos-debian-ubuntu\/\" target=\"_blank\" rel=\"noopener\">Installing and Configuring Django Web Framework with Virtual Environments \u2013 Part 1<\/a><\/p>\n<p><i class=\"fa fa-check\"><\/i>\u00a0<a href=\"https:\/\/www.tecmint.com\/python-overview-and-creating-web-application-project-with-django\/\" target=\"_blank\" rel=\"noopener\">Reviewing Python Basics and Creating Your First Web Application with Django \u2013 Part 2<\/a><\/p>\n<div id=\"attachment_16392\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Creating-Mobile-Friendly-Web-Application-with-Django.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16392\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Creating-Mobile-Friendly-Web-Application-with-Django.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Creating-Mobile-Friendly-Web-Application-with-Django.png 720w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Creating-Mobile-Friendly-Web-Application-with-Django-620x297.png 620w\" alt=\"Creating Mobile Friendly Web Application with Django\" width=\"620\" height=\"297\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Creating Mobile Friendly Web Application with Django \u2013 Part 3<\/p>\n<\/div>\n<p>In this final guide we will discuss how to access the application using the UI and how to make it mobile-friendly for all kind of devices. That said, let\u2019s get started.<\/p>\n<h3>Creating objects via the Django admin interface<\/h3>\n<p>To create objects of type\u00a0<strong>Post<\/strong>\u00a0(remember that is the model we defined in\u00a0<strong>Part 2<\/strong>\u00a0of this series), we will use the Django admin interface.<\/p>\n<p>Make sure the Django built-in web server is running on port\u00a0<strong>8000<\/strong>\u00a0(or another one of your choosing) by running the following command from the outer\u00a0<strong>myfirstdjangoproject<\/strong>\u00a0directory:<\/p>\n<pre># cd ~\/myfirstdjangoenv\/myfirstdjangoproject\r\n# python manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<p>Now open your web browser and point to\u00a0<code>http:\/\/ip-address:8000\/admin<\/code>, then log on using the credentials you set up in the previous article and start writing a post (which, again, will create an object of type\u00a0<strong>Post<\/strong>\u00a0and insert the associated data into the underlying database):<\/p>\n<div id=\"attachment_16381\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Administartion.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16381\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Administartion.png\" alt=\"Django Administration\" width=\"518\" height=\"243\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django Administration<\/p>\n<\/div>\n<p>Repeat the process 2 or 3 times:<\/p>\n<div id=\"attachment_16382\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Object-in-Django.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-16382\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Create-Object-in-Django.png\" alt=\"Create Object in Django\" width=\"541\" height=\"430\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Create Object in Django<\/p>\n<\/div>\n<p>After we have created a couple of posts, let\u2019s see what we need to do in order to display them using our web browser.<\/p>\n<h3>Our initial view<\/h3>\n<p>Our first view (<strong>~\/myfirstdjangoenv\/myfirstdjangoproject\/myblog\/views.py<\/strong>) will be in charge of filtering all\u00a0<strong>Post<\/strong>objects and returning those where the value of\u00a0<strong>whenPublished<\/strong>\u00a0is less than or equal to the current date and time (<strong>whenPublished__lte=timezone.now())<\/strong>\u00a0ordered by descending\u00a0<strong>whenPublished<\/strong>, which is the same as saying \u201c<strong>latest first<\/strong>\u201c.<\/p>\n<p>These objects are saved into a variable conveniently named posts, and are returned (identified as\u00a0<strong>allposts<\/strong>) to be embedded in the HTML, as we will see in the next section:<\/p>\n<pre>from django.shortcuts import render\r\nfrom .models import Post\r\nfrom django.utils import timezone\r\ndef posts(request):\r\n        posts = Post.objects.filter(whenPublished__lte=timezone.now()).order_by('-whenPublished')\r\n        return render(request, 'myblog\/posts.html', {'allposts': posts})\r\n<\/pre>\n<p>Finally, the double underscore in\u00a0<code>whenPublished__lte<\/code>\u00a0above is used to separate a database field\u00a0<strong>(whenPublished)<\/strong>\u00a0from a filter or an operation (<strong>lte = less<\/strong>\u00a0than or equal).<\/p>\n<p>Once we have defined our initial view, let\u2019s work on the associated template.<\/p>\n<h3>Create Template for our first Project<\/h3>\n<p>Following the directives and paths given in the previous section, we will store our initial template inside\u00a0<strong>myblog\/templates\/myblog<\/strong>. This means you will need to create a directory named\u00a0<strong>templates<\/strong>\u00a0and a subdirectory called\u00a0<strong>myblog<\/strong>:<\/p>\n<pre># cd ~\/myfirstdjangoenv\/myfirstdjangoproject\/myblog\r\n# mkdir -p templates\/myblog\r\n<\/pre>\n<p>We will call the template\u00a0<code>posts.html<\/code>\u00a0and insert the following code in it. You will notice that we are adding online references to\u00a0<strong>jQuery<\/strong>,\u00a0<a href=\"https:\/\/www.tecmint.com\/create-mobile-friendly-responsive-web-application-using-jquery\/\" target=\"_blank\" rel=\"noopener\">Bootstrap<\/a>, FontAwesome, and Google fonts.<\/p>\n<p>In addition, we have enclosed Python code inside curly brackets inside the HTML. Please note that for every object of type Post we will show its title, its published date and author, and finally its text. Finally, in red you will see that we make a reference to the objects returned via\u00a0<strong>myblog\/views.py<\/strong>:<\/p>\n<p>Ok, here\u2019s the\u00a0<strong>posts.html<\/strong>\u00a0file:<\/p>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=\"utf-8\"\/&gt;\r\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.5\/css\/bootstrap.min.css\"&gt;\r\n&lt;link href='https:\/\/fonts.googleapis.com\/css?family=Indie+Flower' rel='stylesheet' type='text\/css'&gt;\r\n&lt;link href='https:\/\/fonts.googleapis.com\/css?family=Pacifico' rel='stylesheet' type='text\/css'&gt;\r\n&lt;link href=\"https:\/\/maxcdn.bootstrapcdn.com\/font-awesome\/4.4.0\/css\/font-awesome.min.css\" rel=\"stylesheet\" type='text\/css'\"&gt;\r\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-2.1.4.min.js\"&gt;\r\n&lt;\/script&gt;\r\n    &lt;style&gt;\r\n      .title {\r\n        font-family: 'Indie Flower', serif;\r\n        font-size: 30px;\r\n        color: #1E90FF;\r\n      }\r\n      h1 {\r\n        font-family: 'Pacifico', serif;\r\n        font-size: 45px;\r\n        color: #1E90FF;\r\n      }\r\n    &lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"container\"&gt;&lt;h1&gt;My blog&lt;\/h1&gt;&lt;br&gt;\r\n{% for post in allposts %}\r\n    &lt;div&gt;\r\n        &lt;div class=\"title\"&gt;{{ post.title }}&lt;\/div&gt;\r\n        &lt;strong&gt;Published on {{ post.whenPublished }} by {{ post.author }}.&lt;\/strong&gt;\r\n        &lt;p&gt;{{ post.text|linebreaks }}&lt;\/p&gt;\r\n    &lt;\/div&gt;\r\n{% endfor %}\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>In the above\u00a0<strong>template<\/strong>, the\u00a0<a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/ref\/templates\/builtins\/#linebreaks\" target=\"_blank\" rel=\"noopener\">linebreaks<\/a>\u00a0filter is used to replace line breaks in plain text with the corresponding HTML equivalent (<code>&lt;br \/&gt;<\/code>\u00a0or\u00a0<code>&lt;\/p&gt;<\/code>) to format each post properly with paragraph separation.<\/p>\n<p>Next, we need to set up a mapping between URLs in our application and the corresponding views that return the data. To do so, create a file named\u00a0<strong>urls.py<\/strong>\u00a0inside\u00a0<strong>myblog<\/strong>\u00a0with the following content:<\/p>\n<pre>from django.conf.urls import url\r\nfrom . import views\r\nurlpatterns = [\r\n    url(r'^$', views.posts, name='posts'),\r\n]\r\n<\/pre>\n<p>The\u00a0<code>r'^$'<\/code>\u00a0deserves a little more explanation. The leading\u00a0<code>r<\/code>\u00a0instructs Django to treat the string inside single quotes as a regular expression.<\/p>\n<p>In particular,\u00a0<code>r'^$'<\/code>\u00a0represents an empty string so that when we point our browser to\u00a0<code>http:\/\/ip-address:8000<\/code>\u00a0(and nothing else), the data returned by the variable\u00a0<strong>posts<\/strong>\u00a0inside\u00a0<code>views.py<\/code>\u00a0(refer to the previous section) will be presented in our home page:<\/p>\n<p>Last, but not least, we will include the\u00a0<strong>urls.py<\/strong>\u00a0file of our blog application (<strong>~\/myfirstdjangoenv\/myfirstdjangoproject\/myblog\/urls.py<\/strong>) into the\u00a0<strong>urls.py<\/strong>\u00a0of our main project (<strong>~\/myfirstdjangoenv\/myfirstdjangoproject\/myfirstdjangoproject\/urls.py<\/strong>):<\/p>\n<pre>from django.conf.urls import include, url\r\nfrom django.contrib import admin\r\n\r\nurlpatterns = [\r\n    url(r'^admin\/', include(admin.site.urls)),\r\n    url(r'', include('myblog.urls')),\r\n]\r\n<\/pre>\n<p>Then let\u2019s start the web server:<\/p>\n<pre># cd ~\/myfirstdjangoenv\/myfirstdjangoproject\r\n# python manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<p>We should now be able to see the lists of posts we created earlier:<\/p>\n<div id=\"attachment_16383\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-My-Django-Web-Project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16383\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-My-Django-Web-Project.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-My-Django-Web-Project.png 825w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-My-Django-Web-Project-620x338.png 620w\" alt=\"Check My Django Web Project\" width=\"620\" height=\"338\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check My Django Web Project<\/p>\n<\/div>\n<p>Thanks to\u00a0<strong>Bootstrap<\/strong>, you can still have an excellent visualization in a smaller device:<\/p>\n<div id=\"attachment_16384\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-Site-Mobile-Responsive.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16384\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-Site-Mobile-Responsive.png\" sizes=\"auto, (max-width: 239px) 100vw, 239px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-Site-Mobile-Responsive.png 340w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Check-Site-Mobile-Responsive-239x450.png 239w\" alt=\"Mobile Responsive Look of My Project\" width=\"239\" height=\"450\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Mobile Responsive Look of My Project<\/p>\n<\/div>\n<h3>Summing Up<\/h3>\n<p>Let\u2019s now review the concepts that we have covered in this article and throughout this series:<\/p>\n<p><strong>1.<\/strong>\u00a0Each\u00a0<strong>model<\/strong>\u00a0defines an object and maps to a database table, whose fields in turn map to the properties of that object. On the other hand, a\u00a0<strong>template<\/strong>\u00a0defines the user interface where the data returned by the view will be displayed.<\/p>\n<p>Let\u2019s say we want to modify our model by adding a field named\u00a0<strong>summary<\/strong>\u00a0to the\u00a0<strong>Post<\/strong>\u00a0object, where we will store an optional brief description of each post. Let\u2019s add the following line in\u00a0<strong>myblog\/models.py<\/strong>:<\/p>\n<pre>summary = models.CharField(max_length=350, blank=True, null=True)\r\n<\/pre>\n<p>As we learned in the previous article, we need to migrate the changes to the database:<\/p>\n<pre># python manage.py makemigrations myblog\r\n# python manage.py migrate myblog\r\n<\/pre>\n<div id=\"attachment_16385\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Migrate-Changes-to-Database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16385\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Migrate-Changes-to-Database.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Migrate-Changes-to-Database.png 719w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Django-Migrate-Changes-to-Database-620x182.png 620w\" alt=\"Django: Migrate Changes to Database\" width=\"620\" height=\"182\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django: Migrate Changes to Database<\/p>\n<\/div>\n<p>Then use the admin interface to edit the posts and add a brief summary to each post. Finally, replace the following line in the template (<strong>posts.html<\/strong>):<\/p>\n<pre>&lt;p&gt;{{ post.text|linebreaks }}&lt;\/p&gt;\r\n<\/pre>\n<p>with<\/p>\n<pre>&lt;p&gt;{{ post.summary }}&lt;\/p&gt;\r\n<\/pre>\n<p>Refresh the home page to see the changes:<\/p>\n<div id=\"attachment_16386\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Verify-Changes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-16386\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Verify-Changes.png\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Verify-Changes.png 845w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/Verify-Changes-620x233.png 620w\" alt=\"Django: Verify Changes to Web Project\" width=\"620\" height=\"233\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Django: Verify Changes to Web Project<\/p>\n<\/div>\n<p><strong>2.<\/strong>\u00a0A\u00a0<strong>view<\/strong>\u00a0function takes a HTTP request and returns a HTTP response. In this article,\u00a0<strong>def posts(request)<\/strong>\u00a0in\u00a0<strong>views.py<\/strong>\u00a0makes a call to the underlying database to retrieve all posts. If we want to retrieve all posts with the word\u00a0<a href=\"https:\/\/www.tecmint.com\/install-and-configure-ansible-automation-tool-in-linux\/\" target=\"_blank\" rel=\"noopener\">ansible<\/a>\u00a0in the title, we should replace.<\/p>\n<pre>posts = Post.objects.filter(<strong>whenPublished__lte=timezone.now()<\/strong>).order_by('-whenPublished')\r\n<\/pre>\n<p>with<\/p>\n<pre>posts = Post.objects.filter(<strong>title__icontains=\"ansible\"<\/strong>).order_by('-whenPublished')\r\n<\/pre>\n<p>By separating the user interface from the application logic in web applications, Django facilitates the tasks of maintaining and escalating apps.<\/p>\n<p><strong>3.<\/strong>\u00a0If you followed the instructions provided in this series, the structure of your project should be as follows:<\/p>\n<pre>myfirstdjangoenv\/myfirstdjangoproject\r\n\u251c\u2500\u2500 db.sqlite3\r\n\u251c\u2500\u2500 manage.py\r\n\u251c\u2500\u2500 myblog\r\n\u2502   \u251c\u2500\u2500 admin.py\r\n\u2502   \u251c\u2500\u2500 admin.pyc\r\n\u2502   \u251c\u2500\u2500 __init__.py\r\n\u2502   \u251c\u2500\u2500 __init__.pyc\r\n\u2502   \u251c\u2500\u2500 migrations\r\n\u2502   \u2502   \u251c\u2500\u2500 0001_initial.py\r\n\u2502   \u2502   \u251c\u2500\u2500 0001_initial.pyc\r\n\u2502   \u2502   \u251c\u2500\u2500 __init__.py\r\n\u2502   \u2502   \u2514\u2500\u2500 __init__.pyc\r\n\u2502   \u251c\u2500\u2500 models.py\r\n\u2502   \u251c\u2500\u2500 models.pyc\r\n\u2502   \u251c\u2500\u2500 templates\r\n\u2502   \u2502   \u2514\u2500\u2500 myblog\r\n\u2502   \u2502       \u2514\u2500\u2500 posts.html\r\n\u2502   \u251c\u2500\u2500 tests.py\r\n\u2502   \u251c\u2500\u2500 urls.py\r\n\u2502   \u251c\u2500\u2500 urls.pyc\r\n\u2502   \u251c\u2500\u2500 views.py\r\n\u2502   \u2514\u2500\u2500 views.pyc\r\n\u2514\u2500\u2500 myfirstdjangoproject\r\n    \u251c\u2500\u2500 __init__.py\r\n    \u251c\u2500\u2500 __init__.pyc\r\n    \u251c\u2500\u2500 settings.py\r\n    \u251c\u2500\u2500 settings.pyc\r\n    \u251c\u2500\u2500 urls.py\r\n    \u251c\u2500\u2500 urls.pyc\r\n    \u251c\u2500\u2500 wsgi.py\r\n    \u2514\u2500\u2500 wsgi.pyc\r\n<\/pre>\n<p>In case the above list does not display correctly in your browser, here\u2019s a screenshot of the output of the following command:<\/p>\n<pre># tree myfirstdjangoenv\/myfirstdjangoproject\r\n<\/pre>\n<div id=\"attachment_20779\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/My-Django-Protect-Tree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-20779\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/10\/My-Django-Protect-Tree.png\" alt=\"My Django Protect Tree\" width=\"564\" height=\"742\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">My Django Protect Tree<\/p>\n<\/div>\n<h3>Summary<\/h3>\n<p>Although all of these concepts may seem a little intimidating at first, I can assure you Django is well worth all the efforts necessary to become acquainted with it<\/p>\n<p>I hope that the example that we have used in this series to introduce you to this outstanding web framework will motivate you to learn more. If so, the official\u00a0<a href=\"https:\/\/docs.djangoproject.com\/en\/1.8\/\" target=\"_blank\" rel=\"nofollow noopener\">Django documentation<\/a>\u00a0(which is constantly kept up to date) is the best place to start.<\/p>\n<p>I can assure you that there is a whole lot more to Django than we can adequately cover in a series of articles, so feel free to explore it and learn by doing!<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/install-and-configure-django-web-framework-in-centos-debian-ubuntu\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some 20 years ago when the\u00a0World Wide Web\u00a0was still in its infancy, having a personal or business web site was almost a rare luxury. With the subsequent development of several web technologies and the introduction of dynamic content provided by the combination of server-side programs and databases, companies could no longer be satisfied with having &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/installing-and-configuring-django-web-framework-with-virtual-environments-in-centos-debian\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Installing and Configuring Django Web Framework with Virtual Environments in CentOS\/Debian&#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-11820","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\/11820","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=11820"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11820\/revisions"}],"predecessor-version":[{"id":11821,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11820\/revisions\/11821"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}