新版本的Django和python代码出现问题

时间:2019-03-12 14:14:49

标签: python django

我在这里停留在Django上,我在下面添加了views.py和urls.py的代码。我收到以下错误:

This is the error im getting

请仔细研究它,如果有任何解决方案,请帮助我。   错误位于图像的底部。

 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'

Class 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和urls.py。 查看图片,该错误已在底部提到。

1 个答案:

答案 0 :(得分:0)

根据错误,尝试在django登录视图中使用以下URL:

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