MultiValueDictError Django

9vw9lbht  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(80)

我想在数据库中存储注册数据。当我点击提交按钮时,我在调试页面上得到以下错误:

将应用所有迁移。

models.py

from django.db import models
# Create your models here.
class register(models.Model):
    your_name = models.CharField(max_length=30)
    Email= models.EmailField(max_length=254)
    phone= models.IntegerField(max_length=13)
    account = models.CharField(max_length=10)
    message = models.CharField(max_length=300)

回溯

Internal Server Error: /register
Traceback (most recent call last):
  File "J:\good\test\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "J:\good\test\Lib\site-packages\django\utils\deprecation.py", line 136, in __call__
    response = self.process_response(request, response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "J:\good\test\Lib\site-packages\django\middleware\common.py", line 108, in process_response
    return self.response_redirect_class(self.get_full_path_with_slash(request))
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "J:\good\test\Lib\site-packages\django\middleware\common.py", line 87, in get_full_path_with_slash
    raise RuntimeError(
RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/register/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
[29/Sep/2023 20:35:21] "POST /register HTTP/1.1" 500 70661

我试图达到的观点是功能寄存器

views.py

from django.shortcuts import render,redirect
from django.contrib.auth.models import User
# Create your views here.
def index(request):
    return render(request,"index.html")
def contact(request):
    return render(request,"contact.html")

def ideagained(request):
    return render(request,"idea_gained.html")

def idea(request):
    return render(request,"idea.html")

def ouridea(request):
    return render(request,"our_idea.html")

def aboutpage(request):
    return render(request,"aboutpage.html")

def register(request):
        your_name=request.POST['name']
        email=request.POST['email']
        phone_number=request.POST['phone_number']
        account=request.POST['account']
        message=request.POST['message']
         

        user=user.objects.create_user(name=name)
        print("we wil call back you")
        return redirect('/')
wlp8pajw

wlp8pajw1#

关键字name不存在。

from .models import User

def register(request):
    your_name=request.POST['name']
    email=request.POST['email']
    phone_number=request.POST['phone_number']
    account=request.POST['account']
    message=request.POST['message']
    user=User.objects.create(your_name=your_name, email=email, phone_number=phone_number, account=account, message=message)
    print("we wil call back you")
    return redirect('/')

请注意,我们倾向于使用CamelCase命名模型,使用CamelCase命名字段。通常模特也是一个名词。你的模型应该是:

class User(models.Model):
    your_name = models.CharField(max_length=30)
    email= models.EmailField(max_length=254)
    phone= models.IntegerField(max_length=13)
    account = models.CharField(max_length=10)
    message = models.CharField(max_length=300)

相关问题