7
Django

Django

Embed Size (px)

Citation preview

Page 1: Django

Django

Page 2: Django

Start a new project• django-admin startproject appName

• Directory with the project’s name would be created inside the project with multiple files such as

-settings.py

-urls.py

-wsgi.py

Page 3: Django

Create a new App in project• A django project is a collection of reusable

applications

• For example, you can create a user registration , login app once for a single project of yours and then reuse it with any other project

• To create an app, go to your project’s root and enter

-python manage.py startpp appname

Page 4: Django

App Structure

• The new app that is created would have the following python files as default

-admin.py

-models.py

-tests.py

-views.py

Page 5: Django

Models

• Each model is a python class that subclasses django.db.models.Model• Each attribute of model represents a database field• Once you have defined your models , you need to tell django that you

are going to use those models by adding your app name to INSTALLED_APPS in your settings file

• Ex:-

class Institute(models.Model):

firstName=models.CharField(max_length=30)

secondName=models.CharField(max_length=30)

Page 6: Django

Viewsfrom django.shortcuts import render

import datetime

def currentDateTime(request):

now=datetime.datetime.now()

return render(request,”template.html”,{‘dateTime’,now})

read :https://docs.djangoproject.com/en/1.9/topics/http/views/

Page 7: Django

forms• They are used for input validation just like command objects , as a matter of fact I used to name them using

commandObjects.py unless rebuked by a django nerd.

• Ex:-

from django import forms

class loginForm(forms.Form):

username=forms.EmailField(max_length=70,required=True,widget=forms.EmailInput(attrs={‘class’:’abc’}))

password=forms.CharField(max_length=70,widget=forms.PasswordInput())

In View

import loginForm

def loginUser(request):

form=loginForm(request.POST or None)

if form.is_valid():

print pass

else:

print fail