def test3(request) :
    if request.method == 'POST' :
        po = request.POST.copy()

         #------------------------------------------------------
        code = request.GET.get('code', '')
        if code == '':
           return HttpResponse('请您先授权')
        scope = request.GET.get('scope', None)

        url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+WEIXIN_APPID+'&secret='+WEIXIN_APPSECRET+'&code='+code+'&grant_type=authorization_code'
        resp, content = my_get(url)
        dict_user = parse_Json2Dict(content)
        print scope
            #if state == 'snsapi_base':
               # return render(request, 'user_info.html', dict_user)
            #if state == 'snsapi_userinfo':
        url = 'https://api.weixin.qq.com/sns/userinfo?access_token='+dict_user['access_token']+'&openid='+dict_user['openid']+'&lang=zh_CN'
        res, content = my_get(url)
        dict_user2 = parse_Json2Dict(content)
        dict_user.update(dict_user2)
        request.session['OpenID'] = dict_user['openid']

        #----------------------------------------------

        po['OpenID'] = dict_user['openid']
        userform = Register(po)
    
        print request.POST
        #print request.POST
        if userform.is_valid() :
            userform.save()
#------------------------------------------------------------------------------
            
            #request.session['昵称'] = dict_user['nickname']
            #request.session['性别'] = dict_user['sex']
            #request.session['省份'] = dict_user['province']
            #request.session['城市'] = dict_user['city']
           # request.session['OpenID'] = request.POST.get('OpenID')
#------------------------------------------------------------------------------

            #print userform.cleaned_data
            #return render(request,'project_1/test.html')
            #return HttpResponseRedirect('project_1/requestInfo.html')
            #return render_to_response('project_1/requestInfo.html')
            #return render(request,'project_1/requestInfo.html')
            #return HttpResponseRedirect()
            return render(request,'project_1/requestInfo.html',{'requestform':RequestForm()})
        else :
            print userform.errors
    else :
        userform = Register(initial = {'OpenID':'OpenID'})
    return render(request,'project_1/test3.html', {'userform' : userform})
Exemplo n.º 2
0
def test3(request) :
	if request.method == 'POST' :
		userform = Register(request.POST)
		#print request.POST
		if userform.is_valid() :
			userform.save()
#------------------------------------------------------------------------------
			request.session['OpenID'] = request.POST.get('OpenID')
#------------------------------------------------------------------------------

			#print userform.cleaned_data
			#return render(request,'project_1/test.html')
			#return HttpResponseRedirect('project_1/requestInfo.html')
			#return render_to_response('project_1/requestInfo.html')
			#return render(request,'project_1/requestInfo.html')
			#return HttpResponseRedirect()
			return render(request,'project_1/requestInfo.html',{'requestform':RequestForm()})
		else :
			print userform.errors
	else :
		userform = Register()
	return render(request,'project_1/test3.html', {'userform' : userform})
Exemplo n.º 3
0
def test3(request):
    if request.method == 'POST':
        po = request.POST.copy()

        #------------------------------------------------------
        code = request.GET.get('code', '')
        if code == '':
            return HttpResponse('请您先授权')
        scope = request.GET.get('scope', None)

        url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + WEIXIN_APPID + '&secret=' + WEIXIN_APPSECRET + '&code=' + code + '&grant_type=authorization_code'
        resp, content = my_get(url)
        dict_user = parse_Json2Dict(content)
        print scope
        #if state == 'snsapi_base':
        # return render(request, 'user_info.html', dict_user)
        #if state == 'snsapi_userinfo':
        url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + dict_user[
            'access_token'] + '&openid=' + dict_user['openid'] + '&lang=zh_CN'
        res, content = my_get(url)
        dict_user2 = parse_Json2Dict(content)
        dict_user.update(dict_user2)
        request.session['OpenID'] = dict_user['openid']

        #----------------------------------------------

        po['OpenID'] = dict_user['openid']
        userform = Register(po)

        print request.POST
        #print request.POST
        if userform.is_valid():
            userform.save()
            #------------------------------------------------------------------------------

            #request.session['昵称'] = dict_user['nickname']
            #request.session['性别'] = dict_user['sex']
            #request.session['省份'] = dict_user['province']
            #request.session['城市'] = dict_user['city']
            # request.session['OpenID'] = request.POST.get('OpenID')
            #------------------------------------------------------------------------------

            #print userform.cleaned_data
            #return render(request,'project_1/test.html')
            #return HttpResponseRedirect('project_1/requestInfo.html')
            #return render_to_response('project_1/requestInfo.html')
            #return render(request,'project_1/requestInfo.html')
            #return HttpResponseRedirect()
            return render(request, 'project_1/requestInfo.html',
                          {'requestform': RequestForm()})
        else:
            print userform.errors
    else:
        userinfo = {}
        userinfo = user_info(request)
        print userinfo
        print 'XXXXXXXXXXXXXXXXXXXXX'
        openid = userinfo['openid']
        print openid
        print 'YYYYYYYYYYYYYYYYYYY'
        ret = UserProfile.objects.filter(OpenID=openid)
        print ret
        print 'zzzzzzzzzzzzzzzzzzz'
        if not ret:
            userform = Register(initial={'OpenID': 'OpenID'})
        else:
            userform = Register(initial={'OpenID': openid})
            print userform
            print 'bbbbbbbbbbbbbbbbbbb'
    return render(request, 'project_1/test3.html', {'userform': userform})