Setting Up Django in a Virtual Environment

In this post I am going to setup Django using Pip through command line.  And I will not use any editor like Visual Studio to create a web application in Django. In this way I will better explain the installation process of Django.  And there is no dependency on any code editor tool. You can use any code editor you like. If you are not familiar with Pip and how to setup a virtual environment, please read my post Python setup and creating virtual environment.

Lets create a virtual directory and name it “MyFristApp” by using the following command,

C:\>c:\python35\python c:\python35\tools\scripts\pyvenv.py c:\users\waqas\documents\projects\MyFirstApp

Next step is to activate the newly created virtual environment by running the “activate.bat” file c:\Users\waqas\Documents\Projects\MyFirstApp\Scripts>activate.bat .  To see what are the packages installed in virtual environment run the following command (MyFirstApp) c:\Users\waqas\Documents\Projects\MyFirstApp>pip freeze , and you will see there is nothing appear on the command window.  Following command will install the Django python -m pip install django==1.9.1 . The Django installation will take little time depend on your internet connection speed. Once the installation is complete you can run the “freeze” command again and it will show you the installed Django package and its version. Now I will create a project in Django by using the following command and name it HelloDjango,

(MyFirstApp) c:\Users\waqas\Documents\Projects\MyFirstApp>python .\scripts\django-admin.py startproject HelloDjango

If you browse the MyFirstApp folder you will see the following folder structure,

Django

Browse the HelloDjango folder and you will see the default files are created in it.  To test the Django installation, I will run the web server and a default page will appear. Django comes with a default lightweight web server, its entirely written in Python. But use it for development purpose only, you should not use this or setup this builtin web server for production environment. To run the web server type the following command python manage.py runserver but make sure you should be in HelloDjango directory. The server will by default run on the following IP address http://127.0.0.1:8000/.

 

Django Web Server

As you can see in the above picture the server is running, ignore migration error this error appears because I not setup the database yet. Type the IP address in browser and you will the default output

HelloDjango-img3

Django project consists of multiple apps. An App is a website which perform some functionality.  I will create a simple App which will return an HTML. From the command prompt stop the web server by pressing Ctrl+Break keys. Execute the following command and create a sample app I name it HelloWorld python manage.py startapp HelloWorld .  If you want to verify if the app is successfully installed or not type the URL in the browser “http://127.0.0.1:8000/Helloworld” and you will get the 404 page not found error.  Now I am going to configure the view.  Stop the web server if its running by pressing “Ctrl + Break” keys. Open the “views.py” file from the HelloWorld folder. You can use any text editor. I am going to use Sublime Text to open the file and type the following line in it.

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. This is landing page for HelloWorld app")

The above code means when a calls come for index page the app will return a string in response. To view the string in browser we have to create a URL configuration file in the app folder. create a file name “urls.py” in the root folder of HelloWorld app. And copy the following code in it,

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

Next step is to modify the “urls.py” file in the HelloDjango folder.  Add the “include” keyword in line from django.conf.urls import url , except admin you should use “include()” for adding URL patterns. After modifying the “urls.py” it will look like this,

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

Now run the server and execute the following URL “http://127.0.0.1:8000/HelloWorld/” and you will see the output in the browser. This is a string output return in the response. What if we want to point to HTML page. Django will look for HTML pages in a “Template” folder. Create a new folder the HelloWorld app folder and name is template. Inside the folder create an HTML page and name it “index.html”.  Add the following lines in the page,

<!Doctype html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Hello World</title>
	</head>
	<body>
		<h2>Hello World</h2>
		<p>This content is displayed from html page.</p>
	</body>	
</html>

Next step is to add the app in the installed app setting. Open the “settings.py” file located in the HelloDjango folder.  And add the HelloWorld app,

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'HelloWorld',
]

Now modify the “views.py” file in the so it will read the content from HTML file instead of returning the string.  The file will look like this after modification,

from django.shortcuts import render, render_to_response
from django.http import HttpResponse

def index(request):
    return render_to_response("index.html")

Now run the server again hit the URL, you will the output. Its quite simple and I hope you will get quite a good understanding of how Django is working.

Leave a Reply