python的一个小web代码(Django框架下的)
2019-03-08 16:10
239 查看
[code]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来实现)
[code]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类的,后端基础代码,详细注解略去
[code]# 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的连接数据库部分设置
[code]{% 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页面
[code]#!/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.📅
相关文章推荐
- 选择一个 Python Web 框架:Django vs Flask vs Pyramid
- Python实现手写一个类似django的web框架示例
- [转]Django 是一个 Python 下的 web 开发框架[学习资料]
- Django vs Flask vs Pyramid: 如何去选择一个Python Web框架
- [Link]选择一个 Python Web 框架:Django vs Flask vs Pyramid
- Python web框架Django学习(1)——在win7 64bit下配置开发环境Django:一个可以使Web开发工作愉快并且高效的Web开发框架。 使用Django,使你能够以最小的代价构建和
- python web框架 - Django
- Django---强大的Python Web框架
- python常用web框架性能测试(django,flask,bottle,tornado)
- python常用web框架性能测试(django,flask,bottle,tornado)
- python web开发框架 - 之 Django Request
- Python Web 框架, 使用 Django 和 Python 开发 Web 站点
- 简单学习Python的web框架Django(一)
- 全面解读Python Web开发框架Django
- Python Web框架Django (零)
- Python Web----->Django框架学习
- django--python之web开发框架搭建
- python web框架之django环境搭建
- 使用Python的web.py框架实现类似Django的ORM查询的教程
- Python3之Django Web框架模板继承篇