from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('web', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='userinfo',
name='sex',
field=models.BooleanField(default=True),
preserve_default=False,
),
]
from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoField(primary_key=True) #AutoField有序整形 IntegerField整形 title = models.CharField(max_length=32) #CharField字符 author = models.CharField(max_length=32) publishDate = models.DateField() #DateField日期类型 price = models.DecimalField(max_digits=5, decimal_places=2) #DecimalField浮点型也可以用FloatField
YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
)
from django.db import models
class Person(models.Model):
SHIRT_SIZES = (
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
)
name = models.CharField(max_length=60)
shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
>>> p = Person(name="Fred Flintstone", shirt_size="L")
>>> p.save()
>>> p.shirt_size
'L'
>>> p.get_shirt_size_display()
'Large'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog', #你的数据库名称 数据库需要自己提前建好
'USER': 'root', #你的数据库用户名
'PASSWORD': '', #你的数据库密码
'HOST': '', #你的数据库主机,留空默认为localhost
'PORT': '3306', #你的数据库端口
}
}
python manage.py syncdb
python manage.py makemigrations python manage.py migrate
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-25 03:30
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('nid', models.AutoField(primary_key=True, serialize=False)),
('title', models.CharField(max_length=32)),
('author', models.CharField(max_length=32)),
('publishDate', models.DateField()),
('price', models.DecimalField(decimal_places=2, max_digits=5)),
],
),
]
No module named "MySQLdb"
import pymysql pymysql.install_as_MySQLdb()
from app01 import models #app01是应用名
def index(request):
# 从数据库取出所有书籍对象
bookList=models.Book.objects.all() # [bookObj1,.....]
return render(request,"index.html",{"bookList":bookList})
{% for book_obj in bookList %}
<tr>
<td>{{ book_obj.nid }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.author }}</td>
<td>{{ book_obj.publishDate|date:"Y-m-d" }}</td>
<td>{{ book_obj.price }}</td>
</tr>
{% endfor %}
{% for book_obj in bookList %}
<tr>
<td>{{ book_obj.nid }}</td>
<td>{{ book_obj.title }}</td>
<td>{{ book_obj.author }}</td>
<td>{{ book_obj.publishDate|date:"Y-m-d" }}</td>
<td>{{ book_obj.price }}</td>
<td>
<a href="/del/{{ book_obj.nid }}"><button class="btn btn-danger">删除</button></a>
</td>
</tr>
{% endfor %}
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^del/(\d+)', views.delBook), ]
def delBook(request,id):
models.Book.objects.filter(nid=id).delete()
return redirect("/index/")
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有