概念
本文在上一文章搭建完數據庫,以及創建好項目之后,以及前端靜態文件后,對項目的首頁功能開發。
后端代碼編寫
這里我們使用pymysql模塊對數據庫進行操作,獲取數據。因此需要在dos窗口使用以下指令下載該庫文件
pip install pymysql
在views.py文件中創建方法,連接數據庫,并獲取首頁需要的數據
# Create your views here.
con = pymysql.connect(host="127.0.0.1", port=3306,user="root", password="admin",db="cookieshop", charset="utf8")
# 獲得游標
cursor = con.cursor()
def getGoodsList(type):# 根據類型查詢商品列表信息cursor.execute("select g.id,g.name,g.cover,g.price,t.name as typename from recommend r,goods g,type t where type=%s and r.goods_id=g.id and g.type_id=t.id",(type))newList=cursor.fetchall()ns=[]for new in newList:n={"id":new[0],"name":new[1],"cover":new[2],"price":new[3],"typename":new[4]}ns.append(n)return ns
def index(request):# 查詢所有蛋糕商品分類名稱cursor.execute("select * from type")types=cursor.fetchall()ts=[]for type in types:t=Type(type[0],type[1])ts.append(t)# 查詢推薦欄的商品信息cursor.execute("select g.id,g.name,g.cover,g.price from recommend r,goods g where r.goods_id=g.id")scrolls=cursor.fetchall()ss=[]for scroll in scrolls:s={"id":scroll[0],"name":scroll[1],"cover":scroll[2],"price":scroll[3]}ss.append(s)newList=getGoodsList(3)hotList=getGoodsList(2)return render(request,"index.html",{"typeList":ts,"scroll":ss,"newList":newList,"hotList":hotList} )
在models.py文件中創建Type實體類
#商品分類表
class Type(object):def __init__(self,id,name):""":param id: 商品分類id:param name: 商品分類名稱"""self.id=idself.name=name
前端頁面搭建
在子項目中創建templates文件夾,在該文件夾下創建header.html,foot.html以及index.html作為首頁的前端內容
header.html
<div class="header"><div class="container"><nav class="navbar navbar-default" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><h1 class="navbar-brand"><a href="/index"></a></h1></div><!--navbar-header--><div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"><ul class="nav navbar-nav"><li><a href="/index"{% if flag == 1 %}class="active"{% endif %}>首頁</a></li><li class="dropdown"><a href="#" class="dropdown-toggle{% if flag == 2 %}active{% endif %}" data-toggle="dropdown">商品分類<b class="caret"></b></a><ul class="dropdown-menu multi-column columns-2"><li><div class="row"><div class="col-sm-12"><h4>商品分類</h4><ul class="multi-column-dropdown"><li><a class="list" href="/goods_list">全部系列</a></li>{% for t in typeList%}<li><a class="list" href="/goods_list?typeid={{ t.id }}">{{t.name}}</a></li>{% endfor %}</ul></div></div></li></ul></li><li><a href="/goodsrecommend_list?type=2" {% if flag == 3 and t == 2 %} class="active" {% endif %}>熱銷</a></li><li><a href="/goodsrecommend_list?type=3" {% if flag == 3 and t == 3 %} class="active" {% endif %}>新品</a></li>{% if user %}<li><a href="/order_list" {% if flag == 5 %} class="active" {% endif %}>我的訂單</a></li><li><a href="/user_center.jsp" {% if flag == 4 %} class="active" {% endif %}>個人中心</a></li><li><a href="/user_logout" >退出</a></li>{% else %}<li><a href="/user_register.jsp" {% if flag == 10 %} class="active" {% endif %}>注冊</a></li><li><a href="/user_login.jsp" {% if flag == 9 %} class="active" {% endif %}>登錄</a></li>{% endif %}{% if user and user.isadmin %}<li><a href="/admin/index.jsp" target="_blank">后臺管理</a></li>{% endif %}</ul><!--/.navbar-collapse--></div><!--//navbar-header--></nav><div class="header-info"><div class="header-right search-box"><a href="javascript:;"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></a><div class="search"><form class="navbar-form" action="/goods_search"><input type="text" class="form-control" name="keyword"><button type="submit" class="btn btn-default{% if flag == 7 %}active{% endif %}" aria-label="Left Align">搜索</button></form></div></div><div class="header-right cart"><a href="goods_cart.jsp"><span class="glyphicon glyphicon-shopping-cart{% if flag == 8 %}active{% endif %}" aria-hidden="true"><span class="card_num">{% if order %}{{ order.amount }}{% else %}0{% endif %}</span></span></a></div><div class="clearfix"> </div></div><div class="clearfix"> </div></div>
</div>
foot.html
<div class="footer"><div class="container"><div class="text-center"></div></div>
</div>
index.html
<!DOCTYPE html>
<html>
<head>{% load static %}<title>首頁</title><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link type="text/css" rel="stylesheet" href="{% static 'css/bootstrap.css' %}"><link type="text/css" rel="stylesheet" href="{% static 'css/style.css' %}"><script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script><script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script><script type="text/javascript" src="{% static 'layer/layer.js' %}"></script><script type="text/javascript" src="{% static 'js/cart.js' %}"></script>
</head>
<body>
<!--header-->
{% include "header.html" with flag=1 typeList=typeList %}
<!--banner--><div class="banner"><div class="container"><div id="carousel-example-generic" class="carousel slide" data-ride="carousel"><!-- Indicators --><ol class="carousel-indicators" id="olnum">{% for g in scroll %}{% if forloop.first %}<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>{% else %}<li data-target="#carousel-example-generic" data-slide-to="{{ forloop.counter0 }}"></li>{% endif %}{% endfor %}</ol><!-- Wrapper for slides --><div class="carousel-inner" role="listbox" id="lunbotu" style="width: 1242px; height: 432px;">{% for g in scroll %}{% if forloop.first %}<div class="item active"><h2 class="hdng"><a href="/goods_detail?id={{ g.id }}">{{ g.name }}</a><span></span></h2><p>今日精選推薦</p><a class="banner_a" href="javascript:;" onclick="buy({{ g.id }})">立刻購買</a><div class="banner-text"><a href="/goods_detail?id={{ g.id }}"><img src="{% static g.cover %}" alt="{{ g.name }}" width="350" height="350"></a></div></div>{% else %}<div class="item"><h2 class="hdng"><a href="/goods_detail?id={{ g.id }}">{{ g.name }}</a><span></span></h2><p>今日精選推薦</p><a class="banner_a" href="javascript:;" onclick="buy({{ g.id }})">立刻購買</a><div class="banner-text"><a href="/goods_detail?id={{ g.id }}"><img src="{% static g.cover %}" alt="{{ g.name }}" width="350" height="350"></a></div></div>{% endif %}{% endfor %}</div></div></div>
</div><!--//banner--><div class="subscribe2"></div><!--gallery-->
<div class="gallery"><div class="container"><div class="alert alert-danger">熱銷推薦</div><div class="gallery-grids">{% for g in hotList %}<div class="col-md-4 gallery-grid glry-two"><a href="/goods_detail?id={{ g.id }}"><img src="{% static g.cover %}" class="img-responsive" alt="{{ g.name }}" width="350" height="350"/></a><div class="gallery-info galrr-info-two"><p><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span><a href="/goods_detail?id={{ g.id }}">查看詳情</a></p><a class="shop" href="javascript:;" onclick="buy({{ g.id }})">立刻購買</a><div class="clearfix"></div></div><div class="galy-info"><p>{{ g.typeName }} > {{ g.name }}</p><div class="galry"><div class="prices"><h5 class="item_price">¥ {{ g.price }}</h5></div><div class="clearfix"></div></div></div></div>{% endfor %}</div><div class="clearfix"></div><div class="alert alert-info">新品推薦</div><div class="gallery-grids">{% for g in newList %}<div class="col-md-3 gallery-grid "><a href="/goods_detail?id={{ g.id }}"><img src="{% static g.cover %}" class="img-responsive" alt="{{ g.name }}"/></a><div class="gallery-info"><p><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span><a href="/goods_detail?id={{ g.id }}">查看詳情</a></p><a class="shop" href="javascript:;" onclick="buy({{ g.id }})">立刻購買</a><div class="clearfix"></div></div><div class="galy-info"><p>{{ g.typeName }} > {{ g.name }}</p><div class="galry"><div class="prices"><h5 class="item_price">¥ {{ g.price }}</h5></div><div class="clearfix"></div></div></div></div>{% endfor %}</div></div>
</div>
<!--//gallery--><!--subscribe-->
<div class="subscribe"></div>
<!--//subscribe-->
<!--footer-->
{% include "footer.html" %}
</body>
</html>
在主項目的urls.py文件中定義請求地址,用于瀏覽器訪問views.py文件
?path('',vc.index),
?在PyCharm的內置dos窗口中輸入以下指令啟動服務器
python manage.py runserver
瀏覽器中訪問該地址