Get the App
SLTechnology News&Howtos  ›  Database  › 

[actual combat of python project] BBS Forum (1) build project framework

Shulou Source: shulou.com Published: 2022-06-01 13:57:24 10月03日 Update

1. Preparatory work:

Windows7-64 bit

Python 2.7

Mysql version: 5.7.12

Django 1.9.5

IDE development tools: pycharm 2.7

Second, design the table structure

The main points involved are:

1. The ER diagram of the table. To design the logic, which tables are there and what attributes each table has?

2. The commonly used fields of models model should be proficient, and the ones used this time are:

CharField

ForeignKey

ImageField

TextField

DateTimeField

BooleanField

IntegerField

ManyToManyField

OneToOneField

# # models.py from _ _ future__ import unicode_literalsfrom django.db import modelsfrom django.contrib.auth.models import User# Create your models here.# post information table class Article (models.Model): title = models.CharField (u "article title", max_length=255,unique=True) category = models.ForeignKey ("Category") Verbose_name=u "plates") head_img = models.ImageField (upload_to= "uploads") content = models.TextField (u "content") auther = models.ForeignKey ("UserProfile") publish_date = models.DateTimeField (auto_now=True) hidden = models.BooleanField (default=True) priority = models.IntegerField (u "priority", default=1000) def _ _ unicode__ (self): return "% (self.title) Self.auther) # comment form class Comment (models.Model): article = models.ForeignKey (Article) user = models.ForeignKey ("UserProfile") parent_comment = models.ForeignKey ('self',related_name='p_comment',blank=True,null=True) comment = models.TextField (max_length=1000) date = models.DateTimeField (auto_now=True) def _ unicode__ (self): return "% (self.comment) Self.user) # likes table class ThumbUp (models.Model): article = models.ForeignKey ('Article') user = models.ForeignKey (' UserProfile') date = models.DateTimeField (auto_now=True) def _ unicode__ (self): return "% (self.auther) # Section Classification Table class Category (models.Model): name = models.CharField (max_length=64 Unique=True) admin = models.ManyToManyField ('UserProfile') def _ _ unicode__ (self): return self.name# user Information Table class UserProfile (models.Model): user = models.OneToOneField (User) name = models.CharField (max_length=32) groups = models.ManyToManyField (' UserGroup') def _ unicode__ (self): return self.name# user Group Table class UserGroup (models.Model): name = models.CharField (max_length=64) Unique=True) def _ _ unicode__ (self): return self.name

3. Add database resource information to settings.py

First of all, you need to create a database called s11bbs in mysql, and then edit settings.py

DATABASES = {'default': {' ENGINE': 'django.db.backends.mysql',' NAME': 's11bbsgiving,' HOST':'', 'USER':'root',' PASSWORD':'123',}}

Fourth, synchronize the database

Cd to the directory where the manage.py file is located

Python manage.py migrate

Python manage.py makemigrations

Python manage.py migrate

Register the database table

If you join in admin.py, you can access the background management interface of django, and you can try to add some test data yourself.

Http://127.0.0.1:8000/admin

# # admin.py from django.contrib import adminimport models# Register your models here.admin.site.register (models.Article,ArticleAdmin) admin.site.register (models.Category,CategoryAdmin) admin.site.register (models.Comment) admin.site.register (models.ThumbUp) admin.site.register (models.UserProfile) admin.site.register (models.UserGroup)

Tags: Data database information plates users design project proficiency priority content name background fields properties tools posts commonly used development tools location files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Docker vpn Huawei