简体   繁体   中英

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()

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'

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.

I made some research and came across the get_context_data() function. 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 . You appear to want to set the read_time on the Post object that you're creating as part of the form submission. So you should do that inside form_valid , just like you set the user.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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