gpt4 book ai didi

django - 类型错误 : post() got an unexpected keyword argument

转载 作者:行者123 更新时间:2023-12-02 08:00:43 27 4
gpt4 key购买 nike

我正在尝试创建一个简单的博客,我可以在其中直接与用户交流。每个用户每个月都会有一篇由管理员发布的博客文章,他们可以对其发表评论以进行交流。工作流程如下:
管理员登录站点 -> 管理员显示所有可用用户。 -> 管理员点击用户 -> 如果该用户有当月的帖子,则显示该帖子 -> 否则创建新帖子。
这是我所拥有的:
博客/urls.py :

from django.urls import path
from .views import MessageThread, CreateThread

urlpatterns = [
path('user_thread/<int:user_id>', MessageThread.as_view(), name='message_thread'),
path('create_thread/<int:user_id>', CreateThread.as_view(), name='create_thread'),
]
博客/models.py:
from django.db import models
from django.contrib.auth.models import User


class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField(max_length=9001)
posted_for = models.ForeignKey(User, related_name='posted_for', on_delete=models.CASCADE)
published_date = models.DateTimeField(blank=True, null=True)

def __str__(self):
return self.title
博客/forms.py:
from django.forms import models
from .models import Post


class CreateThreadForm(models.ModelForm):

class Meta:
model = Post
fields = ['title', 'text']
博客/views.py:
from .forms import CreateThreadForm
from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from datetime import datetime
from django.contrib.auth.models import User

class CreateThread(TemplateView):
template_name = 'blog/create_thread.html'

def get(self, request, *args, **kwargs):

return render(request, self.template_name, {'form': CreateThreadForm(), 'user_id': self.kwargs['user_id']})

def post(self, request):

if 'comfirm_post' in request.POST:
form = CreateThreadForm(request.POST)
if form.is_valid():
post_details = form.save(commit=False)
post_details.author = request.user
post_details.posted_for = User.objects.get(id=request.POST['user_id'])
post_details.published_date = datetime.now()
form.save()

messages.success(request, "Successfully created post")
return redirect('message_thread', user_id=request.POST['user_id'])

messages.error(request, 'Something went wrong')
return redirect('create_thread', user_id=request.POST['user_id'])
博客/create_thread.html:
{% extends 'navbar.html' %}
{% block content %}
<h1>Create New Thread</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" name="comfirm_post">Create Thread</button>
</form>
{% endblock %}
问题是它甚至没有进入 CreateThread 的 post() 函数中。类(class)。它只是抛出一个错误,说 post() got an unexpected keyword argument 'user_id' .这是错误的完整引用:
Internal Server Error: /blog/create_thread/4
Traceback (most recent call last):
File "C:\Projects\1-2-1\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Projects\1-2-1\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Projects\1-2-1\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Projects\1-2-1\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Projects\1-2-1\venv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
TypeError: post() got an unexpected keyword argument 'user_id'
[27/Aug/2019 16:04:02] "POST /blog/create_thread/4 HTTP/1.1" 500 75548
我不知道它说的这个 user_id 来自哪里。我尝试将 user_id 作为隐藏输入从表单中传递,我尝试更改操作以传递 user_id,如 action={% url 'create_thread' user_id %}但这根本不起作用。

最佳答案

您有 user_id在您的网址格式中,

path('create_thread/<int:user_id>', ...

因此您的 post方法应该采取 user_id ,你应该使用 user_id而不是 request.POST['user_id'] :
def post(self, request, user_id):
...
post_details.posted_for = User.objects.get(id=user_id)

或者,您可以接受 *args**kwargs ,然后获取 user_id来自 kwargsself.kwargs .
def post(self, request, *args, **kwargs):
...
post_details.posted_for = User.objects.get(id=self.kwargs['user_id'])

关于django - 类型错误 : post() got an unexpected keyword argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57674348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com