提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
要求
提示:以下是本篇文章正文内容,下面答案可供参考
一、使用步骤
1.views.py
代码如下(示例):
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from .models import Chair
def home(request):
chairs = Chair.objects.all().values()
print(chairs)
return render(request, 'chair.html', {'chairs':chairs})
# 数据添加
def append(request):
Chair.objects.create(
chair_name = '小天使',
chair_age = '2-10',
chair_price = '1780',
chair_cheap_price = '1280',
img_url = '/static/images/et04.png'
)
return HttpResponse('创建成功')
2.models.py
代码如下:
from django.db import models
# Create your models here.
class Chair(models.Model):
chair_name = models.CharField(max_length=100, verbose_name='名字')
chair_age = models.CharField(max_length=20, verbose_name='年龄')
chair_price = models.IntegerField(verbose_name='价格')
chair_cheap_price = models.IntegerField(verbose_name='打折价格')
img_url = models.CharField(max_length=100, verbose_name='图片地址')
#数据表名字
class Meta:
db_table = 'chair'
verbose_name = '椅子表'
3.chair.html
<div class="p1">
<p>{{chairs.0.chair_name}}</p>
<font>{{chairs.0.chair_age}}
<br>
市场价:<del>{{chairs.0.chair_price}}</del>
<br>
直销价:<span>{{chairs.0.chair_cheap_price}}</span></font>
</div>
<div class="p2">
<p>{{chairs.1.chair_name}}</p>
<font>{{chairs.1.chair_age}}
<br>
市场价:<del>{{chairs.1.chair_price}}</del>
<br>
直销价:<span>{{chairs.1.chair_cheap_price}}</span></font>
</div>
<div class="p3">
<p>{{chairs.2.chair_name}}</p>
<font>{{chairs.2.chair_age}}
<br>
市场价:<del>{{chairs.2.chair_price}}</del>
<br>
直销价:<span>{{chairs.2.chair_cheap_price}}</span></font>
</div>
<div class="p4">
<p>{{chairs.3.chair_name}}</p>
<font>{{chairs.3.chair_age}}
<br>
市场价:<del>{{chairs.3.chair_price}}</del>
<br>
直销价:<span>{{chairs.3.chair_cheap_price}}</span></font>
</div>