ImportError:无法从“ main.views”导入名称“ PostListView”

时间:2019-07-18 13:49:45

标签: python django

大家好,我是学习Django的初学者,尝试从.views导入PostListView时遇到此错误

此项目的网址:

from django.contrib import admin
from django.urls import path, include
from users import views as user_views
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', user_views.register, name='register'),
    path('profile/', user_views.profile, name='profile'),
    path('/', include('main.urls')),
    path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的应用主网址:

from . import views
from django.urls import path
from main.views import PostListView


urlpatterns = [
    path('', PostListView.as_view, name="blog"),
    path('about/', views.about, name='about'),
]

这是我的观点:

from django.shortcuts import render
from .models import Post
from django.views.generic import ListView


def blog(request):
     context = {
         'posts': Post.objects.all()
     }
     return render(request=request,
                   template_name='main/blog.html',
                   context=context)


class PostListViews(ListView):
    model = Post


def about(request):
    return render(request=request,
                  template_name='main/about.html')

预先感谢您:)

1 个答案:

答案 0 :(得分:2)

您的班级名称被定义为PostListViews,而不是PostListVew。您需要更改类名称或导入的名称。