我正在尝试以轮播方式发布具有类别特色视图的帖子,而其余部分以不同的视图发布。有可能做到这一点。 something like on this website
到目前为止,我已经将帖子发布为主页上的类别,但是在这里我也希望通过帖子来制作轮播,而且我不知道该怎么做。
这是我的代码views.py
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
#from django.contrib.auth.models import User
# Create your views here.posts = [
from django.shortcuts import redirect, render,get_object_or_404
#class based view
from django.views.generic import ListView, DetailView
from .models import Post, Category
class PostView(ListView):
template_name = 'posts/home.html'
model = Category
context_object_name = 'all_categs'
def get_queryset(self):
if self.request.user.is_authenticated:
return Category.objects.all()
else:
return Category.objects.all()[:6]
def get_context_data(self):
if not self.request.user.is_authenticated:
context = super(PostView, self).get_context_data()
context['latest_posts'] = Post.objects.order_by('-date_posted')[0:6]
return context
else:
context = super(PostView, self).get_context_data()
context['latest_posts'] = Post.objects.order_by('-date_posted')
return context
# def get_success_url(self):
# return reverse('home') #add your path
class PostDetailView(LoginRequiredMixin,DetailView):
model = Post
template_name = 'posts/post_detail.html'
home.html我没有在此处添加轮播,但我想在首页的顶部添加轮播。
{% extends "posts/base.html" %}
{% block content %}
<div class="contianer">
<div class="latest_post-title row">
<div class="col-lg-6">
<h3>Latest</h3>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="col-lg-6">
<a class="float-right" href="#">See More</a>
</div>
<div><!--latest_Post-title-->
</div>
<div class="container latest_post-wrapper py-1">
<div class="row py-3">
{% for post in latest_posts %}
<div class="col-lg-4" style="padding-bottom: 2rem;">
<a href="{{post.get_absolute_url}}">
<div class="card p-2 shadow" style="width: 22rem; height: 30rem;">
<img src="{{post.image.url}}" class="card-img-top" alt="{{post.title}}">
<div class="card-body">
<small class="text-muted h6">{{post.category}}</small>
<blockquote class="blockquote">
<p class="mb-0">{{post.title}}</p>
<footer class="blockquote-footer">written By<cite title="Source Title">{{posts.author}}</cite></footer>
</blockquote>
<hr/>
<div class="d-flex flex-row py-0">
<small class="text-muted mr-auto">{{post.date_posted}}</small>
<p><i class="fas fa-share-alt"></i></p>
</div>
</div>
</div>
</a>
</div><!--col-lg-4-->
{% endfor %}
</div><!--row-py-3-->
</div><!--container.latest_post-wrapper-->
{% for ct in all_categs %}
<div class="container latest_post-wrapper py-1">
<div class="latest_post-title row">
<div class="col-lg-6">
<h3>{{ct.title}}</h3>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="col-lg-6">
<a class="float-right" href="#">See More</a>
</div>
<div><!--latest_Post-title-->
<div class="row py-3">
{% for post in ct.postcategory.all %}
<div class="col-lg-4" style="padding-bottom: 2rem;" >
<a href="{{post.get_absolute_url}}">
<div class="card p-2 shadow" style="width: 22rem; height: 30rem">
<img src="{{post.image.url}}" class="card-img-top" alt="...">
<div class="card-body">
<small class="text-muted h6">{{post.category}}</small>
<blockquote class="blockquote">
<p class="mb-0">{{post.title}}</p>
<footer class="blockquote-footer">written By<cite title="Source Title">{{posts.author}}</cite></footer>
</blockquote>
<hr/>
<div class="d-flex flex-row py-0">
<small class="text-muted mr-auto">{{post.date_posted}}</small>
<p><i class="fas fa-share-alt"></i></p>
</div>
</div>
</div>
</a>
</div><!--col-lg-4-->
{% endfor %}
</div><!--row-py-3-->
</div><!--container.latest_post-wrapper-->
{% endfor %}
{% endblock content %}}