Server side Database Serach Fundamental Research Group,IIT Bombay
Server Side Database Search
Charu Sharma
Fundamental Research GroupIIT Bombay
Charu Sharma (IIT Bombay) 1 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Introduction
Django developers create features so that programmers need not to switchbetween PYTHON and SQL
Charu Sharma (IIT Bombay) 2 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Searching on Client and Server side
Persistence
1 Server side database offers persistence
2 Client side in-memory database does not offer upto that extent whatserver side does
ScalingA Database is designed so that performance will not take too much of hit ifit grows ten times bigger but this is not the case with in-memory database.
Charu Sharma (IIT Bombay) 3 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Handling databases through Django models
Django can use any of the packages like psycopg2 and sqlite3.Code forsqlite3 connection is here:edit in settings.py
DATABASES = {
’default’: {
’ENGINE’: ’django.db.backends.sqlite3’,
’NAME’: ’/home/user/Documents/mysite/sqlite3.db’
Charu Sharma (IIT Bombay) 4 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
EXAMPLE:Registration Form
Charu Sharma (IIT Bombay) 5 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Charu Sharma (IIT Bombay) 6 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Charu Sharma (IIT Bombay) 7 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Charu Sharma (IIT Bombay) 8 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Charu Sharma (IIT Bombay) 9 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Source Code Of Example
1 Go into the mysite/myapp directory, and create the directorytemplates. Enter the templates directory.
2 Create a template file named insregis.html. containing the followingtemplate:
<html>
</body>
<form action="" method="post">
{% csrf_token %}
<h3>Username :</h3>{{registration_form.username}}
<h3>Password :</h3>{{registration_form.password}}
<h3>Confirm Password :</h3>{{registration_form.confirmpassword}}
<h3>City :</h3>{{registration_form.city}}
<input type="submit" value="Register"/>
</form>
</body>
</html>Charu Sharma (IIT Bombay) 10 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
models.py
Add models in models.py file
from django.db import models
CITY_CHOICES=(
(’Bangalore’,’Bangalore’),
(’Delhi’,’Delhi’),
(’Pune’,’Pune’),
(’Mumbai’,’Mumbai’),
)
class Registration(models.Model):
username=models.CharField(max_length=100)
password=models.CharField(max_length=100)
confirmpassword=models.CharField(max_length=100)
city=models.CharField(max_length=100,choices=CITY_CHOICES)
Charu Sharma (IIT Bombay) 11 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
forms.py
Then create the forms.py file containing the following:
from django.forms import ModelForm,Textarea
from models import Registration
from django import forms
class RegistrationForm(ModelForm):
class Meta:
model=Registration
fields=[’username’,’password’,’confirmpassword’,’city’]
widgets={
’password’:forms.PasswordInput(),
’confirmpassword’:forms.PasswordInput()
}
Charu Sharma (IIT Bombay) 12 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
views.py
Then create the views.py file containing the following:
from django.shortcuts import render_to_response,render
from django.template import RequestContext
from django.http import HttpResponseRedirect,HttpResponse
from models import Registration
from forms import RegistrationForm
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.forms import AuthenticationForm,authenticate
def register(request):
if request.method == ’POST’:
registration_form=RegistrationForm(data=request.POST)
if registration_form.is_valid():
if request.POST.get(’password’)!=request.POST.get(’confirmpassword’):
return HttpResponseRedirect(’/myapp/register’)
Charu Sharma (IIT Bombay) 13 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
else:
if Registration.objects.filter(username=request.POST.get(’username’)).count()!=0:
return HttpResponseRedirect(’/myapp/register’)
registration_form.save()
request.session[’userrname’]=request.POST.get(’username’)
request.session[’passsword’]=request.POST.get(’password’)
request.session[’confirmmpassword’]=request.POST.get(’confirmpassword’)
request.session[’cityy’]=request.POST.get(’city’)
return HttpResponseRedirect(’/myapp/submit’)
else:
return HttpResponseRedirect(’/myapp/register’)
else:
registrationform={’registration_form’:RegistrationForm}
return render_to_response(’ins_regis.html’,registrationform,context_instance=RequestContext(request))
Charu Sharma (IIT Bombay) 14 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
def submit(request):
state = "You have registered successfully"
return render_to_response(’submit.html’,context_instance=RequestContext(request))
@csrf_exempt
def view(request):
obj_info=Registration.objects.filter(username=request.session.get(’userrname’))
user_data ={"user_detail" : obj_info}
return render_to_response(’search.html’,user_data,context_instance=RequestContext(request))
@csrf_exempt
def detailall(request):
obj_info1 = Registration.objects.filter()
user_data1 ={"user_detail1" : obj_info1}
return render_to_response(’userdetail.html’,user_data1,context_instance=RequestContext(request))
Charu Sharma (IIT Bombay) 15 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
@csrf_exempt
def search1(request):
if request.method == ’POST’:
usersearch_form=RegistrationForm(data=request.POST)
if Registration.objects.filter(username=request.POST.get(’username’)).count()==0:
return HttpResponseRedirect(’/myapp/search1’)
else:
form = AuthenticationForm(request.POST)
u = request.POST[’username’]
user = authenticate(username=u)
if user is not None:
if user.is_active:
login(user)
else:
message = ’disabled account’
return render(request,’submit.html’, {’message’: message})
Charu Sharma (IIT Bombay) 16 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
user_info=Registration.objects.filter(username=request.POST.get(’username’))
user_data1 ={"user_detail1" : user_info}
return render_to_response(’submit.html’,user_data1,context_instance=RequestContext(request))
else:
usersearchform={’usersearch_form’:RegistrationForm}
return render_to_response(’submit.html’,usersearchform,context_instance=RequestContext(request))
Charu Sharma (IIT Bombay) 17 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
urls.py
Go up one level to the myapp directory and edit the urls.py file so that thefirst line after
from django.conf.urls import patterns,url
from myapp import views
from mysite import settings
urlpatterns = patterns(’’,
url(r’^register$’,views.register,name=’register’),
url(r’^submit$’,views.submit,name=’submit’),
url(r’^view$’,views.view,name=’view’),
url(r’^search1$’,views.search1,name=’search1’),
url(r’^detailall$’,views.detailall,name=’detailall’),
)
Charu Sharma (IIT Bombay) 18 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
settings.py
Edit settings.py file, and add:
DATABASES = {
default: {
ENGINE: django.db.backends.sqlite3,
NAME: /home/user/Documents/mysite/sqlite3.db
Enable myapp by adding the below code in INSTALLEDAPPS
myapp,
Charu Sharma (IIT Bombay) 19 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
urls.py
Edit urls.py file in mysite directory, and include myapp.urls:
from django.conf.urls import patterns, include, url
from mysite import settings
urlpatterns = patterns(,
url(r^myapp/, include(myapp.urls)),
)
Charu Sharma (IIT Bombay) 20 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
Run
Then, from the command line, run:
python manage.py syncdb
python manage.py runserver 0.0.0.0:9000
http://localhost:9000/myapp/register
Charu Sharma (IIT Bombay) 21 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
submit.html
To display the registered user details click on View button.
<!DOCTYPE html>
<html>
<body>
<h3 style="margin-bottom:0;">You have registered successfully!!</h3>
<form action="/myapp/view" method="post">
<input type="submit" value="Current User Details"/>
</form>
<form action="/myapp/detailall" method="post">
<input type="submit" value="All Details"/>
</form>
Charu Sharma (IIT Bombay) 22 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
<form action="/myapp/search1" method="post">
{% csrf_token %}
<input type="text" name="username" value="{{ usersearch_form.username }}"/>
<input type="submit" value="Search"/>
{% for detail in user_detail1 %}
<h4>Username:</h4><tr><td>{{ detail.username }}</td></tr></br>
<h4>Password:</h4><tr><td>{{ detail.password }}</td></tr></br>
<h4>Confirm Password:</h4><tr><td>{{ detail.confirmpassword }}</td></tr></br>
<h4>City :</h4><tr><td>{{ detail.city }}</td></tr></br>
{% endfor %}
</form>
</body>
</html>
Charu Sharma (IIT Bombay) 23 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
search.html
Here are the details of the current user
<html><body>
<form>{% csrf_token %}
<h3>Data fetched from Database</h3>
{% for detail in user_detail %}
<h5>Username :</h5><tr><td>{{ detail.username }}</td></tr></br>
<h5>Password :</h5><tr><td>{{ detail.password }}</td></tr></br>
<h5>Confirm Password :</h5><tr><td>{{ detail.confirmpassword }}</td></tr></br>
<h5>City :</h5><tr><td>{{ detail.city }}</td></tr></br>
{% endfor %}
<h3>Data fetched from Session</h3>
<h5>Username :</h5>{{ request.session.userrname }}
<h5>Password :</h5>{{ request.session.passsword }}
<h5>Confirm Password :</h5>{{ request.session.confirmmpassword }}
<h5>City :</h5>{{ request.session.cityy }}</form>
</body></html>Charu Sharma (IIT Bombay) 24 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
userdetail.html
Here are the details of all the users
<html><body>
<form>{% csrf_token %}
<table border="1px">
<tr><td><h3>Username:</h3></td>
<td><h3>Password:</h3></td>
<td><h3>Confirm Password:</h3></td>
<td><h3>City :</h3></td></tr>
{% for detail in user_detail1 %}
<tr><td>{{ detail.username }}</td>
<td>{{ detail.password }}</td>
<td>{{ detail.confirmpassword }}</td>
<td>{{ detail.city }}</td></tr></br>
{% endfor %}
</table>
</body></html>Charu Sharma (IIT Bombay) 25 / 26
Server side Database Serach Fundamental Research Group,IIT Bombay
A TOUR OF D JANGO PERSISTENCE FACILITIES
Django handles ORM
Charu Sharma (IIT Bombay) 26 / 26
Top Related