Search code examples
pythondjangonameerror

Name Error by python, name 'ArticleDetailView' is not defined


This is the problem I have:

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

my urls.py:

    from django.urls import path
    from .views import HomeView

    urlpatterns = [
        path('article/<int:pk>',ArticleDetailView.as_view(), name='article-detail'),
    ]

In my views.py I have clearly defined it, below is my 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'

How do I solve this?


Solution

  • You need to specifically import ArticleDetailView as well:

    from .views import HomeView, ArticleDetailView
    

    Generally if you use a decent IDE, the software will show you that you are missing imports and will make a suggestion on how to solve it.