django post 请求传参

来源:互联网 发布:天津app软件开发 编辑:程序博客网 时间:2024/06/05 14:44
node2:/django/mysite/blog#node2:/django/mysite/blog#cat views.py# -*- coding: utf-8 -*-from __future__ import unicode_literals# from django.shortcuts import render, render_to_responsefrom .models import *# Create your views here.from django.http import HttpResponsefrom django.template import loaderdef archive(req):  print 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'  print req  print type(req)  print req.POST  print '#############################'  print req.POST['username']  print req.POST['password']  print '#############################'  print 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'# get all blogpost objects  posts = BlogPost.objects.all()  print posts  print type(posts)  #print blog_list  template = loader.get_template('archive.html')  context = {  'posts':posts  }  print '------------------------------------------'  print  HttpResponse(template.render(context, req))  print '------------------------------------------'  return HttpResponse(template.render(context, req))调用接口:# !/usr/bin/env python# -*- coding: utf-8 -*-import urllib2import urllibimport cookielibimport jsonimport httplibimport timeimport reimport osimport requestsdef gettoken():    data = {'username': '99999@zjtlcb.com', 'password': '1234567'}    post_data = urllib.urlencode(data)  # 将post消息化成可以让服务器编码的方式    cj = cookielib.CookieJar()  # 获取cookiejar实例    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))    # 自己设置User-Agent(可用于伪造获取,防止某些网站防ip注入)    headers = {}    website = "http://192.168.137.3:8000/blog/"    req = urllib2.Request(website, post_data, headers)    content = opener.open(req)    s = content.read()  # linux下没有gbk编码,只有utf-8编码    print s    print type(s)gettoken()aaaaaaaaaaaaaaaaaaaaaaaaaaa<WSGIRequest: POST '/blog/'><class 'django.core.handlers.wsgi.WSGIRequest'><QueryDict: {u'username': [u'99999@zjtlcb.com'], u'password': [u'1234567']}>#############################99999@zjtlcb.com1234567#############################aaaaaaaaaaaaaaaaaaaaaaaaaaa<QuerySet [<BlogPost: BlogPost object>, <BlogPost: BlogPost object>, <BlogPost: BlogPost object>]><class 'django.db.models.query.QuerySet'>

原创粉丝点击