models:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from django.db import models
# Create your models here
class Publisher(models.Model):
name = models.CharField(max_length = 30)
address = models.CharField(max_length = 50)
city = models.CharField(max_length = 60)
state_province =models.CharField(max_length = 30)
country =models.CharField(max_length = 50)
website = models.URLField()
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email =models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher =models.ForeignKey(Publisher)
publication_date = models.DateField()
views:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mybook.settings'
from django.shortcuts import render_to_response
from django.http import HttpResponse
from books.models import *
def search_form(request):
return render_to_response('search_form.html')
def search(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
books =Book.objects.filter(title=q)
return render_to_response('search_result.html',
{'books':books, 'query':q})
else:
return render_to_response('search_form.html',
{'error':True})
urls:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mybook.settings'
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from books.views import search_form
from books.views import search
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mybook.views.home', name='home'),
# url(r'^mybook/', include('mybook.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^search_form/$',search_form),
(r'^search/$',search)
)
search_form:
<!DOCTYPE html>
<html>
<head >
<title>Search</title>
</head>
<body>
{% if error %}
<p style="color: red;"> please submit a search term</p>
{% endif %}
<form action="/search/" method="get">
<input type="text" name="q">
<input type="submit" name="Search">
</form>
</body>
</html>
search_result:
<p>You searched for: <strong>{{ query }}</strong></p>
{% if books %}
<p>Found {{ books|length }} book{{ books|pluralize }}</p>
<ul>
{% for book in books %}
<li>{{ book.title }}</li>
{% endfor %}
</ul>
{% else %}
<p>No books matched your search criterria</p>
{% endif %}
.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from django.db import models
# Create your models here
class Publisher(models.Model):
name = models.CharField(max_length = 30)
address = models.CharField(max_length = 50)
city = models.CharField(max_length = 60)
state_province =models.CharField(max_length = 30)
country =models.CharField(max_length = 50)
website = models.URLField()
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email =models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher =models.ForeignKey(Publisher)
publication_date = models.DateField()
views:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mybook.settings'
from django.shortcuts import render_to_response
from django.http import HttpResponse
from books.models import *
def search_form(request):
return render_to_response('search_form.html')
def search(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
books =Book.objects.filter(title=q)
return render_to_response('search_result.html',
{'books':books, 'query':q})
else:
return render_to_response('search_form.html',
{'error':True})
urls:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mybook.settings'
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from books.views import search_form
from books.views import search
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mybook.views.home', name='home'),
# url(r'^mybook/', include('mybook.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^search_form/$',search_form),
(r'^search/$',search)
)
search_form:
<!DOCTYPE html>
<html>
<head >
<title>Search</title>
</head>
<body>
{% if error %}
<p style="color: red;"> please submit a search term</p>
{% endif %}
<form action="/search/" method="get">
<input type="text" name="q">
<input type="submit" name="Search">
</form>
</body>
</html>
search_result:
<p>You searched for: <strong>{{ query }}</strong></p>
{% if books %}
<p>Found {{ books|length }} book{{ books|pluralize }}</p>
<ul>
{% for book in books %}
<li>{{ book.title }}</li>
{% endfor %}
</ul>
{% else %}
<p>No books matched your search criterria</p>
{% endif %}
.