|
|
|
|
@ -1,11 +1,45 @@
|
|
|
|
|
#zyl:
|
|
|
|
|
from django.urls import path
|
|
|
|
|
|
|
|
|
|
# 从当前包导入 views 模块
|
|
|
|
|
# 这里包含了处理评论相关请求的视图函数或类
|
|
|
|
|
from . import views
|
|
|
|
|
|
|
|
|
|
# 设置应用命名空间
|
|
|
|
|
# 这在项目中有多个应用时使用,可以避免 URL 名称冲突
|
|
|
|
|
# 在模板或视图中通过 'comments:postcomment' 引用此 URL
|
|
|
|
|
# 例如:reverse('comments:postcomment', kwargs={'article_id': 1})
|
|
|
|
|
app_name = "comments"
|
|
|
|
|
|
|
|
|
|
# URL 模式列表:定义该应用所有可访问的 URL 路径
|
|
|
|
|
urlpatterns = [
|
|
|
|
|
# 发布评论的 URL 模式
|
|
|
|
|
path(
|
|
|
|
|
# URL 路径:匹配形如 /article/123/postcomment 的 URL
|
|
|
|
|
# <int:article_id> 捕获一个整数参数,传递给视图
|
|
|
|
|
# 参数名 article_id 必须与视图中的参数名一致
|
|
|
|
|
'article/<int:article_id>/postcomment',
|
|
|
|
|
|
|
|
|
|
# 视图处理函数:使用类视图的 as_view() 方法
|
|
|
|
|
# CommentPostView 处理评论提交逻辑(POST 请求)
|
|
|
|
|
views.CommentPostView.as_view(),
|
|
|
|
|
|
|
|
|
|
# URL 名称:用于反向解析 URL
|
|
|
|
|
# 在模板中可使用 {% url 'comments:postcomment' article_id=article.id %}
|
|
|
|
|
# 在视图中可使用 reverse('comments:postcomment', kwargs={'article_id': article.id})
|
|
|
|
|
name='postcomment'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 使用说明:
|
|
|
|
|
# 1. 用户访问 /article/123/postcomment (POST 请求)
|
|
|
|
|
# 2. Django 捕获 article_id=123
|
|
|
|
|
# 3. 调用 CommentPostView 处理请求
|
|
|
|
|
# 4. 视图从 request.POST 获取评论内容,创建 Comment 对象
|
|
|
|
|
# 5. 根据 parent_comment_id 判断是主评论还是回复
|
|
|
|
|
# 6. 处理完成后重定向回文章详情页
|
|
|
|
|
|
|
|
|
|
# 安全注意事项:
|
|
|
|
|
# - 此 URL 应仅接受 POST 请求(在视图中需做限制)
|
|
|
|
|
# - 需要验证用户身份(登录状态)
|
|
|
|
|
# - 需要验证用户对文章的访问权限
|
|
|
|
|
# - 需要防范 CSRF 攻击(Django 中间件默认处理)
|