首页 > 解决方案 > 我的 django 视图总是抛出“意外的关键字参数错误

问题描述

我熟悉python并开始学习django,一开始很有趣,从2天开始表格插入对我来说变成了噩梦并且总是抛出以下错误

sample() got an unexpected keyword argument 'firstname'

我已经在文档和 stackoverflow 上尝试了所有可能的方法,但没有任何效果。但是另一个具有相同语法的视图对我有用,这太奇怪了。这是我的文件。这是我的堆栈跟踪。

Traceback (most recent call last):
  File "C:\Users\manee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\manee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\manee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\manee\myapp\PE\views.py", line 77, in sample
    a=sample(firstname=name)

Exception Type: TypeError at /sample
Exception Value: sample() got an unexpected keyword argument 'firstname'

这是工作视图和麻烦视图的组合(问题工作正常,示例不工作)。

def problems(request):

    name=request.POST['name']
    title=request.POST['title']
    difficulty=request.POST['example']
    description=request.POST['description']
    solution=request.POST['solution']
    code=request.POST['code']

    question=problem(name=name,title=title,difficulty=difficulty,description=description,solution=solution,code=code)
    question.save()
    return render(request,'thanks.html')


def sample(request):
    name=request.POST['name']
    email=request.POST['email']
    rno=request.POST['rno']
    a=sample(firstname=name,email=email,rno=rno)
    a.save()
    return render(request,'example.html',{'name':name})

这是我的models.py

from django.db import models

#Create your models here.
class problem(models.Model):
    id=models.AutoField(primary_key=True)
    name=models.CharField(max_length=20)
    title=models.CharField(max_length=30)
    difficulty=models.CharField(max_length=10)
    description=models.TextField()
    solution=models.TextField()
    code=models.TextField()
    def __str__(self):
        return self.name

class sample(models.Model):
    firstname=models.CharField(max_length=20)
    email=models.EmailField()
    rno=models.IntegerField(primary_key=True)

    def __str__(self):
        return self.firstname

请快点修好

标签: pythondjangopython-3.xdjango-modelsdjango-views

解决方案


您的sample模型没有name字段,只有 a firstname,但这不是这里的问题。问题是您的视图具有相同的名称,因此如果您调用sample(..)它将调用视图函数

通常模型以大写字母开头,虽然不是必需的,但它更容易区分模型视图的名称。因此,您可以将模型定义为:

from django.db import models

#Create your models here.
class Problem(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    title = models.CharField(max_length=30)
    difficulty = models.CharField(max_length=10)
    description = models.TextField()
    solution = models.TextField()
    code = models.TextField()

    def __str__(self):
        return self.name

class Sample(models.Model):
    firstname = models.CharField(max_length=20)
    email = models.EmailField()
    rno = models.IntegerField(primary_key=True)

    def __str__(self):
        return self.firstname

在视图中,您可以将模型用于:

from .models import Problem, Sample

def problems(request):
    name=request.POST['name']
    title=request.POST['title']
    difficulty=request.POST['example']
    description=request.POST['description']
    solution=request.POST['solution']
    code=request.POST['code']
    question = Problem.objects.create(
        name=name,
        title=title,
        difficulty=difficulty,
        description=description,
        solution=solution,
        code=code
    )
    return render(request,'thanks.html')


def sample(request):
    name=request.POST['name']
    email=request.POST['email']
    rno=request.POST['rno']
    a = Sample.objects.create(firstname=name,email=email,rno=rno)
    return render(request,'example.html',{'name':name})

话虽如此,通常最好使用ModelForm[Django-doc](或至少使用 aForm来处理输入),这使得它更不容易出错,并且更方便处理数据。

注意:如果 POST 请求成功,您应该制作一个redirect [Django-doc] 来实现Post/Redirect/Get模式 [wiki]。这样可以避免在用户刷新浏览器时发出相同的 POST 请求。


推荐阅读