首页 > 解决方案 > 新版django和python代码的问题

问题描述

我在这里坚持使用 Django,我在下面添加了 views.py 和 urls.py 的代码。我收到以下错误:

这是我得到的错误

如果有任何解决方案,请调查并帮助我。错误在图像的底部。

 views.py 
 from django.shortcuts import render, get_object_or_404, redirect
 from django.contrib.auth.decorators import login_required
 from django.utils import timezone
 from .models import Post, Comment
 from .forms import PostForm, CommentForm

 from django.views.generic import (TemplateView,ListView,
                              DetailView,CreateView,
                              UpdateView,DeleteView)

 from django.urls import reverse_lazy
   from django.contrib.auth.mixins import LoginRequiredMixin

# Create your views here.
class AboutView(TemplateView):
  template_name = 'blog/about.html'

类 PostListView(ListView): 模型 = 发布

def get_queryset(self):
    return 
  Post.objects.filter(published_date__lte=timezone.now()).order_by('- 
   published_date')

class PostDetailView(DetailView):
    model = Post


  class CreatePostView(LoginRequiredMixin,CreateView):
   login_url = '/login/'
    redirect_field_name = 'blog/post_detail.html'

   form_class = PostForm

     model = Post


   class PostUpdateView(LoginRequiredMixin,UpdateView):
      login_url = '/login/'
      redirect_field_name = 'blog/post_detail.html'

    form_class = PostForm

     model = Post


  class PostDeleteView(LoginRequiredMixin,DeleteView):
    model = Post
    success_url = reverse_lazy('post_list')


  class DraftListView(LoginRequiredMixin,ListView):
    login_url = '/login/'
    redirect_field_name = 'blog/post_draft_list.html'

   model = Post

   def get_queryset(self):
       return 
  Post.objects.filter(published_date__isnull=True).order_by('created_date')



   @login_required
    def post_publish(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.publish()
    return redirect('post_detail', pk=pk)

  @login_required
    def add_comment_to_post(request, pk):
     post = get_object_or_404(Post, pk=pk)
       if request.method == "POST":
       form = CommentForm(request.POST)
        if form.is_valid():
           comment = form.save(commit=False)
           comment.post = post
           comment.save()
           return redirect('post_detail', pk=post.pk)
else:
    form = CommentForm()
return render(request, 'blog/comment_form.html', {'form': form})


    @login_required
     def comment_approve(request, pk): 

     comment = get_object_or_404(Comment, pk=pk)
     comment.approve()
     return redirect('post_detail', pk=comment.post.pk)


@login_required
def comment_remove(request, pk):
    comment = get_object_or_404(Comment, pk=pk)
    post_pk = comment.post.pk
    comment.delete()
    return redirect('post_detail', pk=post_pk)
***This is urls.py***
 from django.contrib import admin
 from django.urls import path, include
 from django.contrib.auth import views

urlpatterns = [
    path('', include('blog.urls')),
    path('admin/', admin.site.urls),
    path('accounts/login/', views.login, name='login'),
    path('accounts/logout/', views.logout, name='logout', kwargs= 
    {'next_page': '/'}),
    ]
 from django.urls import path
 from . import views


 urlpatterns = [
  path('',views.PostListView.as_view(),name='post_list'),
  path('about/',views.AboutView.as_view(),name='about'),
  path('post/<int:pk>', views.PostDetailView.as_view(), name='post_detail'),
  path('post/new/', views.CreatePostView.as_view(), name='post_new'),
  path('post/<int:pk>/edit/', views.PostUpdateView.as_view(), 
name='post_edit'),
   path('drafts/', views.DraftListView.as_view(), name='post_draft_list'),
   path('post/<int:pk>/remove/', views.PostDeleteView.as_view(), 
name='post_remove'),
   path('post/<int:pk>/publish/', views.post_publish, name='post_publish'),
   path('post/<int:pk>/comment/', views.add_comment_to_post, 
name='add_comment_to_post'),
     path('comment/<int:pk>/approve/', views.comment_approve, 
name='comment_approve'),
   path('comment/<int:pk>/remove/', views.comment_remove, 
  name='comment_remove'),
 ]

请让我知道错误是什么。上面的文件是views.py,app也是urls.py。查看底部已提到错误的图像。

标签: pythondjango

解决方案


根据错误尝试使用此 url 进行 django 登录视图:

    path('login/', views.LoginView.as_view(), name='login'),

推荐阅读