python - 新版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。查看底部已提到错误的图像。
解决方案
根据错误尝试使用此 url 进行 django 登录视图:
path('login/', views.LoginView.as_view(), name='login'),
推荐阅读
- git - 使用 sshpass 和私钥时 SSH 挂起
- android - 使用 Android Studio 3+ 监控线程正在执行的内容
- angular - 组件重定向后保持 Angular Material 表状态
- php - 激活为php加载更多功能
- ruby - Ruby - 从数组中删除一个值
- ios - 从 json 中获取单个元素
- java - Java 配置中的 Spring Mvc 消息包
- android - 在 TabLayout 下对 tabtext 执行单击总是给出 NoMatchingViewException
- dart - 如何使用地图打印列表元素?
- r - 如何提取特定单词在列中的首次出现