首页 > 代码库 > 联表查询

联表查询

 

技术分享
<div>
        <div><span class="glyphicon glyphicon-search" aria-hidden="true"></span> 搜索条件</div>
        <div class="condition">
            大分类:
            {% if kwargs.article_type_id == 0 %}
                <a class="active"
                   href="/backstage/{{ site }}-0-{{ kwargs.category_id }}-{{ kwargs.article2tag__tag_id }}.html">全部</a>
            {% else %}
                <a href=http://www.mamicode.com/"/backstage/{{ site }}-0-{{ kwargs.category_id }}-{{ kwargs.article2tag__tag_id }}.html">全部</a>
            {% endif %}
            {% for row in type_list %}
                {% if row.0 == kwargs.article_type_id %}
                    <a class="active"
                       href="/backstage/{{ site }}-{{ row.0 }}-{{ kwargs.category_id }}-{{ kwargs.article2tag__tag_id }}.html">{{ row.1 }}</a>
                {% else %}
                    <a href=http://www.mamicode.com/"/backstage/{{ site }}-{{ row.0 }}-{{ kwargs.category_id }}-{{ kwargs.article2tag__tag_id }}.html">{{ row.1 }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div class="condition">
            个人分类:
            {% if kwargs.category_id == 0 %}
                <a class="active"
                   href="/backstage/{{ site }}-{{ kwargs.article_type_id }}-0-{{ kwargs.article2tag__tag_id }}.html">全部</a>
            {% else %}
                <a href=http://www.mamicode.com/"/backstage/{{ site }}-{{ kwargs.article_type_id }}-0-{{ kwargs.article2tag__tag_id }}.html">全部</a>
            {% endif %}
            {% for row in category_list %}
                {% if row.nid == kwargs.category_id %}
                    <a class="active"
                       href="/backstage/{{ site }}-{{ kwargs.article_type_id }}-{{ row.nid }}-{{ kwargs.article2tag__tag_id }}.html">{{ row.title }}</a>
                {% else %}
                    <a href=http://www.mamicode.com/"/backstage/{{ site }}-{{ kwargs.article_type_id }}-{{ row.nid }}-{{ kwargs.article2tag__tag_id }}.html">{{ row.title }}</a>
                {% endif %}
            {% endfor %}
        </div>
        <div class="condition">
            个人标签:
            {% if kwargs.article2tag__tag_id == 0 %}
                <a class="active"
                   href="/backstage/{{ site }}-{{ kwargs.article_type_id }}-{{ kwargs.category_id }}-0.html">全部</a>
            {% else %}
                <a href=http://www.mamicode.com/"/backstage/{{ site }}-{{ kwargs.article_type_id }}-{{ kwargs.category_id }}-0.html">全部</a>
            {% endif %}
            {% for row in tag_list %}
                {% if row.nid == kwargs.article2tag__tag_id %}
                    <a class="active"
                       href="/backstage/{{ site }}-{{ kwargs.article_type_id }}-{{ kwargs.category_id }}-{{ row.nid }}.html">{{ row.title }}</a>
                {% else %}
                    <a href=http://www.mamicode.com/"/backstage/{{ site }}-{{ kwargs.article_type_id }}-{{ kwargs.category_id }}-{{ row.nid }}.html">{{ row.title }}</a>
                {% endif %}
            {% endfor %}
        </div>

        <div class="add_article"><a class="right" href=http://www.mamicode.com/"/add_article-{{ user_info.nid }}-{{ blog_info.nid }}.html"><span class="glyphicon glyphicon-plus"
                                                                aria-hidden="true"></span>创建新文章</a></div>
        <div class="article_list">
            <table class="table table-hover">
                <tr>
                    <th>文章标题</th>
                    <th colspan="2">操作</th>
                </tr>
                {% for article in article_list %}
                    <tr>
                        <td><a href=http://www.mamicode.com/"/perblog/{{ blog_info.site }}/article/{{ article.nid }}.html">{{ article.title }}</a></td>
                        <td><a href=http://www.mamicode.com/"/edi_article-{{ user_info.nid }}-{{ blog_info.nid }}-{{ article.nid }}.html">编辑</a></td>
                        <td><a href=http://www.mamicode.com/"/del_article-{{ user_info.nid }}-{{ blog_info.nid }}-{{ article.nid }}.html">删除</a></td>
                    </tr>
                {% endfor %}
            </table>
        </div>
    </
HTML
技术分享
from django.shortcuts import render, HttpResponse,redirect
from app01 import models

def backstage(request,**kwargs):
    """
    组合删选
    :param request:
    :param kwargs: 前端传过来的定位数字是字符串,要转换成整型
    :return:
    """
    condition = {}
    for k,v in kwargs.items():
        if k != "site":
            kwargs[k] = int(v)
            if v != "0":
                condition[k] = int(v)
    username = request.session.get("username")
    user_info = models.UserInfo.objects.filter(username=username).first()
    site = kwargs.get("site")
    blog_info = models.Blog.objects.filter(site=site).first()
    # 大分类
    type_list = models.Article.type_choices
    # 个人分类
    category_list = models.Category.objects.filter(blog__site=site)
    # 个人标签
    tag_list= models.Tag.objects.filter(blog__site=site)
    condition["blog_id"] = blog_info.nid
    article_list = models.Article.objects.filter(**condition)

    return render(request, back_article.html, {"user_info": user_info,
                                                  "condition": condition,
                                                 blog_info:blog_info,
                                                  "site": site,
                                                  type_list: type_list,
                                                  category_list: category_list,
                                                  tag_list: tag_list,
                                                  article_list: article_list,
                                                  kwargs: kwargs

                                                })
Views

 

联表查询