为什么我会收到此错误 'WSGIRequest' object has no attribute 'kwargs'?
Why am I getting this error 'WSGIRequest' object has no attribute 'kwargs'?
我正在尝试为我的 post 创建一个评论部分,但我不明白为什么会出现此错误,这是代码,您可以查看它,我只会分享 views.py 中的 class 是我遇到的错误
views.py
class AddCommentView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'app1/add_comment.html'
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
return super().form_valid(form)
#fields = '__all__'
success_url = reverse_lazy('index')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from datetime import datetime, date
from ckeditor.fields import RichTextField
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length= 255)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('index')
class Profile(models.Model):
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
bio = models.TextField()
profile_pic = models.ImageField(null = True, blank = True, upload_to = "images/profile/")
website_url = models.CharField(max_length= 255, blank = True, null = True)
facebook_url = models.CharField(max_length= 255, blank = True, null = True)
twitter_url = models.CharField(max_length= 255, blank = True, null = True)
instagram_url = models.CharField(max_length= 255, blank = True, null = True)
pinterest_url = models.CharField(max_length= 255, blank = True, null = True)
def __str__(self):
return str(self.user)
def get_absolute_url(self):
return reverse('index')
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.post.title, self.name)
模板
{% extends 'app1/base.html' %}
{% load static %}
{% block body_block %}
<h1>Add Blog Post</h1>
<br><br>
<div class="form-group">
<form action="" method="post">
{% csrf_token %}
{{form.as_p}}
<button class= "btn btn-secondary">Add Comment</button>
</form>
</div>
{% endblock %}
如果有人能解释问题所在并提前致谢,我将不胜感激。
因为确实 request
没有任何名为 kwargs
的属性。
我看到您有一个名为 CommentForm
的表单正在使用 HTTP POST 方法提交。如果该表单实际上有一个名为 pk
的字段,那么您可以通过以下方式获取它的值:
form.instance.post_id = self.request.POST['pk']
我正在尝试为我的 post 创建一个评论部分,但我不明白为什么会出现此错误,这是代码,您可以查看它,我只会分享 views.py 中的 class 是我遇到的错误
views.py
class AddCommentView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'app1/add_comment.html'
def form_valid(self, form):
form.instance.post_id = self.kwargs['pk']
return super().form_valid(form)
#fields = '__all__'
success_url = reverse_lazy('index')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from datetime import datetime, date
from ckeditor.fields import RichTextField
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length= 255)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('index')
class Profile(models.Model):
user = models.OneToOneField(User, null = True, on_delete=models.CASCADE)
bio = models.TextField()
profile_pic = models.ImageField(null = True, blank = True, upload_to = "images/profile/")
website_url = models.CharField(max_length= 255, blank = True, null = True)
facebook_url = models.CharField(max_length= 255, blank = True, null = True)
twitter_url = models.CharField(max_length= 255, blank = True, null = True)
instagram_url = models.CharField(max_length= 255, blank = True, null = True)
pinterest_url = models.CharField(max_length= 255, blank = True, null = True)
def __str__(self):
return str(self.user)
def get_absolute_url(self):
return reverse('index')
class Post(models.Model):
title = models.CharField(max_length= 255)
header_image = models.ImageField(null = True, blank = True, upload_to = 'images/')
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextField(blank = True, null = True)
#body = models.TextField()
post_date = models.DateField(auto_now_add=True)
category = models.CharField(max_length=255, default='coding')
snippet = models.CharField(max_length=255)
likes = models.ManyToManyField(User, related_name = 'blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('app1:article-detail', args=(self.id,))
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.post.title, self.name)
模板
{% extends 'app1/base.html' %}
{% load static %}
{% block body_block %}
<h1>Add Blog Post</h1>
<br><br>
<div class="form-group">
<form action="" method="post">
{% csrf_token %}
{{form.as_p}}
<button class= "btn btn-secondary">Add Comment</button>
</form>
</div>
{% endblock %}
如果有人能解释问题所在并提前致谢,我将不胜感激。
因为确实 request
没有任何名为 kwargs
的属性。
我看到您有一个名为 CommentForm
的表单正在使用 HTTP POST 方法提交。如果该表单实际上有一个名为 pk
的字段,那么您可以通过以下方式获取它的值:
form.instance.post_id = self.request.POST['pk']