首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >新版本的django和python代码的问题

新版本的django和python代码的问题
EN

Stack Overflow用户
提问于 2019-03-12 22:14:49
回答 1查看 109关注 0票数 0

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

This is the error im getting

请调查一下,如果有什么解决办法,请帮助我。错误位于图像的底部。

代码语言:javascript
复制
 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'

类模型(ListView):PostListView= Post

代码语言:javascript
复制
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'),
 ]

请告诉我错误在哪里。上面的文件也是app的views.py和urls.py。请看下面提到的错误图像。

EN

回答 1

Stack Overflow用户

发布于 2019-03-12 22:30:07

根据错误,尝试此url用于django登录视图:

代码语言:javascript
复制
    path('login/', views.LoginView.as_view(), name='login'),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55123617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档