Home > front end >  Questions correct posture: Django MultiValueDictKeyError add data
Questions correct posture: Django MultiValueDictKeyError add data

Time:04-15

Web site the user submits the data source, to obtain the user submits the data with the added to the database when error
The error code
Ob. Name=request. POST [' name ']
Error message

Baidu many solutions
Plan 1 is invalid and other mistakes:
Ob. Name=request. POST. Get [' name '] # plus False also like

Scheme 2 is invalid:
Ob. Name=request. Get [' name '] # (' name ') as well as for

Solution 3 is invalid:
Ob. Name='name in the request. The POST


I don't know the place out of the question, the bosses, look comfortable with, below is the code section
Models. Py code
 
The from the django. Db import models
The from datetime import datetime
# the Create your models here.
Class Users (models. Model) :
Name=models. CharField (max_length=32)
Sex=models. CharField (max_length=16)
Age=models. IntegerField (default=20)
Dept=models. CharField (max_length=32)


Urls. Py code
 
The from the django. Urls import path
From the import views
Urlpatterns=[
# path (' admin/' admin. Site. Urls),
Path (' views. The index, name='index'),
# configuration the users information operation routing
Path (' users' views. IndexUsers, name='indexUsers'),
Path (' the users/add/' views. AddUsers, name='addUsers'),
Path (' the users/insert/' views. InsertUsers, name='insertUsers'),
The path (' the users/del/& lt; Int: uid> 'views. DelUsers, name=' delUsers'),
The path (' the users/edit/& lt; Int: uid> 'views. EditUsers, name=' editUsers'),
Path (' the users/update 'views. UpdateUsers, name=' updateUsers'),
]


Views. Py code
 
The from the django. Shortcuts import render
The from the django. HTTP import HttpResponse
The from myapp. Models import Users
# the Create your views here.

Def index (request) :
# implement operations mModel

# to add data
# # ob=Users () to instantiate a new object (empty object)
# ob. Name='Cathy'
# ob. Sex='male'
33 # ob. Age=
# ob. Dept='CC'
# # ob. The save () a new object is added, the existing object is modified

# delete data
# # ob=Users. Objects for User Model object
# DE=ob. Get (id=4) # id value is 4 data information
# print (DE. Name)
# # DE. The delete () to delete data

# modify data
# ob=Users. Objects. The get (id=3)
# print (ob) name)
# ob. Name='liu'
26 # ob. Age=
# ob. The save ()

# data query
# ob=Users. Objects
# # ulist=ob. All () all access to all data
# ulist=ob. Filter (name='* *') # filter to filter the data, get the name value of xiaoliu information
# ulist=ob. Filter (age__gt=20) # for age value is greater than 20 information
# ulist=ob. Filter (age__gte=20) # for age values greater than or equal to 20 information
# ulist=ob. Filter (age__lt=20) # for less than 20 age value information
# ulist=ob. Filter (age__lte=20) # for age value less than or equal to 20 information
# ulist=ob. Order_by (' age ') # according to the age ascending order
# ulist=ob. Order_by (" age ") [3] : # in the age before the inside of the three data
# for u in ulist:
# print (u.i d, u.n ame, u.s ex, u.a ge, u.d ept)
Return an HttpResponse (' homepage & lt; Br/& gt; ')

Reading the user information #
Def indexUsers (request) :
Try:
Ulist=Users. Objects. All ()
The context={' userslist: ulist}
Return render (request, 'myapp/users/index HTML', the context) # loading template
Except:
Return an HttpResponse (' not found the user information! ')
# to load the add user information form
Def addUsers (request) :
Return render (request, 'myapp/users/add HTML')
# perform user information to add
Def insertUsers (request) :
Ob=Users ()
# ob. Name='name in the request. The POST
# ob. Name=request. Get [' name ']
# ob. Name=request. POST. Get [' name ']
Ob. Name=request. POST [' name ']
Ob. Sex='sex' in the request. POST
Ob. Age='age' in the request. POST
Ob. Dept='dept request. In the POST
Ob. The save ()
# context={' info ':' add success! '}
# the try:
# ob=Users ()
# ob. Name=request. POST [' name ']
# ob. Sex=request. POST [' sex ']
# ob. Age=request. POST [' age ']
# ob. Dept=request. POST [' dept ']
# ob. The save ()
# context={' info ':' add success! '}
# the except:
# context={' info ':' add failure! '}
# return render (request, 'myapp/users/info. The HTML', the context)

# perform user information delete
Def delUsers (request, uid=0) :
Ob=Users. Objects. Get # (id=uid) obtain the data to delete
DE. Delete delete data () #
# to load user information to modify the form
Def editUsers (request, uid=0) :
Pass
# perform user information change
Def updateUsers (request) :
Pass


And the user submits the information page code
 


<meta charset="utf-8" & gt;
<meta name="viewport" Content="width=device - width, initial - scale=1.0" & gt;
Title

<body>






  • Related