Django implementation of combined search

Posted by JasperBosch on Thu, 30 Apr 2020 11:34:20 +0200

1, Implementation method

1. Pure template language implementation

2. Implementation of custom simpletag (in essence, it simplifies the judgment of pure template language)

2, Fundamentals

The principle is to match the url filter conditions through django routing system, take the filter conditions as the database query results, and return them to the front end.

For example, the url format in the routing system is as follows:

url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.filter)

The article ﹐ type ﹐ ID and category ﹐ ID correspond to the fields in the database. When a url is article-1-2.html, the parameters of the background processing function will be a dictionary {'article ﹐ type ﹐ ID': 1, 'category ﹐ ID': 1}. Then the condition will be used as the database query condition, and the result will be returned to the front end

3, Code example

Method 1: pure template language implementation

urls.py

#!/usr/bin/env python3
#_*_ coding:utf-8 _*_
#Author:wd
from django.conf.urls import url

from . import views
urlpatterns = [
    url(r'^$',views.index),
    url(r'^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html',views.filter),
]

models.py

from django.db import models

class Category(models.Model):
    caption=models.CharField(max_length=64)

class Article_type(models.Model):
    caption=models.CharField(max_length=64)

class Article(models.Model):
    title=models.CharField(max_length=64)
    content=models.CharField(max_length=256)
    category=models.ForeignKey(to='Category')
    article_type=models.ForeignKey(to='Article_type'

views.py

def filter(request,*args,**kwargs):
    if request.method=="GET":
        condition={}
        for k,v in kwargs.items():
                    kwargs[k]=int(v)  #Template if judge row.id It's a number, so it needs to be converted here
                    if v=="0":#When the condition is 0, it means all is selected, then it is unnecessary to add it to the filter condition
                        pass
                    else:
                        condition[k]=int(v)
        aritcle=models.Article.objects.filter(**condition)
        aritcle_type=models.Article_type.objects.all()
        aritcle_category=models.Category.objects.all()
        return  render(request,'search.html',{
            'aritcle':aritcle,
            'article_type':aritcle_type,
            'article_category':aritcle_category,
            'article_arg':kwargs,#Pass the current filter criteria to html
        })

html template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .container a{
            display: inline-block;
            padding: 3px 5px;
            margin: 5px;
            border: 1px solid #dddddd ;
        }
        .active{
            background-color: rebeccapurple;

        }
    </style>
</head>
<body>
<h1>search criteria</h1>
<div class="container">
    {% if article_arg.article_type_id == 0 %}
        <a class="active" href="/cmdb/article-0-{{ article_arg.category_id }}.html">whole</a>
    {% else %}
         <a  href="/cmdb/article-0-{{ article_arg.category_id }}.html">whole</a>
    {% endif %}
    {% for row in article_type %}
        {% if row.id == article_arg.article_type_id %}
            <a class="active" href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html">{{ row.caption }}</a>
        {% else %}
            <a  href="/cmdb/article-{{ row.id }}-{{ article_arg.category_id }}.html">{{ row.caption }}</a>
        {% endif %}
    {% endfor %}
</div>
<div class="container">
     {% if article_arg.category_id == 0 %}
        <a class="active" href="/cmdb/article-{{ article_arg.article_type_id }}-0.html">whole</a>
    {% else %}
          <a  href="/cmdb/article-{{ article_arg.article_type_id }}-0.html">whole</a>
    {% endif %}
    {% for row in article_category %}
        {% if row.id == article_arg.category_id %}
        <a class="active" href="/cmdb/article-{{ article_arg.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
        {% else %}
        <a href="/cmdb/article-{{ article_arg.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
        {% endif %}
    {% endfor %}
</div>
<h1>Query results</h1>
<div>
    {% for row in aritcle %}
        <div>{{ row.id }}-{{ row.title }}</div>
    {% endfor %}
</div>
</body>
</html>

Method 2: using simpletag

Topics: Python Django Database