Introduction to Django and installation
B.Krishnakumar
Architecture
virtual environment
How to create Virtual Environment?�
How to create Virtual Environment?
Installing Django
Creating and Running a Django Project�
Running A Django Project
Creating A Django App�
[ 'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app' ]
Creating Web App
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello, World !')
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")
]
URLconfs and Loose Coupling
little or no effect on the other.
Dynamic URLs
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),
)
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)
each view always takes an HttpRequest object as its first parameter