python开发⼊门篇-获取⽤户信息(⼀)1.打开pycharm-->新建django项⽬
项⽬⽂档结构如下:
执⾏迁移命令⽣成数据库
python manage.py makemigrations
python manage.py migrate
2.安装框架wechatpy
pip install wechatpy
# with cryptography (推荐)
pip install wechatpy[cryptography]
# with pycryptodome
pip install wechatpy[pycrypto]
3.内外⽹穿透
设置内⽹ip和端⼝
4.登录服务号设置可信域名-IP⽩名单
1.设置-->设置
2.点击业务域名
3.浏览器访问如下,确认⽆误点击保存按钮
4.以此设置可信域名
5.设置IP⽩名单
设置-->安全中⼼-->点击查看
5.获取⽤户信息
服务号后台-->开发-->基本设置-->查看服务号appid
扫码查看实例:
效果图:
源码
wechatDemo-->urls.py
"""wechatDemo URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see: docs.djangoproject/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import viewsdjango项目实例
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
ib import admin
from django.urls import path
from app import views
urlpatterns = [
path('admin/', admin.site.urls),
# 认证⽂件建议使⽤nginx配置
path('MP_', views.wechatauth),
# 登录页⾯userinfo
path('userinfo', views.userinfo),
]
app-->views.py
from wechatpy.oauth import WeChatOAuth
from django.shortcuts import render, redirect
from django.http import JsonResponse, HttpResponse, HttpResponseRedirect # Create your views here.
AppID = "服务号APPID"
AppSecret = "服务号AppSecret"
# 认证⽂件,建议通过nginx配置
def wechatauth(request):
return HttpResponse("b1reLtO1xRzEjqxJ")
# 定义授权装饰器
def getWeChatOAuth(redirect_url):
return WeChatOAuth(AppID, AppSecret, redirect_url, 'snsapi_userinfo')
def oauth(method):
def warpper(request):
if ('user_info', None) is None:
code = ('code', None)
wechat_oauth = _raw_uri())
url = wechat_oauth.authorize_url
print(url)
if code:
try:
wechat_oauth.fetch_access_token(code)
user_info = _user_info()
print(user_info)
except Exception as e:
print(str(e))
# 这⾥需要处理请求⾥包含的 code ⽆效的情况
# abort(403)
else:
# 建议存储在⽤户表
request.session['user_info'] = user_info
else:
return redirect(url)
return method(request)
return warpper
# 获取⽤户信息UserInfo
@oauth
def userinfo(request):
user_info = ('user_info')
return render(request, 'userinfo.html', {"user_info": user_info})
templates-->userinfo.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>⽤户信息</title>
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
<meta content="yes" name="apple-mobile-web-app-capable"/>
<meta content="black" name="apple-mobile-web-app-status-bar-style"/>
<meta content="telephone=no" name="format-detection"/>
<!-- 最新版本的 Bootstrap 核⼼ CSS ⽂件 -->
<link rel="stylesheet" href="cdn.jsdelivr/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 可选的 Bootstrap 主题⽂件(⼀般不⽤引⼊) -->
<link rel="stylesheet" href="cdn.jsdelivr/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核⼼ JavaScript ⽂件 -->
<script src="cdn.jsdelivr/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</head>
<body>
<div class="container-fluid">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>属性</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<code>头像</code>
</th>
<td><img src="{{ user_info.headimgurl }}" alt=""/></td>
</tr>
<tr>
<th scope="row">
<code>openid</code>
</th>
<td>{{ user_info.openid }}</td>
</tr>
<tr>
<th scope="row">
<code>昵称</code>
</th>
<td>{{ user_info.nickname }}</td>
</tr>
<tr>
<th scope="row">
<code>性别</code>
</th>
<td>{{ user_info.sex }}</td>
</tr>
<tr>
<th scope="row">
<code>语⾔</code>
</th>
<td>{{ user_info.language }}</td>
</tr>
<tr>
<th scope="row">
<code>城市</code>
</th>
<td>{{ untry }}-{{ user_info.province }}-{{ user_info.city }}</td> </tr>
<tr>
<th scope="row">
<code>标签</code>
</th>
<td>{{ user_info.privilege }}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论