繁体   English   中英

如何在Django中使用get_context_data()?

[英]How to use get_context_data() in Django?

我正在尝试在博客文章中返回正文的读取时间。 但是我发现使用get_context_data()很难做到这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类中执行此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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM