Djnago : AttributeError: module 'django.views.generic' has no attribute 'Detail' - python

where can I import the detail attribute from?
views.py:
from django.shortcuts import render
from django.views import generic
from . import models
class Index(generic.TemplateView):
template_name='catalog/index.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['num_books'] = models.Book.objects.all().count()
context['num_instances'] = models.BookInstance.objects.all().count()
context['num_instances_available'] = models.BookInstance.objects.filter(status__exact='a').count()
context['num_authors'] = models.Author.objects.count()
return context
class BookListView(generic.ListView):
model = models.Book
template_name = 'catalog/book_list.html'
class BookDetialView(generic.Detail.View):
model= models.Book
template_name = 'catalog/book_detail.html'
urls.py
from django.urls import include, path, re_path
from . import views
from django.views.generic import TemplateView
app_name='catalog'
urlpatterns = [
path(r'', views.Index.as_view(), name='index'),
re_path(r'^$', views.Index.as_view(), name='index')
]
urlpatterns = [
path(r'^$', views.index, name='index'),
path(r'^books/$', views.BookListView.as_view(), name='books'),
path(r'^book/(?P<pk>\d+)$', views.BookDetailView.as_view(), name='book-detail'),
]
but the result:
class BookDetialView(generic.Detail.View):
AttributeError: module 'django.views.generic' has no attribute 'Detail'

Should be generic.DetailViewinstead of generic.Detail.View

In your BookDetailView you used generic.Detail.View instead of DetailView[Djano-doc].
Change your BookDetailView as
from django.views import generic
class BookDetialView(generic.DetailView): #<--- change here
model= models.Book
template_name = 'catalog/book_detail.html'

You have a couple of issues here.
(1) In your views you spell your view name as BookDetialView and in your url definition you spell it BookDetailView.
(2) I believe the correct inheritance is generic.DetailView instead of generic.Detail.View

Related

How do i display django class based view api on bootstrap template?

I am working on a photography website and i have created a rest api for blog but i am facing trouble how to display the api on template.
My project structure
enter image description here
This is my view.py of Blog
from rest_framework.response import Response
from rest_framework import permissions
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView, RetrieveAPIView
from blog.models import BlogPost
from blog.serializers import BlogPostSerializer
from rest_framework.renderers import TemplateHTMLRenderer
class BlogPostListView(ListAPIView):
queryset = BlogPost.objects.order_by('-date_created')
serializer_class = BlogPostSerializer
lookup_field = 'slug'
permission_classes = (permissions.AllowAny, )
class BlogPostDetailView(RetrieveAPIView):
queryset = BlogPost.objects.order_by('-date_created')
serializer_class = BlogPostSerializer
lookup_field = 'slug'
permission_classes = (permissions.AllowAny, )
class BlogPostFeaturedView(ListAPIView):
queryset = BlogPost.objects.all().filter(featured=True)
serializer_class = BlogPostSerializer
lookup_field = 'slug'
permission_classes = (permissions.AllowAny, )
class BlogPostCategoryView(APIView):
serializer_class = BlogPostSerializer
permission_classes = (permissions.AllowAny, )
def post(self, request, format=None):
data = self.request.data
category = data['category']
queryset = BlogPost.objects.order_by('-date_created').filter(category__iexact=category)
serializer = BlogPostSerializer(queryset, many=True)
return Response(serializer.data)
This is urls.py of Blog
from django.urls import path
from .views import BlogPostListView, BlogPostDetailView, BlogPostFeaturedView, BlogPostCategoryView
urlpatterns = [
path('', BlogPostListView.as_view(), name='bl'),
path('featured', BlogPostFeaturedView.as_view()),
path('category', BlogPostCategoryView.as_view()),
path('<slug>', BlogPostDetailView.as_view()),
]
This urls.py of application
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from portfolio import urls, views
from blog import urls
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name = 'index'),
path('portfolio/', include('portfolio.urls')),
path('gallery/', views.gallery, name = 'gallery'),
path('shop/', views.shop, name = 'shop'),
path('photo/<str:pk>', views.viewPhoto, name = 'photo'),
path('add/', views.addPhoto, name = 'add'),
path('api-auth/', include('rest_framework.urls')),
path('summernote/', include('django_summernote.urls')),
path('blog/', include('blog.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Rest api for Blog is working, how do use the rest api to display on blog.html??
enter image description here

Django reverse_lazy improperly configured

Under my once my user has signed up I would like to redirect them to the login page. However, reverse_lazy doesn't seem to be able to find my login path.
views.py
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUpView(CreateView):
form_class = forms.UserCreateForm
template_name = 'accounts/signup.html'
#Redirect login page once they have successfully signed up
succes_url = reverse_lazy('login')
urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
path('signup/', views.SignUpView.as_view(template_name='accounts/signup.html'), name='signup'),
]
That is because you specified an app_name = …, in that case you prefix the name of the view with the namespace:
class SignUpView(CreateView):
# …
success_url = reverse_lazy('accounts:login')
You furthermore made a typo, it is success_url [Django-doc], not succes_url.

ImportError: cannot import name 'EntryView' from 'entries.views'

I am importing my views:
from .views import HomeView, EntryView
and the error I'm getting is:
ImportError: cannot import name 'EntryView' from 'entries.views' (.
C:\Users\Kheri\dev\cfehome\blog\entries\views.py) */
Below I'm providing my files:
views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Entry
class HomeView(ListView):
model = Entry
template_name = 'entries/index.html'
context_object_name = "blog_entries"
class EntryView(DetailView):
model = Entry
template_name = 'entries/entry_detail.html'
urls.py:
from django.urls import path
from .views import HomeView, EntryView
urlpatterns = [
path('', HomeView.as_view(), name = 'blog-home'),
path('entry/<int:pk>/', EntryView.as_view(), name = 'entry-detail')
]
Try correcting the indentation of views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Entry
class HomeView(ListView):
model = Entry
template_name = 'entries/index.html'
context_object_name = "blog_entries"
class EntryView(DetailView):
model = Entry
template_name = 'entries/entry_detail.html'
Got this error also, saw that I had a spelling error in urls.py
In my urlpatterns
I had path ('', AtricleListView.as_view(), name='article-list'),
instead of path ('', ArticleListView.as_view(), name='article-list'),
also in the import AtricleListView I had to change it to ArticleListView
from this
from .views import (
AtricleListView
)
to this
from .views import (
ArticleListView
)
My error looked like this
ImportError: cannot import name 'AtricleListView' from 'blog.views'
Here you can see that I tried to import "AtricleListView" instead of the real name that was "AtricleListView"

Trying to trace a circular import error in Django Python

I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django, it is giving me this error message:
django.core.exceptions.ImproperlyConfigured: The included URLconf 'starsocial.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
The issue started when I added a new app which has a urls.py like the following:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r'login/$',
auth_views.LoginView.as_view(template_name='accounts/login.html'),
name='login'),
url(r'logout/$',auth_views.LogoutView.as_view(), name='logout'),
url(r'signup/$',views.SignUp.as_view(), name='signup'),
]
My project urls.py has a line which points to the app and looks like the following code:
from django.contrib import admin
from django.urls import path,include
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^$', views.HomePage.as_view(), name='home'),
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^test/$', views.TestPage.as_view(), name='test'),
url(r'^thanks/$', views.ThanksPage.as_view(), name='thanks')
]
My application's view looks like the following:
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse('login')
template_name = 'accounts/signup.html'
My project's view looks like:
from django.views.generic import TemplateView
class TestPage(TemplateView):
template_name = 'test.html'
class ThanksPage(TemplateView):
template_name = 'thanks.html'
class HomePage(TemplateView):
template_name = 'index.html'
Can anyone please help me identify where I could possibly be going wrong.
You are importing auth.urls twice. Remove url(r'^accounts/', include('django.contrib.auth.urls')) from your project's urls.py
I am importing wrong URL configuration, instead of 'reverse' I should import 'reverse_lazy',
change
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse('login')
template_name = 'accounts/signup.html'
to
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy('login')
template_name = 'accounts/signup.html'

How to write primary key in django 2.0.2

the below code is working only for index page but it's not working for my DetailView. Please help me to fix (Using Django 2.0.2)
The below is my class for view:
from django.views import generic
from .models import Album
class IndexView(generic.ListView):
template_name = "newboston/index.html"
context_object_name = "all_album"
def get_queryset(self):
return Album.objects.all()
class DetailView(generic.DetailView):
model = Album
template_name = 'newboston/detail.html'
The below is my urls.py under my application.
from . import views
from django.urls import path
urlpatterns = [
path('', views.IndexView.as_view(), name='home'),
path('<slug:slug>/', views.DetailView.as_view(), name='detail'),
]
This is how you should refer to the primary key using pk in the urls.py file ...
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
Further details are in the documentation near the end of the page, or search for "polls/urls.py".

Categories