首页 > 解决方案 > 类型错误:comment_detail() 缺少 2 个必需的位置参数:'request' 和 'slug'

问题描述

我有错误

path('post/<int:id>/comments', comment_detail(), name='comments'),

TypeError: comment_detail() missing 2 required positional arguments: 'request' and 'slug'

视图.py

from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .forms import CommentForm
from .models import Post


class BlogListView(ListView):
    model = Post
    template_name = 'home.html'

    context_object_name = 'posts'
    paginate_by = 2
    queryset = Post.objects.all()

class BlogDetailView(DetailView):
    model = Post
    template_name = 'post_detail.html'

class BlogCreateView(CreateView):
    model = Post
    template_name = 'post_new.html'
    fields = ['title', 'author', 'body', 'header_image']

class BlogUpdateView(UpdateView):
    model = Post
    template_name = 'post_edit.html'
    fields = ['title', 'body', 'header_image']

class BlogDeleteView(DeleteView):
    model = Post
    template_name = 'post_delete.html'
    success_url = reverse_lazy('home')

@property
def image_url(self):
    if self.image:
        return getattr(self.photo, 'url', None)
    return None

def comment_detail(request, slug):
    post = Post.objects.get(Post, slug=slug,)

    if request.method == 'POST':
        form = CommentForm(request.POST or None)

        if form.is_valid():
            obj = form.save(commit=False)
            obj.post = post
            obj.save()

            return redirect('detail', slug=post.slug)
    else:
        form = CommentForm()

    context = {
        'post': post,
        'form': form
    }

    return render(request,'post_detail.html', context)

post_detail.html

{% extends 'base.html' %}
{% load static %}

{% block content %}
    <link href="{% static 'css/post_detail.css' %}" rel="stylesheet">
    <div class="post-entry">
        <h2>{{ post.title }}</h2>
        <p>{{ post.body|urlize }}</p>
    </div>

    <p><a href="{% url 'post_edit' post.pk %}">+ Edit Blog Post</a></p>
    <p><a href="{% url 'post_delete' post.pk %}">+ Delete Blog Post</a></p>
    {% if post.header_image %}
        <p><img src="{{post.header_image.url}}"></p>
    {% else %}
        <p></p>
    {% endif %}


    {% for comm in post.commentpost_set.all%}
        {{ comm.user }} <br>
        {{ comm.text }} <br><br>
    {% endfor %}

    <article class="content" >

        <br><hr>


        <form method="post" action='{% url 'comments' %}'>
            {% csrf_token %}

            {{ form.as_table }}
            <div class="container height-100 d-flex justify-content-center align-items-center">
                <div class="card p-3">
                    <h4>Add comments</h4> <textarea id="textarea" class="form-control"></textarea>
                    <div class="mt-3 d-flex justify-content-between align-items-center"> <span id="count"></span><br><button class="btn btn-sm btn-danger">Submit</button> </div>
                </div>
            </div>
        </form>
    </article>

{% endblock content %}

这是我正在使用的模板

看起来您的帖子主要是代码;请添加更多详细信息。好的,我加点文字,真是个笨功能,看不懂,让我无法正常提问

“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua。Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "

标签: pythondjangodjango-views

解决方案


path('post/<int:slug>/comments', comment_detail, name='comments'),

该行中有两个问题,comment_details应该是函数指针而不是函数调用,变量名应该是slug

这是问题

 <form method="post" action='{% url 'comments' %}'>

您没有传递 slug,因此您应将其设为可选参数并添加不带参数的 url。

将此添加到 urls.py

path('post/comments', comment_detail, name='comments'),

在views.py

 def comment_details(request,slug=None):
 

推荐阅读