问题描述
我正尝试使用Django(版本3.1)克隆Instagram网页。
我的Django项目有一个名为“ post”的应用。
我的模板之一是表单,该表单正在对帖子发布评论。表单发布请求应调用path('add_comment/',views.add_comment,name='add_comment'),
,但应调用path('<slug:slug>/',views.post_details,name='post_details'),
。并引发DoesNotExist at /post/add_comment
错误。我在print()
和add_comment()
方法的开头添加了post_details()
语句,以找出发出请求时正在运行的语句。我不知道我做错了什么。
项目GitHub链接-https://github.com/mirasel/Instagram_Clone
post_details.html
模板是-
{% extends 'base.html' %}
{% load static %}
{% block title %} post {% endblock %}
{% block profilephoto %} {{ propic.url }} {% endblock %}
{% block body %}
<div>
<div>
<img src="{{post.image.url}}" alt="post" height="250px" width="250px">
</div>
<div>
<a href="{% url 'instagram:profile' post.uploader %}">
<img src="{{uploader.profile_pic.url}}" alt="{{uploader}}" style="border-radius: 50%;" height="24px" width="24px">
{{ post.uploader }}
</a><br>
<p>{{ post.date_published.date }}</p>
</div>
<div>
<p>{{ post.caption }}</p>
</div>
<div>
<form action="{% url 'post:add_comment' %}" id="comment_form" method="POST">
{% csrf_token %}
<textarea name="comment" id="comment" cols="30" rows="1" placeholder="Write a comment..."></textarea>
<input type="hidden" name="slug" id="slug" value="{{post.slug}}">
<!-- <input type="submit" style="display: none;" name="submit"> -->
</form>
<script>
$(function(){
$("#comment").keypress(function (e) {
if(e.which == 13 && !e.shiftKey) {
$(this).closest("form").submit();
e.preventDefault();
}
});
});
</script>
{% endblock %}
views.py
-
from django.shortcuts import render,redirect
from instagram.views import get_nav_propic,get_profile_details
from .models import UserPost,PostComment,PostLike
from django.http import JsonResponse
def get_post_likes(post):
likes = PostLike.objects.filter(post=post)
total_likes = len(likes)
likers = []
for l in likes:
likers.append(get_profile_details(l.liker))
return {'likers':likers,'total_likes':total_likes}
def get_post_comments(post):
comments = PostComment.objects.filter(post=post)
total_comments = len(comments)
commenter = []
comment = []
for c in comments:
commenter.append(get_profile_details(c.commenter))
comment.append(c.comment)
postcomment = zip(commenter,comment)
return {'post_comment':postcomment,'total_comments':total_comments}
def upload_post(request):
if request.method == 'POST':
image = request.FILES['post_img']
caption = request.POST['caption']
uploader = request.user
UserPost.objects.create(uploader=uploader,image=image,caption=caption)
return redirect('instagram:Feed')
else:
context = {
'propic' : get_nav_propic(request.user)
}
return render(request,'post/upload_post.html',context)
def post_details(request,slug):
print('I am here in post details')
post = UserPost.objects.get(slug=slug)
context = {
'propic' : get_nav_propic(request.user),'post' : post,'uploader' : get_profile_details(post.uploader),'LIKES' : get_post_likes(post),'COMMENTS' : get_post_comments(post),}
return render(request,'post/post_details.html',context)
def add_comment(request):
print('I am here in add comment')
if request.method == 'POST':
post_slug = request.POST.get('slug')
post = UserPost.objects.get(slug=post_slug)
user = request.user
comment = request.POST.get('comment')
PostComment.objects.create(post=post,commenter=user,comment=comment)
return redirect('post:post_details',slug=post_slug)
urls.py
-
from django.urls import path
from . import views
app_name='post'
urlpatterns = [
path('upload_post/',views.upload_post,name='upload_post'),path('<slug:slug>/',path('add_comment/',]
已解决 我必须使add_comment的URL路径如下-
#prevIoUs one
path('add_comment/',#modified one
path('comment/add_comment/',
这是因为段状URL和添加注释URL的模式相似。
解决方法
因为Django将从docs开始依次处理urlpatterns
:
Django按顺序运行每个URL模式,并在第一个停止 一个与请求的URL匹配的文件,与path_info匹配。
并且'/add_comment'
是有效的子弹<slug:slug>
,因此将调用post_details
。
因此,您应该最后保留最通用的网址格式的定义:
urlpatterns = [
path('upload_post/',views.upload_post,name='upload_post'),path('add_comment/',views.add_comment,name='add_comment'),path('<slug:slug>/',views.post_details,name='post_details'),]
希望这对您有用。