Django小项目——简单的天气查询网站

5 篇文章 0 订阅
1 篇文章 0 订阅

 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、运行结果

  • 7
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
当然,我可以帮你创建一个简单Django项目。首先,确保你已经安装了 PythonDjango。 下面是一个简单的步骤,帮助你在 PyCharm 中创建和运行 Django 项目: 1. 打开 PyCharm,点击 "Create New Project" 创建一个新项目。 2. 在左侧面板中选择 "Django"。 3. 在右侧面板中,输入项目的名称和位置,然后点击 "Create"。 4. 在弹出的窗口中,选择 "Django version",并填写 "Project name" 和 "Application name"。 5. 点击 "Create",PyCharm 将会为你创建一个基本的 Django 项目结构。 6. 在 PyCharm 的底部工具栏中,点击 "Terminal",打开终端。 7. 在终端中,输入以下命令来创建数据库迁移文件: ``` python manage.py makemigrations ``` 8. 运行以下命令来应用数据库迁移: ``` python manage.py migrate ``` 9. 创建一个 Django 超级用户,输入以下命令并按照提示操作: ``` python manage.py createsuperuser ``` 10. 创建一个 Django 视图,打开 `views.py` 文件,并添加以下代码: ```python from django.http import HttpResponse def hello(request): return HttpResponse("Hello, Django!") ``` 11. 创建一个 Django 路由,打开 `urls.py` 文件,并添加以下代码: ```python from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello, name='hello'), ] ``` 12. 启动 Django 开发服务器,输入以下命令: ``` python manage.py runserver ``` 13. 在浏览器中访问 `http://localhost:8000/hello/`,你应该能看到 "Hello, Django!" 的输出。 这只是一个简单Django 项目示例,你可以根据自己的需求进行扩展和修改。希望对你有所帮助!如果你有任何问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值