如何在Django中使用get_context_data()?

时间:2019-05-08 17:37:19

标签: python django django-views

我正在尝试在博客文章中返回正文的readtime。但是我发现使用get_context_data()

很难做到这一点

这是创建帖子的查看功能:

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)

现在,我要做的是返回“ body”的读取时间

类似这样:

body = request.POST.get('body')
post_read_time = readtime.of_html(body)
post = Post(read_time=post_read_time)
post.save()

问题是如何在我的BlogCreateView类中执行此操作。

我做了一些研究,发现了get_context_data()函数。所以我尝试了这个:

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)

这是我在模板中呈现数据的方式:

<span>{{ post.read_time }}</span>

我希望输出返回读取时间,但我只会使情况变得更糟。我收到此错误:

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

1 个答案:

答案 0 :(得分:1)

我不明白您为什么认为应该在get_context_data内部完成此操作。您似乎想在表单提交过程中创建的Post对象上设置read_time。因此,您应该在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)