Django 搭建CMDB系统完整[1](用户登录)
***使用的mysql数据库,所以系统中要先安装mysql数据库 1、安装环境: pip install django python -m pip install --upgrade pip setuptools python -m pip install django pip install --upgrade pyls https://pypi.python.org/simple pip install MySQL-python pip install mysqlclient mkdir /dj cd /dj django-admin startproject cmdb cd cmdb python manage.py migrate cd cmdb vi settings.py 做如下更改 ALLOWED_HOSTS = ['*'] 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', ], }, }, ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'cmdb', 'USER': 'root', 'PASSWORD': 'xxxxxxx', 'HOST': '127.0.0.1', 'PORT': '3306', } } vi urls.py 做如下更改 from django.conf.urls import url from django.contrib import admin from cmdb import views from django.contrib.auth.views import * urlpatterns = [ url(r'^', login), ] vi views.py 做如下更改 from django.shortcuts import render_to_response def main_page(request): return render_to_response('main_page.html', {'user':request.user }) cd /dj/cmdb mkdir -p templates/registration cd templates vi main_page.html 做如下更改 <html> <head> <title>Django Bookmarks</title> </head> <body> <h1>Welcome to Django Bookmarks</h1> {% if user.username %} <p>Welcome {{ user.username }}! Here you can store and share bookmarks!</p> {% else %} <p>Welcome anonymous user! You need to <a href="/login/">login</a> before you can store and share bookmarks.</p> {% endif %} </body> </html> cd registration vi login.html 做如下更改 <html> <head> <title>Django Bookmarks - User Login</title> </head> <body> <h1>User Login</h1> {% if form.has_errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} <form method="post" action="."> <p><label for="id_username">Username:</label> {{ form.username }}</p> <p><label for="id_password">Password:</label> {{ form.password }}</p> <input type="hidden" name="next" value="/" /> {% csrf_token %} <input type="submit" value="login" /> </form> </body> </html>