如何修复'str'对象在Django中不可调用

时间:2019-11-01 04:13:15

标签: python django django-models django-forms django-views

大家好,我做了一个博客,但是当我删除帖子时,出现此错误,我使用的是Django最新版本:

TypeError at /post/(?P3\d+)/remove/
'str' object is not callable'

这是我的 Views.py

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

这是我的应用程序 Urls.py

from django.urls import path
from blog import views

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

图片 https://i.stack.imgur.com/wbcfn.png

这是我的 post_confirm_delete.html

{% extends "blog/base.html" %}
{% block content %}
<form method="POST">
    {% csrf_token %}
    <p>Are You sure you want to delete {{ object }}?</p>
    <input type="submit" class="btn btn-danger" value="Confirm">
</form>
{% endblock %}

这是我的项目 Urls.py

"""blogpro URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from django.contrib.auth.views import LoginView,LogoutView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
    path('accounts/login/', LoginView.as_view(), name='login'),
    path('accounts/logout/', LogoutView.as_view(), name='logout', kwargs={'next_page': '/'}),
]

1 个答案:

答案 0 :(得分:1)

您正在使用带有正则表达式的路径,而不是它的工作方式docs,只能将带有正则表达式的url字符串与re_path一起使用

应该是:

path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'),