table of Contents
BBS pre-equipment
Database Configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bbs',
'USER': 'root',
'PASSWORD': 'root',
'HOST' : '127.0.0.1',
'PORT' : 3306,
'CHARSET' : 'utf8',
'OPTIONS': {
'init_command': 'SET sql_mode="STRICT_TRANS_TABLES"',
'charset': 'utf8mb4'
}
}
}
Table Creation
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
# Create your models here.
class UserInfo(AbstractBaseUser):
phone = models.BigIntegerField(null=True)
avatar = models.FileField(upload_to='avatar/', default='avatar/default.jpg')
create_time = models.DateField(auto_now_add=True)
blog = models.OneToOneField(to='Blog', null=True)
class Blog(models.Model):
site_name = models.CharField(max_length=32)
site_title = models.CharField(max_length=64)
site_theme = models.CharField(max_length=64)
class Category(models.Model):
name = models.CharField(max_length=32)
blog = models.ForeignKey(to='Blog', null=True)
class Tag(models.Model):
name = models.CharField(max_length=32)
blog = models.ForeignKey(to='Blog', null=True)
class Article(models.Model):
title = models.CharField(max_length=64)
desc = models.CharField(max_length=256)
content = models.TextField()
create_time = models.DateField(auto_now_add=True)
# 数据库优化字段
comment_num = models.BigIntegerField(default=0)
up_num = models.BigIntegerField(default=0)
down_num = models.BigIntegerField(default=0)
# 外键字段
blog = models.ForeignKey(to='Blog', null=True)
tags = models.ManyToManyField(to='Tag', through='Article2Tag', through_fields=('article', 'tag'))
category = models.ForeignKey(to='Category', null=True)
class Article2Tag(models.Model):
article = models.ForeignKey(to='Article')
tag = models.ForeignKey(to='Tag')
class UpandDown(models.Model):
user = models.ForeignKey(to='UserInfo')
article = models.ForeignKey(to='Article')
is_up = models.BooleanField()
class Common(models.Model):
user = models.ForeignKey(to='UserInfo')
article = models.ForeignKey(to='Article')
content = models.CharField(max_length=255)
create_time = models.DateTimeField(auto_now_add=True)
parent = models.ForeignKey(to='self', null=True)
Static configuration file
Forms forms
Add bootstrap style
widgets can re-point lead at the time does not come out from django.forms import widgets
Rendering the front page of the way
{% foo in form_obj %}
<p> {{foo.lable}}:{{foo}}</p>
{% endfor%}
Forms forms show an error message
foo.errors is a list of the information itself can not display color .o can get the first element of the list is a plain text colors have a
You can change the error message prompted by changing the forms of data in the form of error_messags
File Viewer
.change triggered when the contents of the box will change .onload projects completed before the implementation of the full implementation of
Since the first three is an asynchronous operation using all other fileReader onload completely executed before executing the
Registration function
Front-end data acquisition
Get File
$ () [0] Gets jQuery object into a JavaScript object, so that it can use JavaScript object properties and methods
.files [0] is the JavaScript file attributes is to get
.each()
Jquery cycle for loop
.serializeArray()
Internal labels will form all the common key to be packaged into a set of custom objects array line
ajax transfer files
ajax transfer files must modify these two parameters
Jump page
Jump page (remember to add the path /)
Adding class attributes
. addClass () method to add one or more elements to the selected class. This method does not remove the existing class attribute, only to add one or more class attributes. Tip: To add more classes, use class names separated by spaces.
has_error: This is a fixed wording, said in the input box error occurs, a red box will be reported, and when the cursor focus input box, will restore the original color
Analyzing logic backend
dictionary
ajax request to have a rear end of the callback function, there is usually provided a dictionary for spread front end
Url which can also add key, and then spread with a distal jsonresponse wrapped directly with the preceding