The path to learning in Python -2018/7/10 Blog Development Project Flow
? In general, a project development process is divided into: Project requirements, design table structure, functional development, testing functions, products on-line, the study of the blog Park as the blueprint for the development.
1. Project Requirements
The development of the blog needs mainly the following points:
- Implementation of login verification based on Auth module and Ajax
- Registering functions based on forms components and Ajax
- Design Blog Home
- Design a personal Site page
- Design Article details page
- To implement the article likes the function.
- Implement the comment function of the article, including comments on the article and comments on the article
- Implementing Rich Text Edit boxes
- Prevent XSS attacks (for example, when the user's article contains JS code, other users to access the browser will execute the JS code, greatly reducing the user's security)
2. Design Table structure
According to the functional analysis, there are user Information table (users), blog information Form (blog), blog post classification information table (sort), blog post tag information table (tag), blog article table (article), Article likes table (like), article comment form (Comment), The relationship between them is as follows:
models.py
From django.db import modelsfrom django.contrib.auth.models import abstractuserclass User (Abstractuser): "" "users information Sheet "" "UID = models. Autofield (primary_key=true) phone = models. Charfield (max_length=11, Unique=true, null=true) avatar = models. Filefield (upload_to= "avatars/", default= "/avatars/default.jpg") Create_time = models. Datefield (verbose_name= "Created Date", auto_now_add=true) blog = models. Onetoonefield (to= "Blog", to_field= "bid", Null=true, On_delete=models. CASCADE) # Build a one-to-one relationship with the blog Def __str__ (self): return Self.usernameclass blog (models. Model): "" "Blog Information table" "Bid = models. Autofield (primary_key=true) title = models. Charfield (max_length=32, verbose_name= "personal blog title") Theme = models. Charfield (max_length=32, verbose_name= "blog theme") site = models. Charfield (max_length=32, verbose_name= "personal site name") def __str__ (self): return Self.siteclass Sort (models. Model): "" "blog post classification Information table" "" SID = Models. Autofield (primary_key=true) title = ModelS.charfield (max_length=32, verbose_name= "category title") Blog = models. ForeignKey (to= "Blog", to_field= "bid", on_delete=models. CASCADE) # Build a one-to-many relationship with the blog Def __str__ (self): return Self.titleclass Tag (models. Model): "" "blog post tag Information table" "" TID = models. Autofield (primary_key=true) name = models. Charfield (max_length=32, verbose_name= "tag name") Blog = models. ForeignKey (to= "Blog", to_field= "bid", on_delete=models. CASCADE) # Build a one-to-many relationship with the blog Def __str__ (self): return Self.nameclass article (models. Model): "" "blog post Table" "" AID = models. Autofield (primary_key=true) title = models. Charfield (max_length=32, verbose_name= "article title") Abstract = models. Charfield (max_length=32, verbose_name= "article summary") Create_time = models. Datefield (verbose_name= "Creation date", auto_now_add=true) content = models. TextField () user = models. ForeignKey (to= "User", to_field= "UID", verbose_name= "author", On_delete=models. CASCADE) sort = models. ForeignKey (to= "Sort", to_field= "Sid", Null=true, On_delete=modEls. CASCADE) # Establish many-to-many relationships with tags tag = models. Manytomanyfield (to= "tag", through= "Articletotag", through_fields= ("article", "Tag")) def __str__ (self): return s Elf.titleclass Articletotag (models. Model): AID = models. Autofield (primary_key=true) Article = models. ForeignKey (to= "article", to_field= "title", verbose_name= "article title", On_delete=models. CASCADE) tag = models. ForeignKey (to= "tag", to_field= "name", verbose_name= "article tag", On_delete=models. CASCADE) def __str__ (self): name = Self.article.title + "---" + self.tag.name return nameclass like (model S.model): "" Article Likes Table "" "Lid = models. Autofield (primary_key=true) user = models. ForeignKey (to= "User", to_field= "UID", Null=true, On_delete=models. CASCADE) Article = models. ForeignKey (to= "article", to_field= "aid", Null=true, On_delete=models. CASCADE) Is_like = models. Booleanfield (Default=true) class Comment (models. Model): "" "Article Comment Form" "" CID = models. Autofield (primary_key=true) user = MoDEls. ForeignKey (to= "User", to_field= "UID", Null=true, On_delete=models. CASCADE) Article = models. ForeignKey (to= "article", to_field= "aid", Null=true, On_delete=models. CASCADE) Create_time = models. Datefield (verbose_name= "Creation date", auto_now_add=true) content = models. Charfield (max_length=255, verbose_name= "comment content") Parent_comment = models. ForeignKey (to= "Comment", to_field= "CID", Null=true, On_delete=models. CASCADE) # root Comment def __str__ (self): return self.content
Because MySQL is needed, you need to configure database information in setteings.py
In the first user table, the configuration information is also required because the Abstractuser is inherited
settings.py
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘blog‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘admin‘, ‘HOST‘: ‘localhost‘, ‘PORT‘: 3306 }}AUTH_USER_MODEL = "app01.User"
Note that when using MySQL, you need to add the following code to the init.py of your project:
import pymysqlpymysql.install_as_MySQLdb()
3. Feature Development 3.1 Login verification
login.html
<! DOCTYPE html>
views.py From django.shortcuts import Render, Httpresponseimport randomfrom PIL import Image, Imagedraw, Imagefont, Imagefilterfro M IO Import bytesiodef Login (Request): Return render (Request, "login.html") def random_color (): color = (Random.randi NT (+, 255), Random.randint (+, 255), Random.randint (+, 255)) return Colordef Random_color2 (): color = (random.rand Int (+, 127), Random.randint (+, 127), Random.randint (+, 127)) return Colordef Random_char (): "" "Random Number/letter" "" Random_num = str (random.randint (0, 9)) Random_low = Chr (random.randint (122)) # A~z random_upper = chr (random . Randint (+)) # A~z random_chars = Random.choice ([Random_num, Random_low, Random_upper]) return random_charsdef Verify_code (Request): "" "Captcha" "" Image = Image.new ("RGB", (183, +), (255, 255, 255)) Image_font = IMAGEF Ont.truetype ("Static/blog/font/arial.ttf", +) Draw = Imagedraw.draw (image) # Fill each coordinate with a color for x in range (183): For Y in range (40): Draw.point ((x, y), Fill=random_color ()) for I in range (5): Draw.text ((20+i*30, 0), Random_char (), font= Image_font, Fill=random_color2 ()) image = Image.filter (imagefilter.blur) # obfuscation F = Bytesio () image.save (F, "png ") data = F.getvalue () return HttpResponse (data)
The login interface effect looks like this:
The path to learning in Python -2018/7/10