Python 属性错误:';非类型';对象没有属性';拆分';在django
我已经使用django mptt创建了嵌套的评论系统。(添加评论、回复某个评论、删除某个评论)这些功能工作正常。但当我试图编辑特定评论时,编辑评论后不会重新加载到详细页面。在控制台中,它会显示“AttributeError:'NoneType'对象没有属性'split'”。在此错误期间,另一个错误正在发生->TypeError:'NoneType'对象不可订阅。。以下是我的代码: models.py:Python 属性错误:';非类型';对象没有属性';拆分';在django,python,jquery,django,ajax,Python,Jquery,Django,Ajax,我已经使用django mptt创建了嵌套的评论系统。(添加评论、回复某个评论、删除某个评论)这些功能工作正常。但当我试图编辑特定评论时,编辑评论后不会重新加载到详细页面。在控制台中,它会显示“AttributeError:'NoneType'对象没有属性'split'”。在此错误期间,另一个错误正在发生->TypeError:'NoneType'对象不可订阅。。以下是我的代码: models.py: class Comment(MPTTModel): video = models.Fo
class Comment(MPTTModel):
video = models.ForeignKey(Video, on_delete=models.CASCADE, related_name = 'comments')
parent = TreeForeignKey('self',on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='children')
reply_to = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE, related_name='replayers')
user = models.ForeignKey(User, on_delete= models.CASCADE)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)
class MPTTMeta:
order_insertion_by = ['created']
def __str__(self):
return f'{self.content[:20]} by {self.user}'
views.py:
def edit_comment(request, video_id, comment_id):
video = get_object_or_404 (Video, id=video_id)
comment = Comment.objects.get(id=comment_id)
print(comment)
if request.method == 'POST':
edit_form = CommentUpdateForm(request.POST,instance=comment)
print(edit_form)
if edit_form.is_valid():
edit_form.save()
messages.success(request, f'Your comment has been updated!')
return HttpResponse("")
else:
return HttpResponse( "The content of the form is incorrect, please fill in again.")
else:
edit_form = CommentUpdateForm(instance=comment)
print(edit_form)
context = {
'edit_form': edit_form,
'video_id' : video_id,
'comment_id': comment_id,
}
return render(request,'comment/edit_comment.html',context=context)
main url.py:
urlpatterns = [
path('comment/',include('comment.urls')),
]
注释URL.py:
from django.urls import path
from comment import views
app_name = 'comment'
urlpatterns = [
path('edit-comment/<int:video_id>/<int:comment_id>/', views.edit_comment, name='edit_comment'),
]
编辑_comment.html:
<form action="{% url 'comment:edit_comment' video_id comment_id %}" method="POST" id="edit_comment_form">
<div class="form-group" >
{% csrf_token %}
{{ edit_form.content }}
</div>
<button type="submit" onclick = "confirm_submit({{ video_id }}, {{ comment_id }})" class = "btn btn-primary">Update</button>
</form>
<script>
function confirm_submit(video_id, comment_id ){
// call ajax to exchange data with the backend
$.ajax ({
url : '/comment/edit-comment/' + video_id + '/' + comment_id + '/',
type : 'POST' ,
data : { content: $('#content').val(), },
// success callback
success: function ( e ){
if (e === ''){
parent.location.reload();
}
}
})
}
</script>
我如何解决这个问题?有什么建议吗?请在问题中添加完整的错误回溯对不起,我现在更新了错误回溯。
class CommentUpdateForm(forms.ModelForm):
content = forms.CharField(
widget=forms.Textarea(
attrs={
'class': 'form-group form-control',
'id':'content',
'rows': 3,
'cols': 10,
}
)
)
class Meta:
model = Comment
fields = ['content',]
<form action="{% url 'comment:edit_comment' video_id comment_id %}" method="POST" id="edit_comment_form">
<div class="form-group" >
{% csrf_token %}
{{ edit_form.content }}
</div>
<button type="submit" onclick = "confirm_submit({{ video_id }}, {{ comment_id }})" class = "btn btn-primary">Update</button>
</form>
<script>
function confirm_submit(video_id, comment_id ){
// call ajax to exchange data with the backend
$.ajax ({
url : '/comment/edit-comment/' + video_id + '/' + comment_id + '/',
type : 'POST' ,
data : { content: $('#content').val(), },
// success callback
success: function ( e ){
if (e === ''){
parent.location.reload();
}
}
})
}
</script>
Exception happened during processing of request from ('127.0.0.1', 53885)
Traceback (most recent call last):
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 279, in write
self._write(data)
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 453, in _write
result = self.stdout.write(data)
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 799, in write
self._sock.sendall(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "E:\Bohubrihi\Django\VE\Bohubrihi\lib\site-packages\django\core\servers\basehttp.py", line 116, in handle_error
super().handle_error()
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 720, in __init__
self.handle()
File "E:\Bohubrihi\Django\VE\Bohubrihi\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
self.handle_one_request()
File "E:\Bohubrihi\Django\VE\Bohubrihi\lib\site-packages\django\core\servers\basehttp.py", line 194, in handle_one_request
handler.run(self.server.get_app())
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "E:\Bohubrihi\Django\VE\Bohubrihi\lib\site-packages\django\core\servers\basehttp.py", line 111, in close
super().close()
File "C:\Users\Sudipto\AppData\Local\Programs\Python\Python37-32\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'