After the first day and the second day of study, you can use the database to transmit data.
Now use sqlite3 to transmit data through the Python module.
The data on the first day has already established the database.
You can view it through Navicat premium, as shown
in the figure. None of the above have any data and tables
Add the following code to models.py in the ch01 directory
from django.db import models
from datetime import datetime,timedelta
from django.db import models
from datetime import datetime,timedelta
Create your models here.
class Question(models.Model):
"""Class variables are used to define the characteristics of member variables, and the framework automatically creates corresponding member variables"""
question_text = models.CharField('question', max_length=200)
pub_date = models.DateTimeField ('release date')
def was_published_recently(self):
print(type(self))
print(type(self.pub_date))
now = datetime.now()
past = now - timedelta(hours=72)
return past <= self.pub_date <= now
was_published_recently.short_description = '最新发布?'
was_published_recently.boolean = True
def pub_date_str(self):
return self.pub_date.strftime("%Y-%m-%d %H:%M:%S")
pub_date_str.short_description = '发布日期'
pub_date_str.admin_order_field = 'pub_date'
class Meta:
verbose_name = "问题表"
verbose_name_plural = verbose_name
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(‘选项’, max_length=200)
votes = models.IntegerField(‘票数’, default=0)
def __str__(self):
return self.choice_text
#---------settings.py operation---------------- Add 'ch01.apps.Ch01Config'
to INSTALLED_APPS ,
#--------Operate in root directory urls -------------
Add path('admin/', admin.site.urls) to patterns in url
, if there is If it is wrong, delete this sentence
Operate in -----admin---------------------
from django.contrib import admin
from .models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
#----Change the time zone and Chinese in settings --------
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = ‘Asia/Shanghai’
USE_I18N = True
USE_L10N = True
USE_TZ = False
#--------------Create a new directory under CH01 -----
templates#View
static#
Create a new static configuration file test.html in the static directory
The static directory can be configured with multiple
#This is the search path for static directory files
STATICFILES_DIRS = (
os.path.join(BASE_DIR, “static”),
os.path.join(BASE_DIR, “ch01/static”),
)
Displayed in the browser after starting the service
http://127.0.0.1:8000/static/test.html#Access the root static file
http://127.0.0.1:8000/static/ch01/test.html#Access the static file of the subdirectory