[英]How to use get_context_data() in Django?
I'm trying to return the readtime of the body in a blog post. 我正在尝试在博客文章中返回正文的读取时间。 But I'm finding it difficult to do so with
get_context_data()
但是我发现使用
get_context_data()
很难做到这get_context_data()
This is the view function to create a post: 这是创建帖子的视图功能:
class BlogCreateView(LoginRequiredMixin, CreateView):
model = Post
template_name = 'blog/creator/new_post.html'
fields = ['title', 'category', 'slug', 'body']
login_url = 'login'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
Now, what I want to do is return the readtime for 'body' 现在,我要做的是返回“ body”的读取时间
something like this: 像这样的东西:
body = request.POST.get('body')
post_read_time = readtime.of_html(body)
post = Post(read_time=post_read_time)
post.save()
The problem is how to do this in my BlogCreateView
class. 问题是如何在我的
BlogCreateView
类中执行此BlogCreateView
。
I made some research and came across the get_context_data()
function. 我进行了一些研究,并遇到了
get_context_data()
函数。 So I tried this: 所以我尝试了这个:
class BlogCreateView(LoginRequiredMixin, CreateView):
model = Post
template_name = 'blog/creator/new_post.html'
fields = ['title', 'category', 'slug', 'body']
login_url = 'login'
post_read_time = ''
def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data['body'] = readtime.of_html('body')
c = Post.objects.filter(read_time=context_data)
return context_data
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
This is how I render the data in my template: 这就是我在模板中呈现数据的方式:
<span>{{ post.read_time }}</span>
I expect the output to return the read time but I've only made things worse. 我希望输出返回读取时间,但我只会使情况更糟。 I'm getting this error instead:
我收到此错误:
TypeError at /blog/post/new-post66/
{'object': <Post: We Should All Be Farmers>, 'post': <Post: We Should All Be Farmers>, 'view': <blog.views.PostDetailView object at 0x042D5590>, 'body': <QuerySet [<Post: We Should All Be Farmers>, <Post: We Should All Be Farmers>]>}
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/post/new-post66/
Django Version: 2.0
Exception Type: TypeError
Exception Value:
{'object': <Post: We Should All Be Farmers>, 'post': <Post: We Should All Be Farmers>, 'view': <blog.views.PostDetailView object at 0x042D5590>, 'body': <QuerySet [<Post: We Should All Be Farmers>, <Post: We Should All Be Farmers>]>}
Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\pyquery\pyquery.py in __init__, line 266
Python Executable: C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.0
Python Path:
['C:\\Projects\\project\\django-personal-website',
'C:\\Projects\\project\\django-personal-website',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\user\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\win32',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\win32\\lib',
'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\Pythonwin']
Server time: Wed, 8 May 2019 17:24:18 +0000
I don't understand why you think this should be done inside get_context_data
. 我不明白为什么您认为应该在
get_context_data
内部完成此操作。 You appear to want to set the read_time on the Post object that you're creating as part of the form submission. 您似乎想在表单提交过程中创建的Post对象上设置read_time。 So you should do that inside
form_valid
, just like you set the user. 因此,您应该在
form_valid
内执行此form_valid
,就像设置用户一样。
def form_valid(self, form):
form.instance.author = self.request.user
form.instance.read_time = readtime.of_html(form.cleaned_data['body'])
return super().form_valid(form)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.