python crawls stories to Mysql and uses Django framework to do a search rendering interface [django search rendering stage]

Environment: pycharm 3.6 MySQL Django HTML JS

1. Creating a virtual environment: python-m venv bxwx001

2. CMD enters the Scripts directory and enters the instruction: activate to activate the virtual environment

3. Download the package needed by the project to the virtual environment pip install django/pymysql...
4. Create a django project, switch to django start project bxwx under the virtual environment directory, and create a django project named bxwx.

5. Switch to the bxwx directory and create the app instruction: Python startapp bookinfo
6. Because the django framework has its own background, you can directly create a super administrator: Python management. py create superuser, which will allow you to enter an account, default python, enter a password, as required, the password is invisible.
7. Enter the project and see the following documents

I won't talk about the file function here. I'm not very proficient yet. I can only use it.
Technological process:
a. Project bxwx/settings to make system configuration including language, time zone, database and other modifications.
b. Since the framework default sqlite is not used, it needs to be in bxwx/

c. Write files based on analysis data tables and presentation
d. Register in admin file according to the classes of the models file, generate Python management. py makemigrations, and execute migrations

e. After migration, a file is generated in migrations under app, which records how to generate the data table and defines the fields of the data table.

f. Next you need to design views and templates, configure them in bxwx/urls, and point to the URLs of app

g. Define various views (functions) in app views and call corresponding templates. Modeling template files under project bxwx, store all app template files (.html), edit and wait for view views call.

from django.db import models

# Create your models here.
class Bookinfo(models.Model):
    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

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):
    return render(request,'bookinfo/bookinfo.html',context)
      return render(request, 'bookinfo/error.html')

def bookinfoCN(request,keyWords):
     book = Bookinfo1.objects.get(book_name=keyWords)
     return render(request, 'bookinfo/bookinfo.html', context)

      return render(request, 'bookinfo/error.html')

Django settings for bxwx project.

Generated by 'django-admin startproject' using Django 2.2.5.

For more information on this file, see

For the full list of settings and their values, see

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

# 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


# Application definition



ROOT_URLCONF = 'bxwx.urls'

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

WSGI_APPLICATION = 'bxwx.wsgi.application'

# Database

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bxwx1',

# Password validation

        '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

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'

Search page html

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Novel query</title>
<script type="text/javascript" src=""></script>
function get_url(){
     var sea = $('#keyWord').val();

<style type="text/css">
        width: 422px;
        margin: 100px auto;
        font-family: 'Microsoft YaHei';
        font-size: 14px;
        width: 300px;
        border: 1px solid #e2e2e2;
        height: 40px;
        background-size: 25px;
        background-position:5px center;
        padding:0 0 0 40px;
        width: 80px;
        height: 42px;
        float: right;
        background: black;
        color:  white;
        text-align: center;
        line-height: 32px;
        cursor: pointer;



<body background="">

<div id="box">
<input id="keyWord"  type="text" value="" placeholder="Please enter the key words of the novel.">
<button id="serach" onclick="get_url()">search</button>

Details of the novel are displayed on the page:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Novel details</title>
<body background="">

<table border="1" width="1320px" >
    <th width="200px">Fiction Poster</th>
    <th width="40px">book_id</th>
    <th width="85px">The name of the novel</th>
    <th width="75px">Fiction author</th>
    <th width="820px">Introduction to Novels</th>
    <th width="100px">Recent updates</th>

      <td><img src="{{b.img_src}}" ></td>




The search interface is as follows: it can be queried by novel book_id and novel name. If the data can not be found, it will jump to the specified interface.

Now let's try:

Then try the name of the novel:

Enter a novel that cannot be found in a database:

Enter any character at will:

At this point, the search function has been successfully completed and displayed in the front section of the interface.
See the previous article for database data sources:
python crawls stories to Mysql and uses django framework to do a search and presentation interface [crawler-mysql phase]

Tags: Django Python MySQL Database

Posted on Wed, 11 Sep 2019 22:47:54 -0700 by SuprSpy79