Issue with Django blog tutorial on youtube, class view and GET method not clear

Was following a youtube tut regarding a django blog but ran into an issue. Link to video: https://www.youtube.com/watch?v=CnaB4Nb0-R8&t=607s The tut says to use the following code in the blog app's view.py file:

from django.views.generic. import ListView, DetailView
from .models import Post
from django.views import View

class Blogview(ListView):
    model = Post
    template_name = 'blog.html'

When I tried to implement the code this way, I was given an error referencing something to do with GET. Looking into it, I modified my code to look like the following:

from django.shortcuts import render
from .models import Post
from django.views import View

class Blogview(View):
    model = Post
    template_name = 'blog.html'
    def get(self, request):
        return render(request,'blog.html')

The above version of code works now for me. I find it frustrating why I couldn't figure out why the youtube tutorial code seemed to be much simlified and worked just as expected, whereas when I tried it word for word it failed and had to include a reference to the get method.

I'll let you know I did simplify my version of the template file 'blog.html' to be something as simple as a single line of text whereas the tutorial had a template file which took all the posts stored in the database, iterated through them and listed them. Am I wrong to assume that this should not have been the reason his version of code worked but when I wrote his code on my machine it did not, having to be modified to include get method?

Hope all is clear. any questions welcome. thanks



Read more here: https://stackoverflow.com/questions/68433088/issue-with-django-blog-tutorial-on-youtube-class-view-and-get-method-not-clear

Content Attribution

This content was originally published by Not Applicable at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: