实现功能:

登录功能
添加功能
删除功能(未实现)

代码:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/commons.css" />
</head>
<body>
<h1>欢迎登录</h1>
{# <img src="/static/1.png">#}
<h3>添加内容</h3>
<form action="/index/" method="POST">
<input type="text" placeholder="主机" name="host" />
<input type="text" placeholder="端口" name="port" />
<input type="submit" value="增加" />
</form> <h3>资产列表</h3>
<table border="">
<thead>
<tr>
<th>主机名</th>
<th>端口</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{ row.hostname }}</td>
<td>{{ row.port }}</td>
<td><a href="/delete/?h={{ row.hostname }}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login/" method="POST">
<p>用户名<input type="text" name="user" /></p>
<p>密码<input type="text" name="pwd" /></p>
<input type="submit" value="提交" /> </form>
</body>
</html>

views.py

from django.shortcuts import render,HttpResponse,redirect
# from django.shortcuts import HttpResponse # Create your views here.
#至少一个参数request
#request封装用户请求相关信息 DB=[
{'hostname':'c1.com','port':},
{'hostname':'c2.com','port':},
{'hostname':'c3.com','port':},
{'hostname':'c4.com','port':},
{'hostname':'c5.com','port':},
{'hostname':'c6.com','port':},
] def index(request):
# print(request.GET)
#return HttpResponse('<h1 style="color:red;">OK</h1>')
if request.method == "GET":
#获取数据pymysql
#模块渲染
"""
、读取html文件内容
NB.将特殊的标记和{'data':DB}进入渲染,得到一个字符串
、将html内容返回给用户
"""
return render(request,'index.html',{'data': DB})
if request.method == 'POST':
host = request.POST.get('host')
port = request.POST.get('port')
#拿到数据加到字典里面去
temp = {'hostname': host,'port':port}
DB.append(temp)
# return render(request, 'index.html',{'data': DB})
return redirect('/index/') def login(request):
#request.method "GET" "POST"
if request.method == 'GET':
#request.GET.get()
return render(request,'login.html')
elif request.method == 'POST':
# 获取用户提交的数据(POST)
username = request.POST.get('user')
password = request.POST.get('pwd')
if username == 'root' and password == '':
# return redirect('http://www.baidu.com')
return redirect('/index/')
else:
return render(request,'login.html')

settings.py

"""
Django settings for cmdb project. Generated by 'django-admin startproject' using Django 1.10.. For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/ For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
""" import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'v5f(=a7$&q^tx55iu1p53jf(gi=oq9y&t07b+w6#f+6tx_ngif' # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'monitor',
] MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ROOT_URLCONF = 'cmdb.urls' TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] WSGI_APPLICATION = 'cmdb.wsgi.application' # Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
} # Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
] # Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)

urls.py

"""cmdb URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
. Add an import: from my_app import views
. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
. Add an import: from other_app.views import Home
. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
. Import the include() function: from django.conf.urls import url, include
. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin from monitor import views urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^login/', views.login),
]

运行效果:

Django练习的更多相关文章

  1. 异步任务队列Celery在Django中的使用

    前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...

  2. 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...

  3. django server之间通过remote user 相互调用

    首先,场景是这样的:存在两个django web应用,并且两个应用存在一定的联系.某些情况下彼此需要获取对方的数据. 但是我们的应用肯经都会有对应的鉴权机制.不会让人家随随便便就访问的对吧.好比上车要 ...

  4. Mysql事务探索及其在Django中的实践(二)

    继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...

  5. Mysql事务探索及其在Django中的实践(一)

    前言 很早就有想开始写博客的想法,一方面是对自己近期所学知识的一些总结.沉淀,方便以后对过去的知识进行梳理.追溯,一方面也希望能通过博客来认识更多相同技术圈的朋友.所幸近期通过了博客园的申请,那么今天 ...

  6. 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...

  7. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

  8. 《Django By Example》第一章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:本人目前在杭州某家互联网公司工作, ...

  9. Django

    一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...

  10. Django admin定制化,User字段扩展[原创]

    前言 参考上篇博文,我们利用了OneToOneField的方式使用了django自带的user,http://www.cnblogs.com/caseast/p/5909248.html , 但这么用 ...

随机推荐

  1. grep 同时排除多个关键字

    不说废话, 例如需要排除 abc.txt 中的  mmm   nnn grep -v 'mmm\|nnn' abc.txt 再举个例子,需要确定mac 的本机ip地址,  显然直接可以输入 ifcon ...

  2. viewpager切换耗时控制

    原文地址https://my.oschina.net/javalover/blog/179003 public class FixedSpeedScroller extends Scroller { ...

  3. 【转载】Picasso下载器

    Github源码地址:https://github.com/JakeWharton/picasso2-okhttp3-downloader 使用方法: Gradle: compile 'com.jak ...

  4. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

  5. Python 正则表达式中级

    首先是?:   在括号中用?:用在findall和split之中,去除括号优先级. 如果不用只输出括号内匹配的值 r   的作用是转义python里面换行符等,像是\n 不用加\来转义 1.子表达式 ...

  6. 正则表达式 \ 和 原生字符串 r

    使用python写字符串常量时,raw string是个很好用的东东,比如在C里我要写一个Windows下的路径,得这么写: char *path = "C:\\mydir\\myfile. ...

  7. VS2015常用配置

    一.调用控制台: 在VS中使用opencv或者QT过程中,完成编程后, 运行发现没有控制台窗口, 比如我们用Qt编写的界面软件, 又想看到我们在代码中添加的打印日志信息,这个时候加上控制台窗口就能实现 ...

  8. Luogu P3962 [TJOI2013]数字根 st

    题面 我先对数字根打了个表,然后得到了一个结论:\(a\)的数字根=\((a-1)mod 9+1\) 我在询问大佬后,大佬给出了一个简单的证明: \(\because 10^n\equiv 1(mod ...

  9. hdu 3435 图回路分割

    将一个无向图分成许多回路,回路点交集为空,点幷集为V.幷最小化回路边权和. #include <cstdio> #include <cstring> #include < ...

  10. 五、python的练习题

    1.输入一行字符,分别统计出其中英文字母.空格.数字和其他字符的个数. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/5 ...