Python(自定义url、筛选1)

views
 

from django.shortcuts import render
from app01 import models
# Create your views here.
def article(request,*args,**kwargs):
    print(kwargs)
    condition = {}
    for k,v in kwargs.items():
        kwargs[k] = int(v)
        if v == '0':
            pass
        else:
            condition[k] = v
    article_type_list = models.ArticleType.objects.all()
    category_list = models.Category.objects.all()
    # c = {'article_type_id': '1', 'category_id': '2'}
    result = models.Article.objects.filter(**kwargs)
    return render(
        request,
        'article.html',
        {
            'result':result,
            'article_type_list':article_type_list,
            'category_list':category_list,
        }
    )

models
 

from django.db import models
# Create your models here.
from django.db import models
# Create your models here.
class Category(models.Model):
    caption = models.CharField(max_length=16)
class ArticleType(models.Model):
    caption = models.CharField(max_length=16)
class Article(models.Model):
    title = models.CharField(max_length=32)
    content = models.CharField(max_length=255)
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    article_type = models.ForeignKey(ArticleType,on_delete=models.CASCADE)
    # type_choice = (
    #     (1,'Python'),
    #     (2,'OpenStack'),
    #     (3,'Linux'),
    # )
    # article_type_id = models.IntegerField(choices=type_choice)

urls

"""s14day25 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('article--/', views.article),
]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .condition a{
            display: inline-block;
            padding: 3px 5px;
            border: 1px solid #dddddd;
            margin: 5px;
        }
    </style>
</head>
<body>
        <h1>过滤条件</h1>
        <div class="condition">
            <div>
                <a>全部</a>
                {% for row in article_type_list %}
                    <a>{{ row.caption }}</a>
                {% endfor %}
            </div>
            <div>
                <a>全部</a>
                {% for row in category_list %}
                    <a>{{ row.caption }}</a>
                {% endfor %}
            </div>
        </div>
        <h1>查询结果</h1>
        <ul>
        {% for row in result %}
            <li>{{ row.id }}-{{ row.title }}</li>
        {% endfor %}
        </ul>
</body>
</html>

说点什么

avatar
  Subscribe  
提醒