python 的名称错误,名称 'ArticleDetailView' 未定义
Name Error by python, name 'ArticleDetailView' is not defined
这是我遇到的问题:
File "C:\simpleblog\ablog\myblog\urls.py", line 8, in <module>
path('article/<int:pk>',ArticleDetailView.as_view(), name='article-detail'),
NameError: name 'ArticleDetailView' is not defined
我的urls.py:
from django.urls import path
from .views import HomeView
urlpatterns = [
path('article/<int:pk>',ArticleDetailView.as_view(), name='article-detail'),
]
在我的views.py里面已经明确定义了,下面是我的views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
class HomeView(ListView):
model = Post
template_name = 'home.html'
class ArticleDetailView(DetailView):
model = Post
template_name = 'article_details.html'
我该如何解决?
您还需要专门导入 ArticleDetailView
:
from .views import HomeView, ArticleDetailView
一般情况下,如果你使用得当IDE,软件会告诉你缺少导入,并给出解决方法的建议。
这是我遇到的问题:
File "C:\simpleblog\ablog\myblog\urls.py", line 8, in <module>
path('article/<int:pk>',ArticleDetailView.as_view(), name='article-detail'),
NameError: name 'ArticleDetailView' is not defined
我的urls.py:
from django.urls import path
from .views import HomeView
urlpatterns = [
path('article/<int:pk>',ArticleDetailView.as_view(), name='article-detail'),
]
在我的views.py里面已经明确定义了,下面是我的views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
class HomeView(ListView):
model = Post
template_name = 'home.html'
class ArticleDetailView(DetailView):
model = Post
template_name = 'article_details.html'
我该如何解决?
您还需要专门导入 ArticleDetailView
:
from .views import HomeView, ArticleDetailView
一般情况下,如果你使用得当IDE,软件会告诉你缺少导入,并给出解决方法的建议。