使用DeleteView删除对象,得到“'mysite'不是注册的名称空间”

时间:2019-11-17 21:31:49

标签: django django-templates django-views

我按照此示例将文档上传到AWS(https://github.com/sibtc/simple-s3-setup/tree/master/s3-example-static-and-media)。我现在添加了一项功能,该功能允许用户删除上传的文档。

我收到错误消息“ django.urls.exceptions.NoReverseMatch:'mysite'不是注册的名称空间”。接下来是/home/ubuntu/.local/lib/python3.6/site-packages/django/urls/base.py中的参数值:
args [1]; current_app''; current_ns无; current_path无; kwargs {}; ns'mysite'; ns_converters {}; ns_pattern'';零件['delete','mysite'];路径[];字首 '/'; resolve_path [];解析器; urlconf'mysite.urls';查看“删除”;视图名'mysite:delete'

以下是我进行过更改的文件:
(1)mysite / core / views.py
class DocumentCreateView(CreateView): model = Document fields = ['upload', ] success_url = reverse_lazy('home')

   def get_context_data(self, **kwargs):
       context = super().get_context_data(**kwargs)
       documents = Document.objects.all()
       context['documents'] = documents
       return context

(下面是我添加的。我可能需要在此处添加一些代码以使删除工作正常进行)
class DocumentDeleteView(DeleteView): model = Document fields = ['upload', ] success_url = reverse_lazy('home')

   def get_context_data(self, **kwargs):
       context = super().get_context_data(**kwargs)
       documents = Document.objects.all()
       context['documents'] = documents
       return context

(2)mysite / urls.py
app_name = 'mysite' urlpatterns = [ url(r'^$', views.DocumentCreateView.as_view(), name='home'), *(below was added by me)* path('<int:pk>/delete/', views.DocumentDeleteView.as_view(), name='delete'), ]

(3)mysite / core / templates / core / document_form.html:
我还在下面的第27行中添加了以下代码 https://github.com/sibtc/simple-s3-setup/blob/master/s3-example-static-and-media/mysite/core/templates/core/document_form.html
<td><a href="{% url 'mysite:delete' document.id %}">Delete</a></td>
(如果我将“ mysite:delete”更改为“ delete”,则会收到“未找到“ delete”的反向名称。“ delete”不是有效的视图函数或模式名称。”)

0 个答案:

没有答案