【发布时间】:2018-08-03 01:25:21
【问题描述】:
我是新人,正在学习 django。当我安装 django_cmets 时,我需要为 django_cmets 添加新的 url 和视图,但它不起作用。
comments folder structure:
__init__.py __pycache__/ forms.py migrations/ models.py templates/ urls.py views.py
__init__.py:
def get_model():
from comments.models import CommentModel
return CommentModel
def get_form():
from comments.forms import CommentForm
return CommentForm
forms.py 和 models.py 很好用。但是当我添加 urls.py、views.py 并将 url 添加到主 urls 文件时。它不起作用。
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('delete/<int:comment_id>/', views.delete_own_comment, 'delete_own_comment'),
]
views.py
from .models import CommentModel
@login_required
def delete_own_comment(request, comment_id):
comment = get_object_or_404(CommentModel, id=comment_id, site__pk=settings.SITE_ID)
if comment.user == request.user:
comment.is_removed = True
comment.save()
但是当我将 path('mycomments/', include('comments.urls')) 添加到主 urls.py 时,它会运行奇怪的错误。谁能帮帮我???
【问题讨论】:
-
如果我使用 djang-admin startapp 并按照常规步骤操作,它不起作用,这是怎么回事???
-
为什么要在 init.py 中添加代码?这没有必要。
-
你应该显示实际的错误是什么。
-
显示错误回溯
-
跟随django-contrib-comments.readthedocs.io/en/latest ,它需要在 init.py 文件中添加一些内容。并引发“字典更新序列元素 #0 的长度为 1;需要 2”错误。
标签: django django-models django-views django-comments