In the last blog has been implemented, link database and create APP, this time we do a web page.
The main contents include: mysql's primary key, foreign key, class Meta's use, basic operation and so on.
A data Import project was created and an APP named blog was registered.
First, in. / blog/models.py, two models are defined, one is computer, the other is room number, the main key of computer is identifier, and the foreign key is Room (number = models.ForeignKey(Room, on_delete=models.CASCADE). This is the code of foreign key.
# encoding: utf-8 from django.db import models from django import forms # Create your models here. class Room(models.Model): number = models.CharField(max_length=2, primary_key=True) def __str__(self): return self.number class Meta: verbose_name_plural = 'Room number' verbose_name = 'Room number' # class class Computer(models.Model): number = models.ForeignKey(Room, on_delete=models.CASCADE) identifier = models.CharField(max_length=5, primary_key=True, default='12345') IPaddr = models.GenericIPAddressField(max_length=15, default='172.0.0.1') purpose = models.CharField(max_length=10) status_choice = ((0, 'Shutdown'), (1, 'normal'), (2, 'fault'), (3, 'remove'), ) status = models.SmallIntegerField(choices=status_choice) tpye_choice = ((0, 'DELL'), (1, 'HP'), (2, 'ASUS'), (3, 'association'), (4, 'Other'),) tpye = models.SmallIntegerField(choices=tpye_choice) harddisk = models.CharField(max_length=5, default='500g') optionSyetem_choice = ((0, 'winXP'), (1, 'win7'), (2, 'win10'), (3, 'Linux'),) optionSyetem = models.SmallIntegerField(choices=optionSyetem_choice) repairTime = models.DateTimeField('Repair time', editable=True) completeTime = models.DateTimeField('Completion time', editable=True) def __str__(self): return self.identifier class Meta: verbose_name_plural = "Computer registration" verbose_name = 'Computer registration' # db_table = 'computer' ordering = ['identifier']
2. Display some information
After adding data to the web page, we can select the data that needs to be displayed in the corresponding properties of the web page.
In. / blog/admin.py, add code.
It contains, change the name of the website, title and other information.
from django.contrib import admin # Register your models here. from .models import Computer,Room admin.site.site_title = "Computer Safety Management System" admin.site.site_header = "Computer Management System" admin.site.index_title = "Computer information" @admin.register(Room) class RoomAdmin(admin.ModelAdmin): pass @admin.register(Computer) class BlogAdmin(admin.ModelAdmin): list_display = ('identifier','IPaddr', 'purpose', 'status', 'harddisk', 'optionSyetem', 'repairTime', 'completeTime',) list_editable = ('status',) list_per_page = 10 # Set fields that can be edited list_display_links = ('IPaddr',) # Sizer list_filter = ('status',) date_hierarchy = 'repairTime'
3. Code execution
python manage.py makemigrations python manage.py migrate #Registered Login Account python manage.py createsuperuser #Running Server python manage.py runserver
IV. Interface Display
Links: https://pan.baidu.com/s/1Wmqn-AhwpiumEX3g54EX1w
Extraction code: p2uw