动态的django项目

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 %}
.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值