环境:pycharm3.6 mysql django html js
1、创建虚拟环境: python -m venv bxwx001
2、CMD进入Scripts目录下 输入指令:activate ,激活虚拟环境
3、将项目需要的包下载到虚拟环境 pip install django/pymysql …
4、创建django 项目 切换到虚拟环境目录下 django startproject bxwx ,创建名为bxwx的django项目。
5、切换到bxwx 目录,创建app 指令:python manage.py startapp bookinfo
6、因为django框架自带后台,所以可以直接创建超级管理员 : python manage.py createsuperuser ,会让输入账号,默认python ,输入密码 ,按要求输入即可,密码不可见。
7、进入项目,可看到以下文件
文件功能我这里就不讲了,我也还没有很精通,只会用。
流程:
a.项目bxwx/settings 做系统配置包括 语言、时区、数据库等的修改。
b.由于没有使用框架默认的sqlite,所以需要在bxwx/init.py中
c.根据分析数据表和呈现方式 写models.py文件
d.根据models文件的类在admin文件中注册,并生成数据迁移(python manage.py makemigrations)和执行迁移
e.执行迁移后,会在app下的migrations 中生成一个0001_initial.py文件 ,里面记录了如何生成数据表,定义了数据表的字段
f.接下来就需要设计视图和模板了 ,在bxwx/urls 中配置,指向app的urls
g.在app的 views中定义各种视图(函数) 并调用相应模板,在项目bxwx下建模板文件templates,存放所有的app模板文件(.html) ,编辑好等待视图views调用 。
models.py
from django.db import models
# Create your models here.
class Bookinfo(models.Model):
book_id=models.CharField(max_length=20,primary_key=True)
book_name=models.CharField(max_length=20)
book_author=models.CharField(max_length=20)
book_introduce=models.CharField(max_length=200)
book_update_time=models.CharField(max_length=20)
img_src=models.CharField(max_length=100)
def __str__(self):
return self.book_name
class Bookinfo1(models.Model):
book_id = models.CharField(max_length=20,)
book_name = models.CharField(max_length=20,primary_key=True)
book_author = models.CharField(max_length=20)
book_introduce = models.CharField(max_length=200)
book_update_time = models.CharField(max_length=20)
img_src = models.CharField(max_length=100)
def __str__(self):
return self.book_name
views.py
from django.shortcuts import render
from bookinfo.models import *
# Create your views here.
def index(request):
return render(request,'bookinfo/index1.html')
def bookinfo(request,keyWords):
try:
book=Bookinfo.objects.get(book_id=keyWords)
context={'b':book}
return render(request,'bookinfo/bookinfo.html',context)
except:
return render(request, 'bookinfo/error.html')
def bookinfoCN(request,keyWords):
try:
book = Bookinfo1.objects.get(book_name=keyWords)
context={'b':book}
return render(request, 'bookinfo/bookinfo.html', context)
except:
return render(request, 'bookinfo/error.html')
settings.py
"""
Django settings for bxwx project.
Generated by 'django-admin startproject' using Django 2.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'b*x3vav&e!=z42$1yji8^*y3_+(qi4*-@@a9v3_ba5@_w&su0&'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bookinfo',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'bxwx.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'bxwx.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bxwx1',
'USER':'root',
'PASSWORD':'123456',
'HOST':'localhost',
'PORT':3306
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
搜索页面的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>小说查询</title>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
function get_url(){
debugger;
var sea = $('#keyWord').val();
window.location.href='http://127.0.0.1:8000/index/'+sea+'/';
}
</script>
<style type="text/css">
#box{
width: 422px;
margin: 100px auto;
font-family: 'Microsoft YaHei';
font-size: 14px;
}
input{
width: 300px;
border: 1px solid #e2e2e2;
height: 40px;
background-size: 25px;
background-position:5px center;
padding:0 0 0 40px;
}
button{
width: 80px;
height: 42px;
float: right;
background: black;
color: white;
text-align: center;
line-height: 32px;
cursor: pointer;
}
</style>
</head>
<body background="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568266542099&di=9b453f4d5fe95f3e6af51752787d5aae&imgtype=0&src=http%3A%2F%2Fimg3.jc001.cn%2Fimg%2F285%2F1550285%2F1311%2F1352830e1352a80.jpg">
<div id="box">
<input id="keyWord" type="text" value="" placeholder="请输入小说关键词">
<button id="serach" onclick="get_url()">搜索</button>
</div>
</body>
</html>
小说详情展示的页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>小说详情</title>
</head>
<body background="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568266542099&di=9b453f4d5fe95f3e6af51752787d5aae&imgtype=0&src=http%3A%2F%2Fimg3.jc001.cn%2Fimg%2F285%2F1550285%2F1311%2F1352830e1352a80.jpg">
<table border="1" width="1320px" >
<tr>
<th width="200px">小说海报</th>
<th width="40px">book_id</th>
<th width="85px">小说名字</th>
<th width="75px">小说作者</th>
<th width="820px">小说简介</th>
<th width="100px">最近更新</th>
</tr>
<tr>
<td><img src="{{b.img_src}}" ></td>
<td>{{b.book_id}}</td>
<td>{{b.book_name}}</td>
<td>{{b.book_author}}</td>
<td>{{b.book_introduce}}</td>
<td>{{b.book_update_time}}</td>
</tr>
</table>
</body>
</html>
搜索界面如下:可通过小说book_id 和小说名查询 ,如果在数据找不到,则会跳转到指定界面。
下面我们来试一下:
再用小说名字试下:
输入一个数据库中找不到的小说 :
随便输入任何字符:
到这里,说明已经成功的完成了搜索功能 并展示在前段界面。
数据库数据来源见上一篇文章:
python爬取小说存储至Mysql并用 django框架做了搜索呈现界面【爬虫-mysql阶段】
转载:https://blog.csdn.net/pythoner111/article/details/100768066