在做post的时候,view.py用到了下面的方法,如果是POST的method,就通过request.POTST['XX']获得html中name为XX的值,然后将值save到数据库里
models.py:
from projectmanage.settings import DBNAME
from mongoengine import *
connect(DBNAME)
class project(Document):
name = StringField(max_length=120, required=True)
description = StringField(max_length=500, required=False)
pmember = StringField(max_length=120, required=False)
devmember = StringField(max_length=120, required=False)
testmember = StringField(max_length=120, required=False)
uimember = StringField(max_length=120, required=False)
status = StringField(max_length=120, required=False)
date = DateTimeField(required=False)
last_update = DateTimeField(required=False)
view.py:
def newproject(request):
# id = eval("request." + request.method + "['id']")
# post = project.objects(id=id)[0]
if request.method == 'POST':
name = request.POST['name']
description = request.POST['description']
pmember = request.POST['pmember']
devmember = request.POST['devmember']
testmember = request.POST['testmember']
uimember = request.POST['uimember']
post = project(name=name, pmember=pmember,devmember=devmember,testmember=testmember,uimember=uimember,status="测试中")
post.last_update = datetime.now()
post.save()
return render_to_response('newproject.html', locals(),context_instance=RequestContext(request))
遇到的问题1:提示找不到name
解决方法:在html的form里,需要提交的项目里加上"name"="XXX"
<form id="formid" method="POST" action="/index/" >
{% csrf_token %}
<table>
<tr>
<td>项目名称:</td>
<td>
<input id="projectid" name="name" type="text" value="{{ name }}">
</td>
</tr>
……
<div>
<button onclick="checkUser();" type="submit">保存</button>
<button>取消</button>
</div>
</form>
遇到的问题2:只是通过form中的action跳转了,但是没有提交post请求
解决方法:将action置为空(因为post的参数是传递给action中的地址的,所以如果我给的action地址是/index/,那么我的post的save操作实际上是应该写在/index/的view下的)
<form id="formid" method="POST" action="" >
……
</form>