首页 > 代码库 > Django学习_BBS开发

Django学习_BBS开发

创建数据库

 1 from django.db import models 2 from django.contrib.auth.models import User 3 # Create your models here. 4 class Userprofile(models.Model): 5     user = models.OneToOneField(User,null=True,default=None) 6     name = models.CharField(max_length=32) 7     brief = models.CharField(max_length=128,default="什么也没留下") 8     friends = models.ManyToManyField("Userprofile",blank=True,symmetrical=True) 9     #blank=True form表单提交的时候 字段可以没有值10     def __str__(self):11         return self.name12 class Article(models.Model):13     ‘‘‘文章表‘‘‘14     title = models.CharField(max_length=128,unique=True)15     author = models.ForeignKey("Userprofile")16     category = models.ForeignKey("Category")17     pub_date = models.DateTimeField(auto_now_add=True,auto_created=True)18     tags = models.ManyToManyField("Tag",null=True)19     body = models.TextField(max_length=100000)20     head_img = models.ImageField(upload_to="uploads")21     status_choices = ((0,草稿),(1,"发布"),(2,"隐藏"))22     priority = models.SmallIntegerField(default=1000,verbose_name="优先级")23     #范围数字,默认1000,提供自述名24     def __str__(self):25         return self.title26 class Category(models.Model):27     ‘‘‘板块‘‘‘28     name = models.CharField(max_length=64,unique=True)29     set_as_top_menu = models.BooleanField(default=True) #此字段的默认表单挂件是一个CheckboxInput.30     def __str__(self):31         return self.name32 class Tag(models.Model):33     name = models.CharField(max_length=64,unique=True)34     def __str__(self):35         return self.name36 class Comment(models.Model):37     article = models.ForeignKey("Article")38     p_node = models.ForeignKey("Comment",null=True,blank=True,related_name="my_child_comments")39     user = models.ForeignKey("Userprofile")40     date = models.DateTimeField(auto_now_add=True)41     comment = models.TextField(max_length=1024)42     def __str__(self):43         return self.comment44 class Like(models.Model):45     article = models.ForeignKey("Article")46     user = models.ForeignKey("Userprofile")47     date = models.DateTimeField(auto_now_add=True)48 49 class Privatemail(models.Model):50     ‘‘‘私信‘‘‘51     pass

命令:

python manage.py makemigrations

python manage.py migrate

python manage.py runserver 127.0.0.1:8000   ###启动django

创建后台超级用户

python manage.py createsuperuser

Pillow安装

pip install -i https://pypi.douban.com/simple Pillow

利用bootstrap模板

下载模板放到statics目录下面

 

Django学习_BBS开发