你好,我是Django的新手,所以我决定创建一个Blog页面。问题是在创建新的应用后,我的查询集为空。知道为什么吗?
尝试启用和禁用。
views.py
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView
from django.http import Http404
from .models import BlogPost
class BlogPostListView(ListView):
queryset = BlogPost.objects.all().active()
template_name = "blog.html"
def get_queryset(self, *args, **kwargs):
request = self.request
return BlogPost.objects.all().active()
def BlogPost_list_view(request):
queryset = BlogPost.objects.all().active()
context = {
'object_blog': queryset
}
return render(request, "blog.html", context)
models.py
import random
import os
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.urls import reverse
def get_filename_ext(filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(base_name)
return name, ext
def upload_image_path(instance, filename):
print(instance)
print(filename)
new_filename = random.randint(1,18341264712)
name, ext = get_filename_ext(filename)
final_filename = '{new_filename}{ext}'.format(new_filename= new_filename, ext=ext)
return "products/{new_filename}/{final_filename}".format(
new_filename= new_filename,
final_filename=final_filename
)
class BlogPostQuerySet(models.query.QuerySet):
def active(self):
return self.filter(active=True)
def featured(self):
return self.filter(featured=True, active=True)
class BlogPostManager(models.Manager):
def get_queryset(self):
return BlogPostQuerySet(self.model, using=self._db)
def all(self):
return self.get_queryset()
class BlogPost(models.Model):
title = models.CharField(max_length=120)
slug = models.SlugField(blank=True, unique=True)
description = models.TextField()
image = models.ImageField(upload_to=upload_image_path, null=True, blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=True)
objects = BlogPostManager()
def get_absolute_url(self):
return "{slug}/".format(slug=self.slug)
# return reverse("products:detail", kwargs={"slug": self.slug})
def __str__(self):
return self.title
def __unicode__(self):
return self.title
urls.py
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from .views import BlogPostListView, BlogPostDetailSlugView
urlpatterns = [
url(r'^$', BlogPostListView.as_view(), name='list'),
url(r'^(?P<slug>[-\w]+)/$', BlogPostDetailSlugView.as_view(), name='detail'),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
admin.py
from django.contrib import admin
from .models import BlogPost
class BlogPostAdmin(admin.ModelAdmin):
list_display = ['__str__', 'slug']
class Meta:
model = BlogPost
admin.site.register(BlogPost, BlogPostAdmin)
blog.html
{% for obj in object_blog %}
<!-- Blog Post -->
<div class="card mb-4 text-white bg-dark">
{% if obj.image %}
<!-- {{MEDIA_URL}} -->
<img class="card-img-top" src="{{ obj.image.url }}" alt="Card image cap">
{% else %}
<h1> No pic </h1>
{% endif %}
<div class="card-body">
<h2 class="card-title">{{ obj.title }}</h2>
<p class="card-text">{{ obj.description|slice:":255" }} ...</p>
<a href="#" class="btn btn-primary">Read More →</a>
</div>
<div class="card-footer text-muted">
{{ obj.timestamp }}
<a href="#">Start Bootstrap</a>
</div>
</div>
{% endfor %}
我使用的模式几乎与我在不同应用程序中使用的模式相同,在所有应用程序中一切正常。几天前,我注意到Django出现了某种奇怪的行为,因为它确实想向我展示网站上的图像,直到我完全重启PC。如果有人需要更多信息/查看一些不同的文件,我可以提供更多。这是我的Django管理员提供的几张照片。
答案 0 :(得分:0)
您的两个视图matrix <- df[,3:6]
braycurtis = vegdist(matrix, "bray")
hist(braycurtis)
和BlogPostListView
几乎做同一件事,我不知道您为什么有两个视图。但是请注意,第一个是您的URL实际使用的那个。
现在,该视图是基于类的视图,它将向模板发送名为BlogPost_list_view
或blog_list
的变量。但是,模板本身正在迭代名为object_list
的变量,该变量将由(未使用的)第二个视图发送。您应将其更改为object_blog
-或在视图中添加object_list
(但我不建议这样做)。