当前位置:   article > 正文

Python作业提交系统源码,基于Django+mysql,作业提交系统源代码

作业提交系统源码

需要在mysql数据库中: 新建库:yunzhen_data,并 配置数据库用户名及密码
打开项目后进行数据库的迁移 相关指令:
python manage.py migrate
启动程序python manage.py runserver
在这里插入图片描述
完整程序代码下载地址:Python作业提交系统源码
核心代码

import random
import django.utils.timezone as timezone
from django.core.paginator import Paginator
from django.db.models import Q
from django.http import StreamingHttpResponse, FileResponse
from django.shortcuts import render, HttpResponse, redirect
from django.utils.encoding import escape_uri_path
import requests
from .models import *
from datetime import datetime
# 登录界面


def sign_in(request):  # 登录界面
    td_h = datetime.today().hour  # 获取 当前时间的小时
    title = "欢迎来到作业提交系统"
    if request.method == "GET":
        # return render(request, "login.html", {"title": title})
        return render(request, "login.html", {"title": title, "td_h": td_h})

    number = request.POST.get("user_phone_number")
    get_password = request.POST.get("user_password")
    check = User.objects.filter(user_phone_number=f"{number}").exists()
    if check:
        obj = User.objects.filter(user_phone_number=f"{number}").first()
        password = obj.user_password
        if password == get_password:
            request.session["info"] = {"id": obj.id, "user_phone_number": number, "user_name": obj.user_name}
            return redirect('/inbox/')
        else:
            title = "密码错误,请重新尝试"
            return render(request, "login.html", {"title": title, "td_h": td_h})
    else:
        title = "账号不存在,请检查账号"
        return render(request, "login.html", {"title": title, "td_h": td_h})


# 邮件详情页面
def email(request):
    try:
        user_information = request.session.get('info')
        user_id = user_information['id']
        user_phone_number = user_information['user_phone_number']
        user_name = user_information['user_name']
        email_id = request.GET.get('email_id')
        email_information = Email.objects.get(id=email_id)

        if user_name == email_information.get_user_name:
            Email.objects.filter(id=email_id).update(read=True)
            in_out = 'in'
        else:
            in_out = 'out'

        next = Email.objects.filter(id__lt=email_id).exists()
        if next:
            next = Email.objects.filter(id__lt=email_id).order_by('-id').first().id
        else:
            next = email_id
        previous = Email.objects.filter(id__gt=email_id).exists()
        if previous:
            previous = Email.objects.filter(id__gt=email_id).first().id
        else:
            previous = email_id
        content = {
            'id': email_information.id,
            'get_user_name': email_information.get_user_name,
            'post_user_name': email_information.post_user_name,
            'topic': email_information.topic,
            'content': email_information.content,
            'file': '../static/upload/' + str(email_information.file),
            'date': email_information.date,
            'user_phone_number': user_phone_number,
            'filename': str(email_information.file)[5:],
            'previous': previous,
            'next': next,
            'in_out': in_out,
        }
        return render(request, 'email.html', content)
    except:
        return redirect('/')


# 提交作业页面
def postwork(request):
    try:
        if request.method == 'GET':
            user_information = request.session.get('info')
            user_phone_number = user_information['user_phone_number']
            user_name = user_information['user_name']
            content = {
                'user_phone_number': user_phone_number,
                'user_name': user_name,
            }
            return render(request, 'postwork.html', content)
        else:
            user_information = request.session.get('info')
            user_name = user_information['user_name']
            user_phone_number = user_information['user_phone_number']
            get_user_name = request.POST.get('get_user_name')
            topic = request.POST.get('topic')
            content = request.POST.get('content')
            filename = request.FILES.get('filename')
            user_name_exist = User.objects.filter(user_name=get_user_name).exists()
            if user_name == '':
                content = {
                    'content': content,
                    'topic': topic,
                    'get_user_name': get_user_name,
                    'user_name': user_name,
                    'user_phone_number': user_phone_number,
                    'error': ' 请输入收件人'
                }
                return render(request, 'postwork.html', content)
            elif not user_name_exist:
                content = {
                    'content': content,
                    'topic': topic,
                    'get_user_name': get_user_name,
                    'user_name': user_name,
                    'user_phone_number': user_phone_number,
                    'error': ' 该收件人不存在,请核实收件人信息'
                }
                return render(request, 'postwork.html', content)
            elif user_name == get_user_name:
                content = {
                    'content': content,
                    'topic': topic,
                    'get_user_name': get_user_name,
                    'user_name': user_name,
                    'user_phone_number': user_phone_number,
                    'error': ' 你不能给自己发邮件哦'
                }
                return render(request, 'postwork.html', content)
            else:
                email_information = Email()
                email_information.get_user_name = get_user_name
                email_information.post_user_name = user_name
                email_information.topic = topic
                email_information.content = content
                email_information.file = filename
                email_information.save()
                return redirect('/inbox/')
    except:
        return redirect('/')


# 注册页面
def register(request):
    if request.method == 'GET':
        content = {
            'error': '每一次作业都是在提升自己!'
        }
        return render(request, 'register.html', content)
    else:
        if request.POST.get('submit') == '发送验证码':
            code = ''
            try:
                user_name = request.POST.get('user_name')
                user_password = request.POST.get('user_password')
                test_user_password = request.POST.get('test_user_password')
                user_phone_number = request.POST.get('user_phone_number')
                user_name_exist = User.objects.filter(user_name=user_name).exists()
                user_phone_number_exist = User.objects.filter(user_phone_number=user_phone_number).exists()
                if len(user_name) > 10:
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '请填写小于十个字符的用户名',
                    }
                    return render(request, 'register.html', content)
                elif user_name == '' or user_password == '' or test_user_password == '' or user_phone_number == '':
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '请严格按照规定将信息填写完整'
                    }
                    return render(request, 'register.html', content)
                elif user_name_exist:
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '该用户名已存在',
                    }
                    return render(request, 'register.html', content)
                elif len(user_password) > 20:
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '请填写小于二十个字符的密码'
                    }
                    return render(request, 'register.html', content)
                elif test_user_password != user_password:
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '两次密码填写不一致'
                    }
                    return render(request, 'register.html', content)
                elif len(user_phone_number) != 11:
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '请输入正确的手机号'
                    }
                    return render(request, 'register.html', content)
                elif user_phone_number_exist:
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '该手机号已被注册'
                    }
                    return render(request, 'register.html', content)
                else:
                    for num in range(4):
                        number = str(random.randint(0, 9))
                        code = code + number

                    header = {
                        "Authorization": "APPCODE 843677f6edc44aff9a51962d288e6e96",
                    }
                    body = {
                        "content": f"code:{code}",
                        "phone_number": f"{user_phone_number}",
                        "template_id": "TPL_0000"
                    }
                    url = 'https://dfsns.market.alicloudapi.com/data/send_sms'
                    response = requests.post(url=url, data=body, headers=header)
                    request.session['verification_code'] = code
                    content = {
                        'user_name': user_name,
                        'user_password': user_password,
                        'test_user_password': test_user_password,
                        'user_phone_number': user_phone_number,
                        'error': '验证码已成功发送'
                    }
                    return render(request, 'register.html', content)
            except:
                content = {
                    'user_name': user_name,
                    'user_password': user_password,
                    'test_user_password': test_user_password,
                    'user_phone_number': user_phone_number,
                    'error': '请严格按照规定将信息填写完整'
                }
                return render(request, 'register.html', content)
        else:
            user_name = request.POST.get('user_name')
            user_password = request.POST.get('user_password')
            test_user_password = request.POST.get('test_user_password')
            user_phone_number = request.POST.get('user_phone_number')
            verification_code = request.POST.get('verification_code')
            code = request.session.get('verification_code')
            if user_name == '' or user_password == '' or test_user_password == '' or user_phone_number == '' or verification_code == '':
                content = {
                    'user_name': user_name,
                    'user_password': user_password,
                    'test_user_password': test_user_password,
                    'user_phone_number': user_phone_number,
                    'error': '请严格按照规定将信息填写完整'
                }
                return render(request, 'register.html', content)

            if code != verification_code:
                user_name = request.POST.get('user_name')
                user_password = request.POST.get('user_password')
                test_user_password = request.POST.get('test_user_password')
                user_phone_number = request.POST.get('user_phone_number')
                content = {
                    'user_name': user_name,
                    'user_password': user_password,
                    'test_user_password': test_user_password,
                    'user_phone_number': user_phone_number,
                    'verification_code': verification_code,
                    'error': '每一次作业都是在提升自己!'
                }
                return render(request, 'register.html', content)
            else:
                user = User()
                user.user_name = request.POST.get('user_name')
                user.user_password = request.POST.get('user_password')
                user.user_phone_number = request.POST.get('user_phone_number')
                user.save()
                request.session.flush()
                return redirect('/')


# 收件箱
def inbox(request):
    check = request.session.get("info")
    if check:
        print(check)
        if request.GET.get('search', '') == '':
            # 获取用户名
            name = request.session.get("info")["user_name"]
            data_list = Email.objects.filter(get_user_name=name, get_user_delete=False).order_by('-date')

            # 分页功能
            page = request.GET.get('page', '1')
            paginator = Paginator(data_list, 14)
            data_list = paginator.get_page(page)

            # 修正 css 数据
            number = 50
            number_font = 63
            number_delete = 58
            number_second = 69
            for data_dic in data_list:
                data_dic.number_font = str(number_font)
                data_dic.number = str(number)
                data_dic.number_delete = str(number_delete)
                data_dic.number_second = str(number_second)
                number = number + 50
                number_font = number_font + 50
                number_delete = number_delete + 50
                number_second = number_second + 50

            return render(request, "in_box.html", {"data_list": data_list})
        else:
            search = request.GET.get('search').strip()
            condition = None
            for q in search.split(' '):
                if condition is None:
                    condition = Q(topic__icontains=q)
                else:
                    condition = condition | Q(topic__icontains=q)
            data_list = []
            if condition is not None:
                name = request.session.get("info")["user_name"]
                data_list = Email.objects.filter(condition, get_user_delete=False, get_user_name=name)

            # 分页功能
            page = request.GET.get('page', '1')
            paginator = Paginator(data_list, 14)
            data_list = paginator.get_page(page)

            # 修正 css 数据
            number = 50
            number_font = 63
            number_delete = 58
            number_second = 69
            for data_dic in data_list:
                data_dic.number_font = str(number_font)
                data_dic.number = str(number)
                data_dic.number_delete = str(number_delete)
                data_dic.number_second = str(number_second)
                number = number + 50
                number_font = number_font + 50
                number_delete = number_delete + 50
                number_second = number_second + 50

            content = {
                'data_list': data_list,
                'search': search,
            }
            return render(request, 'in_box.html', content)
    else:
        return redirect('/')


# 发件箱
def outbox(request):
    check = request.session.get("info")
    if check:
        print(check)
        if request.GET.get('search', '') == '':
            master = request.session.get("info")["user_name"]
            data_list = Email.objects.filter(post_user_name=master, post_user_delete=False).order_by('-date')

            # 分页功能
            page = request.GET.get('page', '1')
            paginator = Paginator(data_list, 14)
            data_list = paginator.get_page(page)

            # 修正 css 数据
            number = 50
            number_font = 63
            number_delete = 58
            number_second = 69
            for data_dic in data_list:
                data_dic.number_font = str(number_font)
                data_dic.number = str(number)
                data_dic.number_delete = str(number_delete)
                data_dic.number_second = str(number_second)
                number = number + 50
                number_font = number_font + 50
                number_delete = number_delete + 50
                number_second = number_second + 50

            return render(request, "out_box.html", {"data_list": data_list})
        else:
            search = request.GET.get('search').strip()
            condition = None
            for q in search.split(' '):
                if condition is None:
                    condition = Q(topic__icontains=q)
                else:
                    condition = condition | Q(topic__icontains=q)
            data_list = []
            if condition is not None:
                master = request.session.get("info")["user_name"]
                data_list = Email.objects.filter(condition, post_user_name=master, post_user_delete=False)

            # 分页功能
            page = request.GET.get('page', '1')
            paginator = Paginator(data_list, 14)
            data_list = paginator.get_page(page)

            # 修正 css 数据
            number = 50
            number_font = 63
            number_delete = 58
            number_second = 69
            for data_dic in data_list:
                data_dic.number_font = str(number_font)
                data_dic.number = str(number)
                data_dic.number_delete = str(number_delete)
                data_dic.number_second = str(number_second)
                number = number + 50
                number_font = number_font + 50
                number_delete = number_delete + 50
                number_second = number_second + 50

            content = {
                'data_list': data_list,
                'search': search,
            }
            return render(request, 'out_box.html', content)
    else:
        return redirect('/')


def log_out(request):  # 退出登录
    request.session.flush()
    return redirect('/')


def retrievepassword(request):
    title = "云震"
    number = request.POST.get("user_phone_number")
    if request.POST.get("button") == "发送验证码":
        check_number = User.objects.filter(user_phone_number=number).exists()
        if check_number:
            coder = str(random.randint(999, 9999))
            header = {
                "Authorization": "APPCODE 843677f6edc44aff9a51962d288e6e96",
            }
            body = {
                "content": f"code:{coder}",
                "phone_number": f"{number}",
                "template_id": "TPL_0000"
            }
            url = 'https://dfsns.market.alicloudapi.com/data/send_sms'
            response = requests.post(url=url, data=body, headers=header)
            get_password = request.POST.get("new_password", '')
            re_get_password = request.POST.get("test_new_password", '')
            context = {
                "number": number,
                "title": '已发送验证码',
                "get_password": get_password,
                "re_get_password": re_get_password
                       }
            request.session["coder"] = coder

            return render(request, 'retrievepassword.html', context=context)
        else:
            title = "未找到此手机号的用户"
            return render(request, 'retrievepassword.html', {"title": title, "number": number})
    elif request.POST.get("button") == "提交":
        get_code = request.POST.get("verification_code", "")
        get_password = request.POST.get("new_password")
        re_get_password = request.POST.get("test_new_password")
        content = {
            "title": title,
            "number": number,
            "get_code": get_code,
            "get_password": get_password,
            "re_get_password": re_get_password
        }
        if get_password == '' or re_get_password == '' or get_code == '' or number == '':
            content["title"] = "所有数据均不能为空"
            return render(request, 'retrievepassword.html', content)

        if get_code == request.session.get("coder") and get_password != re_get_password:
            content["title"] = "两次密码并不一致!"
            return render(request, 'retrievepassword.html', content)
        elif get_code != request.session.get("coder") and get_password == re_get_password:
            content["title"] = "验证码错误请重输!"
            return render(request, 'retrievepassword.html', content)
        elif get_code != request.session.get("coder") and get_password != re_get_password:
            content["title"] = "验证码和再次确认都有误!"
            return render(request, 'retrievepassword.html', content)
        else:
            data = User.objects.filter(user_phone_number=number).update(user_password=get_password)
            content["title"] = "您的密码已经重置成功!"
            request.session.flush()
            return render(request, 'retrievepassword.html', content)
    return render(request, 'retrievepassword.html', {"title": title})


# 删除邮箱操作
def delete(request):
    email_id = request.GET.get('email_id')
    in_out = request.GET.get('in_out')
    user_information = request.session['info']
    if in_out == 'in':
        Email.objects.filter(id=email_id).update(get_user_delete=True)
        return redirect('/inbox/')
    else:
        Email.objects.filter(id=email_id).update(post_user_delete=True)
        return redirect('/outbox/')


# 全部已读操作
def all_read(request):
    try:
        user_information = request.session.get('info')
        user_name = user_information.user_name
        Email.objects.filter(get_user_name=user_name).update(read=True)
        return redirect('/inbox/')
    except:
        return redirect('/')


# 自定义404页面
def page_not_found(request, exception):
    return render(request, '404.html')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541

在这里插入图片描述
完整程序代码下载地址:Python作业提交系统源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/154018?site
推荐阅读
相关标签
  

闽ICP备14008679号