1.在views.py中
from django.shortcuts import render
import requests
# Create your views here.
from django.http import HttpResponse
def myMovie(request):
# return render(request,'index.html')
# 獲取城市信息
# 使用城市拼接url 請求電影信息
# 首次進入這個方法 直接請求地址信息
if request.method =='GET':
url = 'https://api.map.baidu.com/location/ip?ak=KHkVjtmfrM6NuzqxEALj0p8i1cUQot6Z'
response = requests.get(url)
response_dic = response.json()
city = response_dic['content']['address_detail']['city']
else:
# 如果發現請求方式是post方式 就可以認爲是form表單發送的請求
# 那麼直接獲取form表單裏面name值 爲city的input標籤 裏面輸入的內容
city = request.POST['city']
# 拼接url 獲取全部電影信息
movie_url = 'http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location={}&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json'.format(city)
movie_data = requests.get(movie_url).json()
# 判斷電影信息是否請求成功
if movie_data['error'] == 0:
all_movies = movie_data['result']['movie']
context = {
'city':city,
'all_movies':all_movies,
}
# return render(request,'index.html',context)
else:
context = {
'city':city,
'error':'無法獲取當前城市電影信息'
}
return render(request,'index.html',context)
2.在urls.py中設置路徑
from django.contrib import admin
from django.urls import path
from myApp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('movie/',views.myMovie)
]
3.在html文件中
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="{% static 'css/bootstrap.css'%}">
<title>{{city}}即將上映電影</title>
<style>
section{
/*border:1px solid red;*/
height:240px;
position: relative;
}
img{
height:150px;
}
.right{
display: inline-block;
/*border:1px solid blue;*/
position: absolute;
left:200px;
top:40px;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse"
data-target="#example-navbar-collapse">
<span class="sr-only">切換導航</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">電影首頁</a>
</div>
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">加入我們</a></li>
<li><a href="#">關於我們</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
聯繫我們
</a>
</li>
</ul>
<!--跳轉到urls.py裏面的 /movie 路徑-->
<!--/movie 跳轉到 views.py 裏面的myMovie 方法裏面-->
<form action="/movie/" method="POST" class="navbar-form navbar-left" role="search">
{% csrf_token%}
<div class="form-group">
<input name="city" type="text" class="form-control" placeholder="請輸入城市進行搜索">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
</div>
</div>
</nav>
<div class="container">
{% if error %}
<h1>{{error}}</h1>
{% endif%}
{% for movie in all_movies %}
<section class="col-lg-6 col-md-6 col-sm-12">
<h4>{{movie.movie_name}}</h4>
<img src="{{movie.movie_picture}}" alt="">
<div class="right">
<div>類型:{{movie.movie_type}}</div>
<div>上映日期:{{movie.movie_release_date}}</div>
<div>演員:{{movie.movie_starring}}</div>
<div>評分:{{movie.movie_score}}</div>
<div>導演:{{movie.movie_director}}</div>
<div>類型:{{movie.movie_tags}}</div>
</div>
</section>
{% endfor%}
</div>
<script src="{% static 'js/jquery.js'%}"></script>
<script src="{% static 'js/bootstrap.js'%}"></script>
</body>
</html>
注意:由於用到bootstrap中的模型,所有需要先引入bootstrap靜態文件
在html文件最上方將靜態文件 進行加載
{% csrf_token %} 防止跨域