django-comments添加一个新的URL和一个意见视图?

时间:2018-08-03 01:25:21

标签: django django-models django-views django-comments

我是新手,正在研究django。当我安装django_comments时,我需要添加以添加新的url和django_comments的视图,但这不起作用。

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添加到主URL文件时。它不起作用。

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()

但是当我在主urls.py中添加path('mycomments/', include('comments.urls'))时,它会运行奇怪的错误。谁能帮我吗?

1 个答案:

答案 0 :(得分:0)

不是

django-admin startapp myapp

python manage.py startapp myapp

提供您在创建项目之前使用

django-admin startproject myproject