1.首先应该建立一个Django项目,注意要选择Existing interpreter

2、创建app,--python manage.py startapp
.
3、settings.py文件需要配置一下
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
使可以找到文件包


4、views中的代码
from django.shortcuts import render
import requests
import json
def tq(request):
ip_api = 'https://api.map.baidu.com/location/ip?ak=KHkVjtmfrM6NuzqxEALj0p8i1cUQot6Z'
response = requests.get(ip_api)
city_dict = json.loads(response.text)
nowcity = city_dict['content']['address_detail']['city']
if request.method == 'POST':
city = request.POST['city']
str = 'http://api.map.baidu.com/telematics/v3/weather?location=' + city + '&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
else:
str = 'http://api.map.baidu.com/telematics/v3/weather?location=' + nowcity + '&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
# city = input("请输入要查询的城市信息(例如:郑州,北京,上海):")
response = requests.get(str)
json_str = response.text
json_dict = json.loads(json_str)
data_dict = json_dict['results']
lin = data_dict[0]
index = lin['index']
w_date = lin['weather_data']
city = data_dict[0]['currentCity']
pm = data_dict[0]['pm25']
print('城市:{}; pm25:{};'.format(city, pm))
nowtq = w_date[0]
onetq = w_date[1]
twotq = w_date[2]
threetq = w_date[3]
for item_dict1 in w_date:
date = item_dict1['date']
temperature = item_dict1['temperature']
weather = item_dict1['weather']
wind = item_dict1['wind']
print('时间:{}; 温度:{}; 天气:{}; 风向:{}'.format(date, temperature, weather, wind))
context = {
'city': city,
'weather_list': w_date,
'nowtq': nowtq,
'onetq': onetq,
'twotq': twotq,
'threetq': threetq,
'nowcity': nowcity,
}
return render(request, template_name='weather.html', context=context)
5、html代码
<html>
<head>
<title>天气预报</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/static/weather.css">
</head>
<body>
<form action="/weather/" method="POST">
{% csrf_token %}
<input name="city" type="text" placeholder="请输入要查询的城市名称">
<button type="submit">查询</button>
</form>
<header>{{ city }}</header>
<main>
<img class="icon" src="/static/snow4.png">
<div class="tempers">{{ nowtq.date }} </div>
<div class="weather">{{ nowtq.temperature }}</div>
<div class="wind">{{ nowtq.weather }}</div>
<div class="current">{{ nowtq.wind }}</div>
</main>
<footer>
{# {% for item,index in weather_list %}#}
<section>
<div class="week">{{ onetq.date }}</div>
<img weight="60px" height="40px" src="/static/snow4.png">
<div class="temper">{{ onetq.temperature }}</div>
<div class="weather">{{ onetq.weather }}</div>
<div class="wind">{{ onetq.wind }}</div>
</section>
<section>
<div class="week">{{ twotq.date }}</div>
<img weight="60px" height="40px" src="/static/snow4.png">
<div class="temper">{{ twotq.temperature }}</div>
<div class="weather">{{ twotq.weather }}</div>
<div class="wind">{{ twotq.wind }}</div>
</section>
<section>
<div class="week">{{ threetq.date }}</div>
<img weight="60px" height="40px" src="/static/snow4.png">
<div class="temper">{{ threetq.temperature }}</div>
<div class="weather">{{ threetq.weather }}</div>
<div class="wind">{{ threetq.wind }}</div>
</section>
{# {% endfor %}#}
<div style="clear:left;"></div>
</footer>
</body>
</html>
6、urls中
urlpatterns = [
path('admin/', admin.site.urls),
path('weather/', tq),
]
7、运行结果


7714

被折叠的 条评论
为什么被折叠?



