爬虫成果分享小程序后端及审核后台搭建

2020年2月5日

利用Django写一个简单的小程序接口及后台

利用Django的urls和view写小程序的接口

先在settings中添加一个APP

INSTALLED_APPS = [
    ...
    'wx_xcx',
    ...
]

在urls中添加路由

from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from search import views as search_views
from wx.view import *
# 从上到下匹配
urlpatterns = [
    ...
    # 小程序前端接口
    url(r'^wx_xcx/qurey_rad', wx_xcx_qurey_rad),
    url(r'^wx_xcx/add_wx', wx_xcx_add_wx),
    url(r'^wx_xcx/update_num', wx_xcx_update_num),
    # 小程序后台接口
    path('wx_xcx/index/', wx_index),
    url(r'^wx_xcx/add_page', wx_xcx_add_page),
    url(r'^wx_xcx/del_nop', wx_xcx_del_nop),
    url(r'^wx_xcx/update_pass', wx_xcx_update_pass),
    url(r'^wx_xcx/update_name', wx_xcx_update_name),
    ...
]

接下来就View中的逻辑和路由了

功能还是比较简单的,毕竟第一版

import random
from itertools import chain
from django.http import HttpResponse
import json
from django.shortcuts import render
from .models import sr_Resources


# view 微信公众号接口
# 小程序后台主页
def wx_index(request, msg=None):
    data_list = sr_Resources.objects.filter(is_pass__icontains=0).values()[0:20]
    all_count = sr_Resources.objects.count()
    nop_count = sr_Resources.objects.filter(is_pass__icontains=0).count()
    return render(request, "index.html",
                  {'data': data_list, 'name': '小程序审核微后台', 'all_count': all_count, 'nop_count': nop_count, 'msg': msg})


# 小程序随机取样num个样本By num
def wx_xcx_qurey_rad(request):
    num = request.GET.get('num')
    queryset = None
    all_count = sr_Resources.objects.count()
    index = random.sample(range(0, all_count), num)
    for i in index:
        if queryset != None:
            var = sr_Resources.objects.all().values_list()[i:i + 1]
            queryset = chain(queryset, var)
        else:
            queryset = sr_Resources.objects.all().values_list()[i:i + 1]
    ret = json.dumps(list(queryset), ensure_ascii=False)
    return HttpResponse(ret)


# 小程序添加分享POST By name url author
def wx_xcx_add_wx(request):
    name = request.POST.get('name')
    url = request.POST.get('url')
    author = request.POST.get('author')
    if name and url and author:
        sr_Resources.objects.create(name=name, url=url, author=author, copy_numb=0, is_pass=0)
        msg = '添加成功'
    else:
        msg = '添加失败'
    ret = json.dumps([200, msg], ensure_ascii=False)
    return HttpResponse(ret)


# 小程序变化热度 By id
def wx_xcx_update_num(request):
    id = request.GET.get('id')
    sr = sr_Resources.objects.get(id=id)
    sr.copy_numb += 1
    sr.save()
    ret = json.dumps([200, '复制成功'], ensure_ascii=False)
    return HttpResponse(ret)


# 后台添加分享POST By name url author
def wx_xcx_add_page(request):
    name = request.POST.get('name')
    url = request.POST.get('url')
    author = request.POST.get('author')
    if name and url and author:
        sr_Resources.objects.create(name=name, url=url, author=author, copy_numb=0, is_pass=0)
        msg = '添加成功'
    else:
        msg = '添加失败'
    return wx_index(request, msg)


# 后台删除一条数据By Id
def wx_xcx_del_nop(request):
    id = request.GET.get('id')
    sr_Resources.objects.get(id=id, is_pass='0').delete()
    msg = '删除成功'
    return wx_index(request, msg)


# 后台修改名称By Id Name
def wx_xcx_update_name(request):
    id = request.POST.get('id')
    name = request.POST.get('name')
    sr = sr_Resources.objects.get(id=id)
    sr.name = name
    sr.save()
    return wx_index(request, '修改name成功')


# 后台通过审核By Id
def wx_xcx_update_pass(request):
    id = request.GET.get('id')
    sr = sr_Resources.objects.get(id=id)
    if sr.is_pass == 1:
        msg = None
    else:
        sr.is_pass = 1
        sr.save()
        msg = '审核通过一条'
    return wx_index(request, msg)


# 后台模糊搜索Name
def wx_xcx_qurey(request):
    name = request.GET.get('name')
    queryset = sr_Resources.objects.filter(name__icontains=name).values()
    ret = json.dumps(list(queryset), ensure_ascii=False)
    return HttpResponse(ret)


# 后台查找全部
def wx_xcx_qurey_all(request):
    queryset = sr_Resources.objects.values()
    ret = json.dumps(list(queryset), ensure_ascii=False)
    return HttpResponse(ret)

写好后,用postman进行测试,最后改好小程序的审核后台的模板

{% extends "base.html" %}

{% block body_class %}template-sr_resources{% endblock %}

{% block content %}
<div class="container ">
    <div class=" position-relative overflow-hidden p-3 p-md-5 m-md-3 text-center bg-light">
        <div class="col-md-5 p-lg-5 mx-auto my-5">
            <h1 class="display-4 font-weight-normal">{{ name }}</h1>
            <p class="lead font-weight-normal">分享总数:{{ all_count }}</p>
            <p class="lead font-weight-normal">待审核总数:{{ nop_count }}</p>
        </div>
        <div class="product-device shadow-sm d-none d-md-block"></div>
        <div class="product-device product-device-2 shadow-sm d-none d-md-block"></div>
    </div>
</div>
<div class="container">
    {% if msg %}
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <strong>{{ msg }}</strong> 请继续操作
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    {% endif%}
    <div class="btn-group col-md-12" role="group" aria-label="share">
        <a class="btn btn-light col-md-6" href="/zlfadmin/assdkjfkasjflkajsdjfad/">刷新</a>
        <a class="btn btn-light col-md-6" data-toggle="modal" data-target="#add">添加分享</a>
    </div>
    <table class="table table-borderless">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Id</th>
            <th scope="col">Name</th>
            <th scope="col">Url</th>
            <th class="w-25" scope="col">Author</th>
            <th scope="col">操作</th>
        </tr>
        </thead>
        <tbody>
        {% for post in data %}
        <tr>
            <th scope="row">{{forloop.counter}}</th>
            <td>{{ post.id }}</td>
            <td>{{ post.name }}</td>
            <td>{{ post.url }}</td>
            <td>{{ post.author }}</td>
            <td><a class="btn btn-dark btn-sm" href="/wx/update_pass/?id={{post.id}}">通过审核</a>
                <a class="btn btn-dark btn-sm" href="/wx/del_nop/?id={{post.id}}">删除</a>
                <a class="btn btn-dark btn-sm text-white" data-toggle="modal" data-target="#update_name"
                   data-whatever="{{ post.id }}">修改名称</a>
            </td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
</div>

<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="push" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="push">添加分享</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="/wx/add/" method="post">
                    <div class="form-group">
                        <label for="name-name" class="col-form-label">名称</label>
                        <input type="text" name="name" class="form-control" id="name-name">
                    </div>
                    <div class="form-group">
                        <label for="url-name" class="col-form-label">链接</label>
                        <input type="text" name="url" class="form-control" id="url-name">
                    </div>
                    <div class="form-group">
                        <label for="author-name" class="col-form-label">作者</label>
                        <input type="text" name="author" class="form-control" id="author-name">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="submit" class="btn btn-primary">发布</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="update_name" tabindex="-1" role="dialog" aria-labelledby="update_dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="update_dialog">修改名称</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="/wx/update_name/" method="post">
                    <div class="form-group">
                        <label for="id-name" class="col-form-label">ID</label>
                        <input type="text" name="id" class="form-control" id="id-name" readonly>
                    </div>
                    <div class="form-group">
                        <label for="u-name-name" class="col-form-label">名称</label>
                        <input type="text" name="name" class="form-control" id="u-name-name">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="submit" class="btn btn-primary">发布</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

差不多就是下面的那个样子


{{ name }}

分享总数:{{ all_count }}

待审核总数:{{ nop_count }}

{% if msg %} {% endif%} {% for post in data %} {% endfor %}
# Id Name Url Author 操作
{{forloop.counter}} {{ post.id }} {{ post.name }} {{ post.url }} {{ post.author }} 通过审核 删除 修改名称

返回文章列表

本文所属标签

技术 小程序 后端