教材:
python web 企业级项目开发教程 黑马程序员 5.4 实例1:商品管理
实验步骤:
1.创建项目(任意名字)和应用(goods)
2.在项目文件夹(manage.py文件所在路径)新建templates文件夹,在其中新建good.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>商品列表</title>
</head>
<body>
<div>
<table cellpadding="1" cellspacing="0" border="1" style="width:100%;max-width:100%;
margin-bottom:20px">
<caption align="top" style="font-size:26px">商品列表</caption>
<thead>
<tr>
<th>编号</th>
<th>名字</th>
<th>价格</th>
<th>库存</th>
<th>销量</th>
<th>管理</th>
</tr>
</thead>
<tfoot align="right">
</tfoot>
<tbody>
{% for row in goods %}
<tr align="center">
<td>{{ forloop.counter }}</td>
<td>{{ row.name }}</td>
<td>{{ row.price }}</td>
<td>{{ row.stock }}</td>
<td>{{ row.sales }}</td>
<td><a href="/delete{{ row.id }}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
3.修改应用goods的models.py文件
python
from django.db import models
class Goods(models.Model):
create_time=models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
update_time=models.DateTimeField(auto_now=True,verbose_name="更新时间")
name =models.CharField(max_length=50,verbose_name="名字")
price=models.DecimalField(max_digits=10,decimal_places=2,verbose_name="价格")
stock=models.IntegerField(default=0,verbose_name='库存')
sales=models.IntegerField(default=0,verbose_name='销量')
class Meta:
db_table = 'tb_goods'
verbose_name='商品'
verbose_name_plural=verbose_name
def _str_(self):
return '%s: %s' % (self.id,self.name)
4.修改应用goods的views.py文件
python
from django.shortcuts import render,loader
from django import http
from .models import Goods
def get_goods(request):
t = loader.get_template('good.html')
goods = Goods.objects.all()
context = {'goods':goods,}
response = t.render(context,request)
return http.HttpResponse(response)
def del_good(request,gid):
good = Goods.objects.get(id=gid)
good.delete()
return http.HttpResponseRedirect('/')
5.修改主路由
python
from django.contrib import admin
from django.urls import path,include,re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path('^',include('goods.urls')),
]
6.在应用goods下,新建urls.py文件,内容如下
python
from django.urls import path,re_path
from . import views
urlpatterns = [
re_path(r'^$',views.get_goods),
re_path(r'^delete(\d+)$',views.del_good),
]
7.修改settings.py文件
python
#34line
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'goods',
]
#56 line
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
8.生成迁移文件
py manage.py migrations
py manage.py migrate
9.进入python shell,添加数据记录
python
#先执行
py manage.py shell
#引入 from 应用名.models import 类名
from good.models import Goods
Goods.objects.create(name="test1",price="10",stock="50",sales="30")
Goods.objects.create(name="test2",price="20",stock="50",sales="30")
Goods.objects.create(name="test3",price="30",stock="50",sales="30")
10.开启服务
py manage.py runservser
11.测试并完成