django post 请求传参

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_response
from .models import *
# Create your views here.
from django.http import HttpResponse
from django.template import loader

def 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 urllib2
import urllib
import cookielib
import json
import httplib
import time
import re
import os
import requests
def 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.com
1234567
#############################
aaaaaaaaaaaaaaaaaaaaaaaaaaa
<QuerySet [<BlogPost: BlogPost object>, <BlogPost: BlogPost object>, <BlogPost: BlogPost object>]>
<class 'django.db.models.query.QuerySet'>

原文地址:https://www.cnblogs.com/hzcya1995/p/13349428.html