Python(自定义url、筛选 2)

article.html

<!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;
        }
        .condition a.active {
            background-color: black;
        }
    </style>
</head>
<body>
<h1>过滤条件</h1>
<div class="condition">
    <div>
        {% if arg_dict.article_type_id == 0 %}
            <a class="active" href="/article-0-{{ arg_dict.category_id }}/">全部</a>
        {% else %}
            <a href="/article-{{ arg_dict.category_id }}-0/">全部</a>
        {% endif %}
        {% for row in article_type_list %}
            {% if row.id == arg_dict.article_type_id %}
                <a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}/">{{ row.caption }}</a>
            {% else %}
                <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}/">{{ row.caption }}</a>
            {% endif %}
        {% endfor %}
    </div>
    <div>
        {% if arg_dict.category_id == 0 %}
            <a class="active" href="/article-{{ arg_dict.article_type_id }}-0/">全部</a>
        {% else %}
            <a href="/article-{{ arg_dict.article_type_id }}-0/">全部</a>
        {% endif %}
        {% for row in category_list %}
            {% if row.id == arg_dict.category_id %}
                <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}/">{{ row.caption }}</a>
            {% else %}
                <a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}/">{{ row.caption }}</a>
            {% endif %}
        {% endfor %}
    </div>
</div>
<h1>查询结果</h1>
<ul>
    {% for row in result %}
        <li>{{ row.id }}-{{ row.title }}</li>
    {% endfor %}
</ul>
</body>
</html>

views

from django.shortcuts import render
from app01 import models
# Create your views here.
def article(request, *args, **kwargs):
    print(kwargs)
    # print(request.path_info) # 获取当前URL
    # from django.urls import reverse
    # # {'article_type_id': '0', 'category_id': '0'}
    # url = reverse('article',kwargs={'article_type_id': '1', 'category_id': '0'})
    # print(url)
    # print(kwargs) # {'article_type_id': '0', 'category_id': '0'}
    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,
            'arg_dict': kwargs
        }
    )

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, name='article'),
]

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)

setting

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

说点什么

avatar
  Subscribe  
提醒