python的一个小web代码(Django框架下的)

def login(re):
    '''
    登录验证
    '''
    nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if req.method == 'GET':
        uf = UserForm()
        #User.objects.create_user(username='ADMIN', password='ABC')
        return render(re, 'login.html', {'uf': uf, 'nowtime': nowtime})
    else:
        uf = UserForm(re.POST)
        if uf.is_valid():
            username = re.POST.get('username', '')
            password = re.POST.get('password', '')
            user = auth.authenticate(username=username, password=password)
            if user is not None:      #and user.is_active:
                auth.login(re, user)
                return render(re, 'index.html')
            else:
                return HttpResponse('is failed')   #render(re, 'login.html', {'uf': uf, 'nowtime': nowtime, 'password_is_wrong': True})
        else:
            return HttpResponse('NOT GET')  #render(re, 'login.html', {'uf': uf, 'nowtime': nowtime})

建立和mysql的数据库连接(通过调用python里面自带的auth里面的authenticate来实现)

from django.db import models
from datetime import *
from django.contrib.auth.models import User
class webserver(models.Model):
    user = models.OneToOneField(User, unique=True,verbose_name=('user'))
    phone = models.CharField(u'phone',max_length=20)
    user_role = models.CharField(u'role',max_length=40)

在类里面是必须要设置一个user类的,后端基础代码,详细注解略去

 

# Django settings for DjangoWeb project.
import os
from django.db import models

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = True
# TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database1',
        'USER': 'root',
        'PASSWORD': 'ABCabc123',
        'HOST': '127.0.0.1',
        'PORT': 3306,
    }
}
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.4/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ['*']

这部分是settings.py的连接数据库部分设置

{% load staticfiles %}
<link href="{% static "css/adstyle.css"%}" rel="stylesheet" type="text/css" />
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Linux-Management</title>
</head>
{% if password.is_wrong %}
    <script type="text/javascript" src="{%static "js/jquery-1.11.0.min.js" %}"></script>
    <script type="text/javascript" src="{%static "js/alert.js" %}"></script>
    <link href="{%static "css/alert.css" %}" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        Alert.showMsg("用户名或密码错误!");
        location.href="/webserver/login/"
    </script>
{% endif %}
<body>
    <div class="admin-ht">
        <div class="adminBord">
            <h1>登陆</h1>
        <h4>{{ nowtime }}</h4>
            <form method="post" enctype="multipart/form-data" >
                {% csrf_token %}
                {{uf.as_p}}
                <input type="submit" value="login" id="loging">
            </form>
        </div>
    </div>
</body>
</html>

这是前端代码,login页面

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoWeb.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

这是manage.py代码

以上基本是这个小工程的框架结构,还有详细的例如前端技术bootstrap之类的东西现在没那么多时间去写,还有其他重要的事情需要做,这个项目的部分就写到这里。2019.3.?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值