django-admin.py startproject test_project
cd test_project
python manage.py startapp test_app1
URLS:
url(r'^test_app1/', include('test_project.test_app1.urls')),
test_app1/URLS:
url(r'^/?$', 'test_app1.views.home', name='home'),
from django.shortcuts import HttpResponse, render_to_response
def home(request):
return HttpResponse("Home Page!")
# return render_to_response('test_app1/home.html', {'developer_name':'vivek'})
Any text surrounded by a pair of braces (e.g., {{ person_name }} ) is a variable . This means “insert the value of the variable with the given name.”
Any text that’s surrounded by curly braces and percent signs (e.g., {% if is_experienced %} ) is a template tag .
mkdir -p templates/test_app1
<html>
<body>
This page is developed by
{{ developer_name }}!
</body>
</html>
class Developer(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
experience = models.IntegerField()
python manage.py syncdb
from test_app1 import models
models.Developer(name='vivek', experience=3).save()
A website has many common features that spread across various pages or the whole website. In-order to avoid template duplication, django provides the following template inheritance logic: The first step is to define a base template – a skeleton of your page that child templates will later fill in.
<html lang="en"> <head><title>{% block title %}{% endblock %}</title> </head> <body> <h1>My first site</h1> {% block content %}{% endblock %} {% block footer %} <hr> <p>Thanks for visiting my site.</p> {% endblock %} </body> </html>
{% extends "base.html" %}
{% block title %}Home Page{% endblock %}
{% block content %}
<p>>This page is developed by {{ developer_name }}!</p>
{% endblock %}
{% include "inclusions/disclaimer.html" %}
mkdir -p templates/inclusions
disclaimer.html:
<p>©Global Analytics</p>
from django.shortcuts import HttpResponse, render_to_response
from test_app1.models import Developer
def home(request):
developers = Developer.objects.all()
return render_to_response('test_app1/home.html', {'developers':developers})
<table>
<tr>
<th>Developer Name</th>
<th>Designation</th>
</tr>
{% for developer in developers %}
<tr>
<td>{{ developer.name }}</td>
<td>
{% if developer.experience > 2 %}
Senior Developer
{% else %}
Developer
{% endif %}
</td>
</tr>
{% endfor %}
</table>
<p>This page is developed by {{ developers.0.name }}!</p>
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
import uuid
class TestMiddleware(object):
def process_request(self, request):
unique_id = request.COOKIES.get(\
'unique_id', str(uuid.uuid4()))
request.__setattr__('unique_id', unique_id)
return render_to_response('test_app1/home.html', \
{'developers':developers, 'unique_id':request.unique_id})
<p>Your unique id is: {{ unique_id }}</p>
Context processors allows us to add common variables to the context dictionary before getting passed to the template create a context_processors.py file in the Django project folder:
def extra_context(request):
"""
Add variables that you would like accessed across all pages.
"""
context_extras = {}
context_extras['unique_id'] = request.unique_id
return context_extras
'test_project.context_processors.extra_context'
from django.template import RequestContext
return render_to_response('test_app1/home.html', {'developers':developers}, context_instance=RequestContext(request))
from test_app1.views import AddDeveloper
url(r'^add/?$', AddDeveloper.as_view(), name='add'),
class AddDeveloper(View):
def get(self, *args, **kwargs):
return render_to_response('test_app1/add.html', {}, context_instance=RequestContext(self.request))
def post(self, *args, **kwargs):
print self.request.POST
if self.is_valid():
Developer(name=self.request.POST['developer_name'], experience=self.request.POST['experience']).save()
return HttpResponseRedirect("/test_app1/")
else:
return render_to_response('test_app1/add.html', {'errors':self.errors}, context_instance=RequestContext(self.request))
def is_valid(self):
self.errors = {}
if not self.request.POST.get('developer_name'):
self.errors['developer_name'] = 'Please enter developer name'
if not self.request.POST.get('experience'):
self.errors['experience'] = 'Please select your experience'
return False if self.errors else True
<style> .error { color: red;} </style>
<form method="post" > {% csrf_token %} <label>Dev Name</label> {% if errors.developer_name %} <span class="error">{{ errors.developer_name }}</span> {% endif %}
<input type="text" name="developer_name" /> <br> <br> <label>Experience</label> {% if errors.experience %} <span class="error">{{ errors.experience }}</span> {% endif %}
<select name="experience"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input type="submit" value="submit" name="submit" /> </form>
from django import forms
EXPERIENCE = (
('1','1',),
('2','2',),
('3','3',),
)
class DeveloperForm(forms.Form):
name = forms.CharField(required=True, label='Developer Name')
experience = forms.ChoiceField(choices=EXPERIENCE, widget=forms.Select(attrs={'data-field': '1'}), required=True)
class AddDeveloper(View):
def get(self, *args, **kwargs):
form = DeveloperForm()
return render_to_response('test_app1/add.html', {'form':form}, context_instance=RequestContext(self.request))
def post(self, *args, **kwargs):
form = DeveloperForm(self.request.POST)
if form.is_valid():
Developer(name=self.request.POST['developer_name'], experience=self.request.POST['experience']).save()
return HttpResponseRedirect("/test_app1/")
else:
return render_to_response('test_app1/add.html', {'form':form}, context_instance=RequestContext(self.request))
<style>
.error {
color: red;
}
</style>
<form method="post" >
{% csrf_token %}
<span class="error">{{form.name.errors.as_text }}</span><br/>
{{ form.name.label }}
{{ form.name }}
<br>
<br>
{{ form.experience.label }}
{{ form.experience }}
<br>
<br>
<input type="submit" value="submit" name="submit" />
</form>
def clean_name(self):
name = self.cleaned_data['name']
num_words = len(name.split())
if num_words < 2:
raise forms.ValidationError("Enter first & last name")
return name
# BAD!
def ua_display_bad(request):
ua = request.META['HTTP_USER_AGENT'] # Might raise KeyError!
return HttpResponse("Your browser is %s" % ua)
# GOOD (VERSION 1)
def ua_display_good1(request):
#OR ua = request.META.get('HTTP_USER_AGENT', 'unknown')
try:
ua = request.META['HTTP_USER_AGENT']
except KeyError:
ua = 'unknown'
return HttpResponse("Your browser is %s" % ua)