一个url对应多个文章

M

未更改模型

from django.db import models

# Create your models here.
class People(models.Model):
    name = models.CharField(null=True, blank=True,max_length=200)
    job = models.CharField(null=True, blank=True, max_length=200)
    def __str__(self):
        return self.name

class Article(models.Model):
    headline = models.CharField(null=True, blank=True,max_length=500)
    content = models.TextField(null=True, blank=True)
    TAG_CHOICES = (
        ('tech', 'Tech'),
        ('life','Life'),
    )
    tag = models.CharField(null=True, blank=True, max_length=5, choices=TAG_CHOICES)
    def __str__(self):
        return self.headline

class Comment(models.Model):
    name = models.CharField(null=True, blank=True, max_length=50)
    comment = models.TextField()
    def __str__(self):
        return self.comment

U

from django.conf.urls import url
from django.contrib import admin
from firstapp.views import index,detail

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index', index, name='index'),#url(‘url地址’,函数名,查找方便名)
    url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'),
    #新增
    #url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'),
    #(?P<page_num>\d+)------把\d+命名为?P<page_num>【page_num为数据库默认文章id】
]

V

from django.shortcuts import render, HttpResponse, redirect
from firstapp.models import Article, Comment
from django.template import Context, Template
from firstapp.form import CommentForm

# Create your views here.

def index(request):
    print(request)
    print('==='*30)
    print(dir(request))
    print('==='*30)
    print(type(request))
    queryset = request.GET.get('tag')
    if queryset:
        article_list = Aritcle.objects.filter(tag=queryset)
    else:
        article_list = Article.objects.all()
    context = {}
    context['article_list'] = article_list
    index_page = render(request, 'first_web_2.html', context)
    return index_page

def detail(request, page_num):#新增参数page_num
    if request.method == 'GET':
        form = CommentForm
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            comment = form.cleaned_data['comment']
            c = Comment(name=name, comment=comment)
            c.save()
            return redirect(to='detail')
    context = {}
    comment_list = Comment.objects.all()
    article = Article.objects.get(id=page_num)#从数据库中找到id=page_num的文章
    context['article'] = article#装入context上下文中
    context['comment_list'] = comment_list
    context['form'] = form
    return render(request, 'article_detail.html', context)

T

article_detail.html
在文章详情页添加文章标题变量{{ article.headline }}和文章内容变量{{ article.content }}

<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
    <link href="https://fonts.googleapis.com/css?family=0swald|Raleway" rel="stylesheet">

    <style type="text/css">
        .ui.segment.container{
            width:700px;
        }
        p {
            font-family:'Raleway', sans-serif;
            font-size:18px;
        }
        body {
            background: url({% static 'images/star_banner.jpg' %});
            background-size:cover;
            background-repeat:no-repeat;
            background-attachment:fixed
        }
    </style>
</head>
<body>
    <div class="ui image">
        <img src="" alt="" />
    </div>
    <div class="ui segment padded container">
        <h1 class="ui header" style="font-family:'Oswald',sans-serif;font-size: 40px">
            {{ article.headline }}
        </h1>
        <p>
            {{ article.content }}
        </p>
    </div>
    <div class="ui segment container" style="width:700px;">
        <h3 class="ui header" style="font-family: 'Oswald', sans-serif;">Comments</h3><!--标题Comment-->
        <div class="ui comments"><!--评论-->
            {% for comment in comment_list %}
                <div class="comment">
                    <div class="avatar">
                        <img src="http://semantic-ui.com/images/avatar/small/matt.jpg" alt="" />
                    </div>
                    <div class="content">
                        <a href="#" class="author">{{ comment.name }}</a>
                        <div class="metadata">
                            <div class="date">2 days ago</div>
                        </div>
                        <p class="text" style="font-family:'Raleway', sans-serif;">
                            {{ comment.comment }}
                        </p>
                    </div>
                </div>
            {% endfor %}
        </div>
        <div class="ui divider"></div><!--分割线评论-->
        <form class="ui error tiny form" method="post">
            {% if form.errors %}
                <div class="ui error message">
                    {{ form.errors }}
                </div>

                {% for field in form %}
                    <div class="{{ field.errors|yesno:'error, '}} field"><!--#依次判断表格中的field是否有error-->
                    {{ field.label }}
                    {{ field }}
                    </div>
                {% endfor %}
            {% else %}
                {% for field in form %}
                    <div class="field">
                        {{ field.label }}
                        {{ field }}
                    </div>
                {% endfor %}
            {% endif %}
            {% csrf_token %}<!--Django防跨站"{%csrf_token%}"标签-->
            <button type="submit" class="ui blue button">Click</button>
        </form>
    </div>

</body>
</html>

first_web_2.html
在列表页面添加每个文章的链接href=”{% url ‘detail’ article.id %}”

{% load staticfiles %}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>first web</title>
        <link rel="stylesheet" href="{% static 'css/semantic.css' %}"  media="screen" title="no title" charset="utf-8">
        <link href="https://fonts.googleapis.com/css?family=Oswald|Raleway" rel="stylesheet">


        <style type="text/css">
            h1 {
                font-family:'Oswald', sans-serif!important;
                font-size:40px;
            }

            body {
                font-family: 'Raleway', sans-serif;
            }
            p {
                font-family: 'Raleway', sans-serif;
                font-size:18px;
            }
            .ui.vertical.segment.masthead {
                height: 300px;
                background-image: url({% static 'images/star_banner.jpg' %});
                background-size: cover;
                background-position: 100% 80%;
            }

            .ui.container.segment {
                width: 800px;
            }

            .ui.center.aligned.header.blogslogon {
                margin-top: 40px;
            }

            .ui.center.aligned.header.blogslogon p {
                margin-top: 10px;
                color: white;
                font-size: 10px;
            }
            .ui.container.nav {
                width: 500px;
            }

        </style>
    </head>
    <body>
        <div class="ui inverted vertical  segment masthead">

            <h1 class="ui center aligned header blogslogon" style="font-size:50px;font-family: 'Raleway', sans-serif!important;">
                Bloger
                <p class="ui sub header">
                    everyone has a story to tell
                </p>

            </h1>
        </div>
        <div class="ui container nav">
            <div class="ui borderless text three item menu ">
                <div class="ui simple dropdown  item">
                    Categories
                    <i class="dropdown icon"></i>
                    <div class="menu">
                        <a class="item" href="?tag=life">life</a>
                        <a class="item" href="?tag=tech">tech</a>
                    </div>
                </div>
                <a class="item">
                    Popular
                </a>
                <a class="item">
                    About
                </a>

            </div>
        </div>

        <div class="ui divider"></div>

        <div class="ui  vertical segment">
            {% for article in article_list %}
                <div class="ui container vertical segment">
                    <a href="{% url 'detail' article.id %}">
                        <h1 class="ui header">
                            {{ article.headline }}
                        </h1>
                    </a>

                    <i class="icon grey small unhide">10,000</i>
                    <p>
                        {{ article.content|truncatewords:100 }}
                        <a href="{% url 'detail' article.id %}">
                            <i class="angle tiny double grey right icon">READMORE</i>
                        </a>
                    </p>

                    <div class="ui mini  tag label">
                        {{ article.tag }}
                    </div>
                </div>
            {% endfor %}


        </div>

        <div class="ui inverted  vertical very padded  segment">
            Mugglecoding®
        </div>
    </body>
</html>

运行顺序是

T点击页面的href="{% url 'detail' article.id %}"http://127.0.0.1:8000/detail/2

U通过url索引找到url(r'^detail/(?P<page_num>\d+)$', detail, name='detail'),
把detail后面的\d+数字传入并命名为page_num变量

V 传入page_num【即前端页面的article.id,在U层被命名为page_num】找到指定文章

def detail(request, page_num):
    article = Article.objects.get(id=page_num)
    context['article'] = article
    return render(request, 'article_detail.html', context)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Scrapy-Redis框架的介绍与应用 摘要: Scrapy-Redis框架是Scrapy爬虫框架的一个扩展,它使用Redis数据库作为Scrapy的调度器和数据存储。本文介绍了Scrapy-Redis框架的基本原理、架构和应用,详细阐述了其在分布式爬虫、数据存储和增量式爬虫等方面的优势,并通过实例说明了如何使用Scrapy-Redis框架进行爬虫开发。 关键词:Scrapy-Redis,分布式爬虫,数据存储,增量式爬虫 一、Scrapy-Redis框架的介绍 Scrapy-Redis框架是Scrapy爬虫框架的一个扩展,它使用Redis数据库作为Scrapy的调度器和数据存储。Scrapy-Redis框架可以在分布式环境下运行,使多台服务器能够同时进行爬取任务,大大提高了爬取效率。此外,Scrapy-Redis框架还支持增量式爬虫,可以根据之前爬取的结果来更新数据,避免重复爬取。 Scrapy-Redis框架主要包含以下组件: 1. Scrapy-Redis调度器 Scrapy-Redis调度器使用Redis的list数据结构实现,每个爬虫任务对应一个Redis列表,爬虫任务被分为多个URL请求,每个请求对应一个Redis列表元素。Scrapy-Redis调度器通过阻塞读取Redis列表来获取待爬取的URL,保证多个爬虫任务之间的URL请求不会重复。 2. Scrapy-Redis去重器 Scrapy-Redis去重器使用Redis的set数据结构实现,对每个爬虫任务的URL进行去重。Scrapy-Redis去重器可以避免重复爬取相同的URL,提高爬取效率。 3. Scrapy-Redis管道 Scrapy-Redis管道使用Redis的hash数据结构实现,将爬取的数据存储到Redis中。Scrapy-Redis管道支持多种数据格式,如json、xml、csv等,可以灵活处理不同类型的数据。 二、Scrapy-Redis框架的应用 1. 分布式爬虫 Scrapy-Redis框架使用Redis作为调度器和数据存储,可以很容易地实现分布式爬虫。多台服务器可以同时运行Scrapy-Redis爬虫,通过Redis列表来共享URL请求和爬取结果。此外,Scrapy-Redis还提供了分布式锁机制,保证多个爬虫任务之间的URL请求不会重复,避免数据重复爬取。 2. 数据存储 Scrapy-Redis框架可以将爬取的数据存储到Redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值