django项目——爱鲜蜂
2018-06-06 本文已影响0人
Jason_c8d4
项目源代码地址:https://github.com/SongJiaxin95/axf
项目部署地址:118.25.211.249/
项目图:
image.png一、创建项目
创建虚拟环境
virtualenv --no-site-packages django_axf_env
进入虚拟环境
cd django_axf_env/Scripts
activate
在虚拟环境中安装项目所需要的包
pip install Django==1.11
pip install PyMySQL
pip install Pillow
创建django项目
不要在虚拟文件夹下创建
d:
django-admin startproject axf
创建app
cd axf
python manage.py startapp app
打开pycharm,打开d盘下创建的axf项目,file>settings>project:axf>project interpreter>齿轮图标>add python Interpreter>Virtualenv Environment>Existing environment>选择刚才所创建虚拟环境的目录>django_axf_env/Scripts/python.exe>ok
二、配置
settings.py
- 在INSTALLED_APPS中添加'app'
- TEMPLATES的DIRS改为
'DIRS': [os.path.join(BASE_DIR, 'templates')],
- DATABASES配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 使用mysql
'NAME': 'axf', # 数据库名
'USER': 'root', # 用户名
'PASSWORD': 'root', # 密码
'PORT': '3306', # 端口
'HOST': 'localhost' # 主机地址
}
}
- LANGUAGE_CODE = 'zh-hans'
- TIME_ZONE = 'Asia/Shanghai'
- 配置静态文件
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR,'static') # 项目部署时需要
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
- 配置上传文件路径
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
然后在项目下新建三个文件夹templates、static、media。
__init__.py
import pymysql
pymysql.install_as_MySQLdb()
debug
点击菜单中的Run>Debug>Edit>左上角绿色加号>python>name 随便取,script path选择项目下的manage.py文件,parameters填 'runserver 8000'>ok
image.png
image.png
三、创建模型
app下的models.py
from django.db import models
# Create your models here.
class Main(models.Model):
img = models.CharField(max_length=200) # 图片
name = models.CharField(max_length=100) # 名称
trackid = models.CharField(max_length=16) # id
class Meta:
abstract = True
class MainWheel(Main):
class Meta:
db_table = 'axf_wheel'
class MainNav(Main):
class Meta:
db_table = 'axf_nav'
class MainHotGoods(Main):
class Meta:
db_table = 'axf_hotgoods'
class MainShop(Main):
class Meta:
db_table = 'axf_shop'
class MainShow(Main):
categoryid = models.CharField(max_length=16,null=True)
brandname = models.CharField(max_length=100,null=True)
img1 = models.CharField(max_length=200)
childcid1 = models.CharField(max_length=16,null=True)
productid1 = models.CharField(max_length=16,null=True)
longname1 = models.CharField(max_length=100)
price1 = models.FloatField(default=0)
marketprice1 = models.FloatField(default=1)
img2 = models.CharField(max_length=200)
childcid2 = models.CharField(max_length=16,null=True)
productid2 = models.CharField(max_length=16,null=True)
longname2 = models.CharField(max_length=100)
price2 = models.FloatField(default=0)
marketprice2 = models.FloatField(default=1)
img3 = models.CharField(max_length=200)
childcid3 = models.CharField(max_length=16,null=True)
productid3 = models.CharField(max_length=16,null=True)
longname3 = models.CharField(max_length=100)
price3 = models.FloatField(default=0)
marketprice3 = models.FloatField(default=1)
class Meta:
db_table = 'axf_mainshow'
# 闪购左侧模型
class FoodType(models.Model):
typeid = models.CharField(max_length=60)
typename = models.CharField(max_length=100)
childtypenames = models.CharField(max_length=200)
typesort = models.IntegerField(default=1)
class Meta:
db_table = 'axf_foodtypes'
class Goods(models.Model):
productid = models.CharField(max_length=16) # 商品的id
productimg = models.CharField(max_length=200) # 商品的图片
productname = models.CharField(max_length=100) # 商品的名称
productlongname = models.CharField(max_length=200) # 商品的规格
isxf = models.IntegerField(default=1)
pmdesc = models.CharField(max_length=100)
specifics = models.CharField(max_length=100) # 规格
price = models.FloatField(default=0) # 商品的折后价格
marketprice = models.FloatField(default=1) # 商品的原价
categoryid = models.CharField(max_length=16) # 分类的id
childcid = models.CharField(max_length=16) # 子分类的id
childcidname = models.CharField(max_length=100) # 子分类的名称
dealerid = models.CharField(max_length=16)
storenums = models.IntegerField(default=1) # 排序
productnum = models.IntegerField(default=1) # 销量排序
class Meta:
db_table = 'axf_goods'
class UserModel(models.Model):
username = models.CharField(max_length=32,unique=True)
password = models.CharField(max_length=256)
email = models.CharField(max_length=64, unique=True)
# Falsh代表女
sex = models.BooleanField(default=False)
icon = models.ImageField(upload_to='icon')
is_delete = models.BooleanField(default=False)
ticket = models.CharField(max_length=30,null=True)
class Meta:
db_table = 'axf_user'
# 购物车
class CarModel(models.Model):
user = models.ForeignKey(UserModel) # 关联用户
goods = models.ForeignKey(Goods) # 关联商品
c_num = models.IntegerField(default=1) # 商品的个数
is_select = models.BooleanField(default=True) # 是否选择商品
class Meta:
db_table = 'axf_cart'
class OrderModel(models.Model):
user = models.ForeignKey(UserModel) # 关联用户
o_num = models.CharField(max_length=64) # 数量
# 0 代表下单,但是未付款 1 代表已付款未发货 2 已付款。已发货
o_status = models.IntegerField(default=0) # 状态
o_create = models.DateTimeField(auto_now_add=True) # 创建时间
class Meta:
db_table = 'axf_order'
class OrderGoodsModel(models.Model):
goods = models.ForeignKey(Goods) # 关联的商品
order = models.ForeignKey(OrderModel) # 关联的订单
good_num = models.IntegerField(default=1) # 商品的个数
class Meta:
db_table = 'axf_order_goods'
class UserTicketModel(models.Model):
user = models.ForeignKey(UserModel)
ticket = models.CharField(max_length=30,null=True)
out_time = models.DateTimeField()
class Meta:
db_table = 'axf_user_ticket'
使用navicate或者其他数据库可视化工具 新建数据库axf
然后在Terminal中或者命令行中输入命令
python manage.py makemigrations
python manage.py migrate
然后在数据库中表就创建好了
image.png
image.png
四、完整流程
- 在项目文件axf下的url.py中编写
from django.conf.urls import url,include
from django.contrib import admin
# from django.views.static import serve
# from axftest import settings
from app import views
urlpatterns = [
# url(r'static/(?P<path>.*)$',serve,{"document_root":settings.STATIC_ROOT},),
# url(r'media/(?P<path>.*)$',serve,{"document_root":settings.MEDIA_ROOT},),
url(r'^admin/', admin.site.urls),
url(r'axf/',include('app.urls',namespace='axf')),
url(r'^$',views.Home) # 不加路径默认访问home主页
]
注释的地方是项目部署时需要添加的
- 在app目录下新建urls.py的python文件,编写url路由
from django.conf.urls import url
from app import views
urlpatterns = [
# 首页
url(r'home/',views.Home,name='home'),
# 闪购
url(r'market/',views.Market,name='market'),
# 购物车
url(r'cart/',views.Cart,name='cart'),
# 个人中心
url(r'mine/',views.Mine,name='mine'),
]
因为base_main.html模板中的a标签已经写好了url,所以把四个路由都写了,不然会报错
- 在views.py中编写方法
from django.shortcuts import render
# 首页
def Home(request):
return render(request, 'home/home.html')
# 闪购
def Market(request):
return render(request,'market/market.html')
# 购物车
def Cart(request):
return render(request,'cart/cart.html')
# 个人中心
def Mine(request):
return render(request,'mine/mine.html')
现在就可以访问http://127.0.0.1:8000/axf/home/或http://127.0.0.1:8000/来访问到首页了
接下来就是从数据库获取数据添加到页面
编写Home方法:
def Home(request):
if request.method == 'GET':
date = {'wheels': MainWheel.objects.all(), # 轮播
'navs': MainNav.objects.all(), # 导航
'hotgoods': MainHotGoods.objects.all(), # 热购
'shops': MainShop.objects.all(), # 商店展示
'mainshows': MainShow.objects.all() # 主要商品展示
}
return render(request, 'home/home.html', date)
home.html 页面解析数据
<!--处理轮播banner图-->
{% for wheel in wheels %}
<div class="swiper-slide">
<a href="#">
<img src="{{ wheel.img }}" alt="">
</a>
</div>
{% endfor %}
<!-- 处理结束 -->
<!-- 处理导航数据 -->
{% for nav in navs %}
<li>
<img src="{{ nav.img }}" alt="">
<span></span>
</li>
{% endfor %}
<!--处理结束-->
<!-- 处理必购商品信息 -->
{% for hotgood in hotgoods %}
<li class="swiper-slide">
<img src="{{ hotgood.img }}" alt="">
</li>
{% endfor %}
<!-- 处理结束 -->
{# 商店 #}
<div class="CVS">
<!--处理第一个店铺的数据的图片-->
{% for shop in shops %}
{% ifequal forloop.counter 1 %}
<h2>
<img src="{{ shop.img }}" alt="">
</h2>
{% endifequal %}
{% endfor %}
<!--处理结束-->
{% for shop in shops %}
{% if forloop.counter > 1 and forloop.counter < 4 %}
{% ifequal forloop.counter 2 %}
<fieldset>
{% endifequal %}
<!--处理第二个到第四个数据-->
<a href="#">
<img src="{{ shop.img }}" alt="">
</a>
<!--处理结束-->
{% ifequal forloop.counter 3 %}
</fieldset>
{% endifequal %}
{% endif %}
{% endfor %}
{% for shop in shops %}
{% if forloop.counter > 3 and forloop.counter < 8 %}
{% ifequal forloop.counter 4 %}
<ul>
{% endifequal %}
<!--处理第五个到第八个数据-->
<li>
<a href="#">
<img src="{{ shop.img }}" alt="">
<span></span>
</a>
</li>
<!--处理结束-->
{% ifequal forloop.counter 7 %}
</ul>
{% endifequal %}
{% endif %}
{% endfor %}
{% for shop in shops %}
{% if forloop.counter > 7 and forloop.counter < 12 %}
{% ifequal forloop.counter 8 %}
<ol>
{% endifequal %}
<!--处理第八个到第十二个数据-->
<li>
<a href="#">
<img src="{{ shop.img }}" alt="">
</a>
</li>
<!--处理结束-->
{% ifequal forloop.counter 11 %}
</ol>
{% endifequal %}
{% endif %}
{% endfor %}
</div>
<!--商品展示-->
{% for mainshow in mainshows %}
<section>
<h3>{{ mainshow.name }}<span></span><a href="#">更多></a></h3>
<div>
<a href="#">
<img src="{{ mainshow.img }}" alt="">
</a>
</div>
<ul>
<li>
<a href="#">
<img src="{{ mainshow.img1 }}" alt="">
<p class="description">
</p>
<span>¥{{ mainshow.price1 }}</span>
<s>¥{{ mainshow.marketprice1 }}</s>
</a>
<button>
<span>+</span>
</button>
</li>
<li>
<a href="#">
<img src="{{ mainshow.img2 }}" alt="">
<p class="description">
</p>
<span>¥{{ mainshow.price2 }}</span>
<s>¥{{ mainshow.marketprice2 }}</s>
</a>
<button>
<span>+</span>
</button>
</li>
<li>
<a href="#">
<img src="{{ mainshow.img3 }}" alt="">
<p class="description">
</p>
<span>¥{{ mainshow.price3 }}</span>
<s>¥{{ mainshow.marketprice3 }}</s>
</a>
<button>
<span>+</span>
</button>
</li>
</ul>
</section>
{% endfor %}
<!--商品展示结束-->
image.png
后端传到前端的数据均是以字典形式传递(ajax使用jason格式数据,传对象需要序列化)
前端传数据到后端,get请求在url后面加'?',使用键值对