帮助我解决以下问题(我只是初学者,(即使我可能没有以正确的方式提出问题))。 我还需要为您提供什么。
students()获得了意外的关键字参数'stu_name' 请求方法:POST 要求网址:http://127.0.0.1:8000/students
enter code here
Django版本:2.2.5 异常类型:TypeError 异常值: students()收到了意外的关键字参数'stu_name' 异常位置:学生的C:\ Users \ GAGAN \ Desktop \ tarkProject \ myapp \ views.py,第18行 Python可执行文件:C:\ Users \ GAGAN \ Anaconda3 \ envs \ djangoenv \ python.exe 的Python版本:3.7.5 Python路径: ['C:\ Users \ GAGAN \ Desktop \ tarkProject', 'C:\ Users \ GAGAN \ Anaconda3 \ envs \ djangoenv \ python37.zip', 'C:\ Users \ GAGAN \ Anaconda3 \ envs \ djangoenv \ DLLs', 'C:\ Users \ GAGAN \ Anaconda3 \ envs \ djangoenv \ lib', 'C:\ Users \ GAGAN \ Anaconda3 \ envs \ djangoenv', 'C:\ Users \ GAGAN \ Anaconda3 \ envs \ djangoenv \ lib \ site-packages'] 服务器时间:2020年1月1日,星期三06:23:38 +0000'''
views.py
from django.shortcuts import render
from .models import students
# Create your views here.
def home(request):
return render (request, 'myapp/home.html')
def contact(request):
return render (request, 'myapp/contact.html')
def students(request):
if request.method == 'POST':
name = request.POST.get('stu_name')
father = request.POST.get('stu_father')
mother = request.POST.get('stu_mother')
cl = request.POST.get('stu_class')
s = students(stu_name=name, stu_father=father, stu_mother=mother, stu_class=cl)
s.save()
return render (request, 'myapp/students.html')
else:
return render (request, 'myapp/students.html')
models.py
from django.db import models
# Create your models here.
class contact(models.Model):
stu_name = models.CharField(max_length=30)
stu_father = models.CharField(max_length=30)
stu_mother = models.CharField(max_length=30)
# stuClass = models.CharField(max_length=30)
def __str__(self):
return self.stu_name
class students(models.Model):
stu_name = models.CharField(max_length=30)
stu_father = models.CharField(max_length=30)
stu_mother = models.CharField(max_length=30)
stu_class = models.CharField(max_length=10)
def __str__(self):
return self.stu_name
答案 0 :(得分:0)
这里students(stu_name=name, stu_father=father, stu_mother=mother, stu_class=cl)
在调用您的函数,而不是实例化模型。因为,您的功能和型号名称是相同的。
解决方案:
students
更改为Student
python manage.py makemgirations
python manage.py migrate