zoukankan      html  css  js  c++  java
  • django提交post请求

    在做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>
  • 相关阅读:
    jQuery轮播图(一)轮播实现并封装
    openSUSE 12.3 默认启动项
    最大堆(最小堆)
    二叉树基本操作续二:前序、中序、后序遍历(非递归 迭代方式)
    二叉树基本操作续一:二叉树建立、节点数统计
    二叉树基本操作:前序、中序、后序遍历(递归方式)
    Android如何打印std::cout/printf(重定向stdout)
    textarea高度跟随文字高度而变化
    箭头函数与普通函数的区别
    浏览器兼容问题
  • 原文地址:https://www.cnblogs.com/meitian/p/4730868.html
Copyright © 2011-2022 走看看