1 of 25

Introduction to Django and installation

B.Krishnakumar

2 of 25

Architecture

3 of 25

virtual environment

  • It is a way for you to have multiple versions of python on your machine without them clashing with each other, each version can be considered as a development environment and you can have different versions of python libraries and modules all isolated from one another

4 of 25

How to create Virtual Environment?�

  • Step – 1
  • Open your terminal and create a directory to store all your virtual environments, using the command mkdir (mkdir environment)
  • Now go inside the directory using the command CD which stands for call Directory, CD Environments (cd environment)
  • Step 2
  • use a module named virtualenv to create isolated virtual environments.
  • So, first install this module by the following command,
  • pip install virtualenv
  • To verify a successful installation run this
  • virtualenv –versionNow
  • Next, create a virtual environment named myenv.

5 of 25

How to create Virtual Environment?

  • How to create Virtual Environment if you have two different versions of Python installed in your machine?
  • To create a Virtual Environment for Python 2.x do the following
  • virtualenv myenv
  • For a Python 3 virtual environment type –
  • python3 -m venv myenvI
  • if you only have Python 3 on your machine do the following
  • virtualenv myenv
  • This will also work 🡪 python -m venv myenv

6 of 25

  • To add modules and packages in our Environment, we need to activate it first.

  • myenv\Scripts\activate.bat
  • test\Scripts\activate.bat

7 of 25

Installing Django

  • proceed to Install Django.
  • pip install Django
  • to know the version of Django installed in your system execute this is terminal.
  • django-admin --version

8 of 25

Creating and Running a Django Project�

  • django-admin startproject mysite
  • mysite/
    • manage.py
    • mysite/
      • __init__.py
      • settings.py
      • urls.py
      • wsgi.py

Running A Django Project

  • python manage.py runserver

  • open your preferred browser and go to http://127.0.0.1:8000/

9 of 25

Creating A Django App�

  • python manage.py startapp my_app
  • Open your preferred text editor and open the settings.py file and scroll to the INSTALLED_APPS section.
  • INSTALLED_APPS =

[ 'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes', 'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'my_app' ]

10 of 25

Creating Web App

  • views.py

from django.http import HttpResponse

def index(request):

return HttpResponse('Hello, World !')

  • urls.py

from django.contrib import admin

from django.urls import path

# imported views

from my_app import views

urlpatterns = [

path('admin/', admin.site.urls),

# configured the url

path('',views.index, name="homepage")

]

11 of 25

  • python manage.py runserver
  • visit http://127.0.0.1:8000/

12 of 25

URLconfs and Loose Coupling

  • loose coupling is a software-development approach that values the importance of making pieces interchangeable.
  • If two pieces of code are loosely coupled, then changes made to one of the pieces will have

little or no effect on the other.

13 of 25

  • In a Django web application, the URL definitions and the view functions they call are loosely coupled
  • that is, the decision of what the URL should be for a given function, and the implementation of the function itself, reside in two separate places. This lets you switch out one piece without affecting the other.

14 of 25

  • If we wanted to change the URL for the application – say, to move it from /time/ to /current-time/ – we could make a quick change to the URLconf, without having to worry about the view itself.
  • if we wanted to change the view function – altering its logic somehow – we could do that without affecting the URL to which the function is bound.

15 of 25

  • if we wanted to expose the current-date functionality at several URLs
  • url(r’^time/$’, current_datetime),
  • url(r’^another-time-page/$’, current_datetime),

16 of 25

Dynamic URLs

  • In our current_datetime view, the contents of the page – the current date/time – were dynamic, but the URL (/time/) was static.

17 of 25

  • Let’s create a third view that displays the current date and time offset by a certain number of hours.
  • The goal is to craft a site in such a way that the page /time/plus/1/ displays the date/time one hour into the future,
  • The page /time/plus/2/ displays the date/time two hours into the future,
  • the page /time/plus/3/ displays the date/time three hours into the future, and so on.

18 of 25

urlpatterns = patterns(’’,

url(r’^time/$’, current_datetime),

url(r’^time/plus/1/$’, one_hour_ahead),

url (r’^time/plus/2/$’, two_hours_ahead),

url(r’^time/plus/3/$’, three_hours_ahead),

url (r’^time/plus/4/$’, four_hours_ahead),

)

19 of 25

  • url(r’^time/plus/\d+/$’, hours_ahead),

  • url(r’^time/plus/\d{1,2}/$’, hours_ahead),

20 of 25

def hours_ahead(request, offset):

try:

offset = int(offset)

except ValueError:

raise Http404()

dt = datetime.datetime.now() + datetime.timedelta(hours=offset)

html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)

return HttpResponse(html)

21 of 25

  • request is an HttpRequest object, just as in hello and current_datetime.

each view always takes an HttpRequest object as its first parameter

22 of 25

  • offset is the string captured by the parentheses in the URLpattern.
  • For example, if the requested URL were /time/plus/3/, then offset would be the string ’3’.
  • If the requested URL were /time/plus/21/, then offset would be the string ’21’. Note that captured values will always be strings, not integers, even if the string is composed of only digits, such as ’21’.

23 of 25

  • we do within the function is call int() on offset. This converts the string value to an integer

24 of 25

  • def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt) return HttpResponse(html)

25 of 25

  • from django.conf.urls import url, include
  • url(r'^time/plus/(\d+)/$', views.hours_ahead, name='hours_ahead')