我正在尝试创建基于类的详细视图,可以通过单击列表视图项来访问它。问题是在基于函数的视图中很容易实现,但是在基于类的视图中却不能做到这一点。
model.py
func main() {
http.HandleFunc("/", emailListHandler)
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
fmt.Println(http.ListenAndServe(":8080", nil))
}
views.py
from django.db import models
import datetime
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length=500)
writer = models.CharField(max_length=150,default='my dept')
category =models.CharField(max_length=150)
image = models.ImageField(upload_to='images')
post = models.TextField(max_length=2000)
Date = models.DateField( default=datetime.date.today)
def __str__(self):
return self.title
urls.py
from.models import BlogPost , EDITORIAL_RESPONSIBILITIES , Reviewers ,Confrences
,ABSTRACT_IN_CONFERENCES
class BlogList(ListView):
model = BlogPost
template_name = 'blog/bloglist.html'
context_object_name = 'post'
class BlogDetail(DetailView):
model = BlogPost
template_name = 'blogdetail.html'
listview模板运行完全正常。.目录结构也很好。.listviw.html和detail.html都位于template / blog / ..的同一文件夹中。
listview模板
path('list', BlogList.as_view(), name='list'),
path('(?P<id>\d+)/', BlogDetail.as_view())
答案 0 :(得分:0)
确保目录正确
templates
--blog
---bloglist.html
---blogdetail.html
添加此
class BlogDetail(DetailView):
model = BlogPost
template_name = 'blog/blogdetail.html'
def get_context_data(self, *args, **kwargs):
context = super(BlogDetail, self).get_context_data(*args, **kwargs)
post = self.get_object()
context["post"] = post
print(post) #//To check if it's returning the right post
return context
告诉我是否有效