django 조합 검색 (검색 탭 저장 데이터베이스에)

4439 단어
models.py
from __future__ import unicode_literals
from django.db import models

# Create your models here.

class Category(models.Model):
    caption = models.CharField(max_length=12)
    def __unicode__(self):
        return self.caption

class ArticleTag(models.Model):
    caption = models.CharField(max_length=12)
    def __unicode__(self):
        return self.caption

class Article(models.Model):
    title = models.CharField(max_length=32, unique=True)
    content = models.TextField()
    author = models.CharField(max_length=32)
    category = models.ForeignKey(Category)
    article_tag = models.ForeignKey(ArticleTag)
    def __unicode__(self):
        return self.title

urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
#   <...>       @       _id@ ,       ..objects.update(**kwargs)
    url(r'^index-(?P\d+)-(?P\d+).html', views.index, name='index'),
]

views.py
from django.shortcuts import render
from app01 import models
# Create your views here.

def index(request, *args, **kwargs):
    # print(kwargs)   kwargs     , {'category_id':'1', 'article_tag_id':'2'}
#     id   0,         
    condition = {}
    for k, v in kwargs.items():
        kwargs[k] = int(v)  #kwargs          ,   v   int  ,   urls.py             ,   category_id      int  
        if v == '0':
            pass          #   0 ,      
        else:
            condition[k] = v   #    0   ..

    categorys = models.Category.objects.all()
    article_tags = models.ArticleTag.objects.all()
    articles = models.Article.objects.filter(**condition)
    return render(request, 'index.html', {'articles': articles, 'categorys': categorys, 'article_tags': article_tags, 'kwargs': kwargs})

tamplate-index.html



    
    
    <style>
       .condition a{
            display: inline-block;
            padding: 3px 5px;
            border: 1px solid #dddddd;
            margin: 5px ;
        }
       .condition a.active{
           background-color: red;
       }
    </style>


<div class="condition">
<!--      0,  class="active" -->
{% if kwargs.category_id == 0 %}
    <a class="active" href="#!/index-0-{{ kwargs.article_tag_id }}.html" rel="nofollow">  </a>
{% else %}
    <a href="#!/index-0-{{ kwargs.article_tag_id }}.html" rel="nofollow">  </a>
{% endif %}
<!--            ,   class="active",      -->
{% for c in categorys %}
    {% if c.id == kwargs.category_id %}
        <a class="active" href="#!/index-{{ c.id }}-{{ kwargs.article_tag_id }}.html" rel="nofollow">{{ c }}</a>
    {% else %}
        <a href="#!/index-{{ c.id }}-{{ kwargs.article_tag_id }}.html" rel="nofollow">{{ c }}</a>
    {% endif %}
{% endfor %}

<br/>
<!--   -->
{% if kwargs.article_tag_id == 0 %}
    <a class="active" href="#!/index-{{ kwargs.category_id }}-0.html" rel="nofollow">  </a>
{% else %}
    <a href="#!/index-{{ kwargs.category_id }}-0.html" rel="nofollow">  </a>
{% endif %}

{% for tag in article_tags %}
    {% if tag.id == kwargs.article_tag_id %}
        <a class="active" href="#!/index-{{ kwargs.category_id }}-{{ tag.id }}.html" rel="nofollow">{{ tag }}</a>
    {% else %}
        <a href="#!/index-{{ kwargs.category_id }}-{{ tag.id }}.html" rel="nofollow">{{ tag }}</a>
    {% endif %}
{% endfor %}
<!-- ======================       ,    articles   ========================-->
{% for article in articles %}
    <h2>{{ article.title }}</h2>
    <span>{{ article.author }} {{ article.category.caption }} {{ article.article_tag.caption }}</span>
    <p>{{ article.content }}</p>
    <hr/>
{% endfor %}
</div>


</code></pre> 
</article>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기