Web Development With Django: © Shrinath Shenoy
Web Development With Django: © Shrinath Shenoy
© Shrinath Shenoy
What is Django
urlpatterns += patterns('',
urlpatterns = (
url(r'^$', views.index, name='index'),
)
views.py
There are two types of view. That a user can opt for
depending on the requirement.
1. Function Based View
2. Classbased view
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>Its now %s</body></html>"
% now
return HttpResponse(html)
A typical Django CBV would look like this
import datetime
from django.http import HttpResponse
from django.views.generic import View
class ShowDateView(View):
def get(request):
now = datetime.datetime.now()
html = "<html><body>Its now %s</body></html>"
% now
return HttpResponse(html)
models.py
class UserProfile(models.Model):
first_name = models.CharField(max_lenght=10)
last_name = models.CharField(max_lenght=10)
phone_number = models.IntegerField(null=True)
Availble Built in Fields in Django models
AutoField
BigIntegerField
BooleanField
CharField
CommaSeparatedIntegerField
DateField
DateTimeField
DecimalField
EmailField
FileField
forms.py
class LoginForm(forms.Form):
username = forms.CharField(max_length=12)
password = forms.CharField(max_lenght=12, att)
Django Forms built in Fields
BooleanFieldCharField ChoiceField
DateField DateTimeField
EmailField FileField
FloatField ImageField
IPAddressField GenericIPAddressField
TypedMultipleChoiceField NullBooleanField
SlugField TimeField
TypedChoiceField DecimalField
FilePathField IntegerField
MultipleChoiceField RegexField
URLField
templates