简体   繁体   中英

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 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)

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 .

I see you have a form called CommentForm that is being submitted using the HTTP POST method. If that form actually has a field named pk , then you can get the value of it by doing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM