Home>

Please tell me and explain. Do not judge strictly -I am a completely newbie and do not understand much yet.

Established:

  • Operating system: openSUSE Tumbleweed 20211227
  • KDE Plasma version: 5.23.4
  • KDE Frameworks version: 5.89.0
  • Qt version: 5.15.2
  • Kernel version: 5.15.8-1-default (64-bit)
  • Graphics platform: X11
  • Processors: 2 × AMD A4-6300 APU with Radeon (tm) HD Graphics
  • Memory: 13.6 GiB RAM
  • GPU: AMD ARUBA

If I use classes, I get the error:

  • Method Not Allowed (GET): /posts /
  • Method Not Allowed: /posts /
  • [29 /Dec /2021 23:16:04] "GET /posts /HTTP /1.1" 405 0

If I use functions and remove as_view (), then everything works. What's my mistake?

views.py

from django.shortcuts import render
from django.views.generic.base import View
# Create your views here.
from .models import post_class
class index_views (View):
    def index (self, request):
        return render (request, 'main_app /index.html')
class post_views (View):
    def get_post (self, request):
        posts= post_class.objects.all ()
        return render (request, 'main_app /posts_list.html', {'posts_list': posts})
def index (request):
    posts= post_class.objects.all ()
    return render (request, 'main_app /index.html')
def post (request):
    posts= post_class.objects.all ()
    return render (request, 'main_app /posts_list.html', {'posts_list': posts})

urls.py

from django.urls import path
from. import views
urlpatterns= [
    path ('index /', views.index, name= 'index'),
    path ('posts /', views.post, name= 'posts'),
    path ('index_views /', views.index_views.as_view (), name= 'index_views'),
    path ('posts_views /', views.post_views.as_view (), name= 'posts_views'),
]

index.html

<
meta charset= "UTF-8" >
<
title >
Home <
/title >
 <
/head >
 <
body >
 <
br >
Home page (index.html) <
br >
 {% block content%} {% endblock%} <
/body >
 <
/html >

posts_list.html

{% extends 'main_app /index.html'%}
{% block content%}
    {% for post in posts_list%}
    {{post.post_title}}
    {% endfor%}
{% endblock%}

models.py

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
# Create your models here.
from django.contrib.auth.models import User
from django.db.models.fields import SlugField
from django_ckeditor_5.fields import CKEditor5Field
class category_class (MPTTModel):
    category_name= models.CharField (max_length= 150, unique= True)
    category_url= models.SlugField (max_length= 150, unique= True)
    parent= TreeForeignKey (
        'self',
        related_name= 'category_children_related',
        on_delete= models.SET_NULL,
        null= True,
        blank= True
    )
    def __str __ (self):
        return self.category_name
class tag_class (models.Model):
    tag_name= models.CharField (verbose_name= 'Tag', max_length= 150, unique= True)
    tag_slug= SlugField (max_length= 150, unique= True)
    def __str __ (self):
        return self.tag_name
class post_class (models.Model):
    post_title= models.CharField (verbose_name= 'Title (150)', max_length= 150)
    post_content= CKEditor5Field (verbose_name= 'Post', config_name= 'extends')
    post_author= models.ForeignKey (User, related_name= 'post_author_related', verbose_name= 'Author name (50 characters)', on_delete= models.SET_NULL,
                                    null= True)
    public_date= models.DateTimeField (verbose_name= 'Date published', auto_now_add= True)change_date= models.DateTimeField (verbose_name= 'Date of publication', auto_now= True) post_category= models.ForeignKey (category_class, verbose_name= 'Category', related_name= 'post_category_related', on_delete= models.SET_NULL,
                                      null= True)
    post_tags= models.ManyToManyField (tag_class, verbose_name= 'Tags', related_name= 'post')
    post_url= models.SlugField (max_length= 150, null= True, blank= False, unique= True)
    on_moderation= models.BooleanField (verbose_name= 'On moderation', default= True)
    def __str __ (self):
        return self.post_title

path ('posts /', views.post, name= 'posts') -where is your posts function?

strawdog2021-12-30 05:54:29

@strawdog figured it out. the name of the function in the class should be called get, and not like mine -in another way.

Вадим Соловьев2021-12-30 11:17:43