• BBS项目


    首先考虑是么数据库,如果不想使用默认数据库,使用MySQL的话,

    #setings
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME':'bbs',
            'HOST':'127.0.0.1',
            'PORT':3306,
            'USER':'root',
            'PASSWORD':'123'
    
        }
    }
    
    #__init__
    import pymysql
    pymysql.install_as_MySQLdb()
    修改settings以及__init__

    创建static文件夹,并修改settings

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    
    #注意,一定要加逗号(  (BASE_DIR, 'static'),   )
    View Code

    如果要使用auth组件,继承AbstractUser,需要在settings添加

    AUTH_USER_MODEL = 'blog.UserInfo'
    
    
    #blog是APP名,UserInfo是继承AbstractUser的表
    View Code

    创建表(执行数据库迁移命令)

    from django.db import models
    from django.contrib.auth.models import AbstractUser
    
    
    # Create your models here.
    # UserInfo这个表,继承AbstractUser,因为要用auth组件
    
    class UserInfo(AbstractUser):
        nid = models.AutoField(primary_key=True)
    
        # username=models.CharField(max_length=32,unique=True)
        # 该字段可以为空,为该字段设置默认值,default='123455666'
        phone = models.CharField(max_length=32,null=True)
        # upload_to需要传一个路径
        avatar = models.FileField(upload_to='avatar/', default='/static/img/default.png')
        #  一对一关联blog表,to_field如果不写,默认主键
        # blog_id字段存的数据是什么?blog表的---nid这个字段
        blog = models.OneToOneField(to='Blog', to_field='nid')
    
        # user表
        #     id name  blog_id
        #     1  111    1
        #     2  111    1
    class Blog(models.Model):
        nid = models.AutoField(primary_key=True)
        title = models.CharField(max_length=64)
        site_name = models.CharField(max_length=32)
        theme = models.CharField(max_length=64)
    
    
    class Category(models.Model):
        nid = models.AutoField(primary_key=True)
        title = models.CharField(max_length=64)
        # ForeignKey跟OneToOneField的区别?
        #OneToOneField unique=True
        blog = models.ForeignKey(to='Blog', to_field='nid', null=True)
    
    
    class Tag(models.Model):
        nid = models.AutoField(primary_key=True)
        title = models.CharField(max_length=64)
        blog = models.ForeignKey(to='Blog', to_field='nid', null=True)
    
    
    class Article(models.Model):
        nid = models.AutoField(primary_key=True)
        title = models.CharField(max_length=64)
        # 摘要,简单描述
        desc = models.CharField(max_length=255)
        # 大文本TextField()
        content = models.TextField()
        # 存时间类型,auto_now_add每插入一条数据,时间自动写入当前时间,
        # auto_now,这条数据修改的时候,会更新成当前时间
        create_time = models.DateTimeField(auto_now_add=True)
    
        blog = models.ForeignKey(to='Blog', to_field='nid', null=True)
    
        category = models.ForeignKey(to='Category', to_field='nid', null=True)
        # through_fields应该怎么写?
        # 中介模型,手动创建第三张表
        tag = models.ManyToManyField(to='Tag', through='ArticleTOTag', through_fields=('article', 'tag'))
        # 这样写,会自动创建第三张表
        # tag = models.ManyToManyField(to='Tag')
    
    
    # 手动创建第三张表
    class ArticleTOTag(models.Model):
        nid = models.AutoField(primary_key=True)
        article = models.ForeignKey(to='Article', to_field='nid')
        tag = models.ForeignKey(to='Tag', to_field='nid')
        # article和tag应不应该联合唯一?
    
        # article_id  1
        # tag_id     1
    
    
    class Commit(models.Model):
        nid = models.AutoField(primary_key=True)
        user = models.ForeignKey(to='UserInfo', to_field='nid')
        article = models.ForeignKey(to='Article', to_field='nid')
        content = models.CharField(max_length=255)
        create_time = models.DateTimeField(auto_now_add=True)
        # 这样写是可以的
        # parent_id=models.IntegerField()
        # 自关联
        # parent_id=models.ForeignKey(to='Commit',to_field='nid')
        parent = models.ForeignKey(to='self', to_field='nid',null=True)
    
    
    class UpAndDown(models.Model):
        nid = models.AutoField(primary_key=True)
        user = models.ForeignKey(to='UserInfo', to_field='nid')
        article = models.ForeignKey(to='Article', to_field='nid')
        is_up = models.BooleanField()
    
        class Meta:
            # 写这些,只是为了不写脏数据,联合唯一
            unique_together = (('user', 'article'),)
    View Code

    登陆界面设计

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
        <script src="/static/jquery-3.3.1.js"></script>
        <title>Title</title>
    </head>
    <body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <form>
                    <div class="form-group">
                        <label for="name">用户名</label>
                        #这里的for等于input的id,那么在点击用户名时,光标自动弹到input框中
            
                        <input type="text" id="name" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="pwd">密码</label>
                        <input type="password" id="pwd" class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="valid_code">验证码</label>
                        <div class="row">
                            <div class="col-md-6">
                                <input type="text" id="valid_code" class="form-control" >
                            </div>
                            <img height="35" width='360' src="get_valid_code" alt="">
    
                        </div>
    
    
                    </div>
                    <input type="button" value="登陆" class="btn btn-primary">
                </form>
            </div>
    
        </div>
    </div>
    
    </body>
    </html>
    login.html
    from django.shortcuts import render,HttpResponse
    from PIL import Image
    # Create your views here.
    import random
    from io import BytesIO
    
    def login(request):
        if request.method == 'GET':
            return render(request,'login.html')
    
    
    #方式一
    #直接在static静态文件夹中引用图片
    # def get_valid_code(request):
    #     with open('static/img/1','rb') as f:
    #         data = f.read()
    #     return HttpResponse(data)
    
    
    
    # 方式二
    #自动生成图片
    # pip3 install Pillow
    
    def get_random_color():# 为了随机获得图片颜色
        return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
    
    
    def get_valid_code(request):
        # new()生成一张图片
        # 三个参数
        # 参数一是图片模式,RGB是三原色,参数二是图片大小,前边是款宽,后边是高
        #     参数三是颜色
        # img=Image.new('RGB',(360,35),color='green')        方式一
        # img=Image.new('RGB',(360,35),color=(0,0,255))      方式二
        # img=Image.new('RGB',(360,35),color=get_random_color())   方式三
    
    # 存取方式一
        # # 保存到本地
        # with open('valid_code.png','wb')as f:
        #     # 直接用img的save方法,参数一是文件,参数二是图片格式
        #     img.save(f,'png')
        # #打开文件,再返回给前端
        # with open('valid_code.png','rb')as f:
        #     data=f.read()
        #     return HttpResponse(data)
        
    # 存取方式二
        # 将图片保存到内存中,存取快,并且解释器可以自动清理资源
        img=Image.new('RGB',(360,35),color=get_random_color())
        f=BytesIO()
        img.save(f,'png')
    
        data=f.getvalue() #用getvalue取值,此时会把f的内存文件中的所有内容读取出来
        return HttpResponse(data)
    随机验证码背景设计,无验证码版本
    from django.shortcuts import render,HttpResponse
    from PIL import Image
    # Create your views here.
    import random
    from io import BytesIO

    def login(request):
    if request.method == 'GET':
    return render(request,'login.html')


    #方式一
    #直接在static静态文件夹中引用图片
    # def get_valid_code(request):
    # with open('static/img/1','rb') as f:
    # data = f.read()
    # return HttpResponse(data)



    # 方式二
    #自动生成图片
    # pip3 install Pillow

    def get_random_color():# 为了随机获得图片颜色
    return (random.randint(0,255),random.randint(0,255),random.randint(0,255))


    def get_valid_code(request):
    # new()生成一张图片
    # 三个参数
    # 参数一是图片模式,RGB是三原色,参数二是图片大小,前边是款宽,后边是高
    # 参数三是颜色
    # img=Image.new('RGB',(360,35),color='green') 方式一
    # img=Image.new('RGB',(360,35),color=(0,0,255)) 方式二
    # img=Image.new('RGB',(360,35),color=get_random_color()) 方式三

    # 存取方式一
    # # 保存到本地
    # with open('valid_code.png','wb')as f:
    # # 直接用img的save方法,参数一是文件,参数二是图片格式
    # img.save(f,'png')
    # #打开文件,再返回给前端
    # with open('valid_code.png','rb')as f:
    # data=f.read()
    # return HttpResponse(data)

    # 存取方式二
    # 将图片保存到内存中,存取快,并且解释器可以自动清理资源
    img=Image.new('RGB',(360,35),color=get_random_color())
    f=BytesIO()
    img.save(f,'png')

    data=f.getvalue() #用getvalue取值,此时会把f的内存文件中的所有内容读取出来
    return HttpResponse(data)
  • 相关阅读:
    (ubuntu ufw)My firewall is blocking network connections from the docker container to outside
    nginx repos
    Xvfb新建虚拟X窗口,通过x11vnc启动VNC Server并转发Xvfb启动的虚拟窗口
    xdotool xdotool模拟击键和鼠标移动--CutyCapt是一个截图工具,xvfb-run
    zabbix debug and vulnerability https://www.zabbix.com/documentation/3.0/manual/concepts/sender
    初探 Nginx 架构
    Nginx缓存
    Nginx代理功能与负载均衡详解
    CentOS 7 部署 nginx-1.14.2
    LDAP第三天 MySQL+LDAP 安装
  • 原文地址:https://www.cnblogs.com/pdun/p/10824656.html
Copyright © 2020-2023  润新知