简体   繁体   English

为什么我收到此错误“WSGIRequest”object 没有属性“kwargs”?

[英]Why am I getting this error 'WSGIRequest' object has no attribute 'kwargs'?

I'm trying to create a comment section for my post, and I don't understand why I'm getting this error, here's the code so you can check it out, I'll only share the class in views.py were I'm getting the error我正在尝试为我的帖子创建评论部分,但我不明白为什么会出现此错误,这是代码,您可以查看它,我只会在 views.py 中分享 class得到错误

views.py视图.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模型.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)

template模板

 
{% 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 %}

I would really appreciate if someone could explain what's the problem and thanks in advance.如果有人能解释问题所在并提前致谢,我将不胜感激。

Because indeed request doesn't have any attribute called kwargs .因为确实request没有任何名为kwargs的属性。

I see you have a form called CommentForm that is being submitted using the HTTP POST method.我看到您有一个名为CommentForm的表单,它正在使用 HTTP POST 方法提交。 If that form actually has a field named pk , then you can get the value of it by doing:如果该表单实际上有一个名为pk的字段,那么您可以通过执行以下操作来获取它的值:

form.instance.post_id = self.request.POST['pk']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我一直得到&#39;WSGIRequest&#39;对象在django上没有&#39;Get&#39;属性 - I keep getting 'WSGIRequest' object has no attribute 'Get' on django 为什么会出现错误:“ int”对象没有属性“ create”? - Why am I getting the error: 'int' object has no attribute 'create'? 为什么我收到此错误:对象没有属性“ astype” - Why I am getting this error: object has no attribute 'astype' 属性错误:'WSGIRequest' object 没有属性 'get' - Attribute error : 'WSGIRequest' object has no attribute 'get' Django错误:“ WSGIRequest”对象没有属性“ user” - Django Error : 'WSGIRequest' object has no attribute 'user' 我不知道为什么,但我收到错误消息:“AttributeError: 'super' object has no attribute '__getattr__'” - I am not sure why but I am getting error: “AttributeError: 'super' object has no attribute '__getattr__'” 为什么我收到属性错误“AttributeError:‘Values’对象没有属性‘interface’” - why am i getting attribute error "AttributeError: 'Values' object has no attribute 'interface'" 为什么我会出现情节表达属性错误? (AttributeError: &#39;Figure&#39; 对象没有属性 &#39;add_hline&#39; - Why am I getting plotly express attribute error? (AttributeError: 'Figure' object has no attribute 'add_hline' 我收到属性错误为'int' object has no attribute 'to' - I am getting Attribute error as 'int' object has no attribute 'to' “ WSGIRequest”对象没有属性“ update” - 'WSGIRequest' object has no attribute 'update'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM