pycharm
1.新建一个Django工程
2.部署
在pycharm下方的terminal终端中输入命令:
python manage.py startapp app1
生成“app1”文件夹
3.再部署
mysite下创建一个static的文件夹用来存放js,css,image等。templates用来存放.html文件
4.网页输出简单字符
views+urls
views代码
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): return HttpResponse("hello world")
urls代码
from django.conf.urls import url from django.contrib import admin from app1 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index),
运行pycharm
访问http://127.0.0.1:8000/index/
5.引入html文件
views+urls+templates
在templates里增加一个html文件
起名index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>meng</title> </head> <body> <h1>123</h1> </body> </html>
urls代码
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): return render(request,"index.html",)
运行
6.引入静态文件
.views+urls+templates+static
为了引入js,对settings进行配置
STATIC_URL = '/static/'#自动生成了 STATICFILES_DIRS=( os.path.join(BASE_DIR,'static'), )
index.html
<!doctype html> <html> <head> <meta charset="utf-8"> <title>meng</title> <link rel="stylesheet" type="text/css" href="/static/css/mystyle.css"> </head> <body> <form action="/index/" method="post"> <p> <label>账号:</label> <input type="text" name="username"> </p> <p> <label>密码:</label> <input type="password" name="password"> </p> <p> <input type="submit" value="提交"> <input type="reset" value="重置"> </p> </form> </body> </html>
views.py文件
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. def index(request): if request.method == "POST": username = request.POST.get("username",None) password = request.POST.get("password",None) print(username,password) return render(request,"index.html",)
注释跨站请求保护机制
settings.py
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', ]
登陆index 输入账号密码,在服务器后台便打印出来
7.返回用户信息
views.py改成
from django.shortcuts import render from django.shortcuts import HttpResponse # Create your views here. userlist=[ {"user":"tom","pwd":"123"}, ] def index(request): if request.method == "POST": username = request.POST.get("username",None) password = request.POST.get("password",None) print(username,password) temp={"user":username,"pwd":password} userlist.append(temp) return render(request,"index.html",{"data":userlist})
index.html改成
<!doctype html> <html> <head> <meta charset="utf-8"> <title>meng</title> <link rel="stylesheet" type="text/css" href="/static/css/mystyle.css"> </head> <body> <form action="/index/" method="post"> <p> <label>账号:</label> <input type="text" name="username"> </p> <p> <label>密码:</label> <input type="password" name="password"> </p> <p> <input type="submit" value="提交"> <input type="reset" value="重置"> </p> </form> {% for a in data %} <p>{{ a.user }},{{ a.pwd }}</p> {% endfor %} </table> </body> </html>
8.数据库交互
配置settings
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app1'#新增 ]
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),#用此数据库默认有此代码 } }
编辑models.py
from django.db import models # Create your models here. class UserInfo(models.Model): user = models.CharField(max_length=32) pwd = models.CharField(max_length=32)
创建sqlite3的表
pycharm的teminal中通过命令创建数据库的表
python manage.py makemigrations
python manage.py migrate
....
修改views.py
from django.shortcuts import render from django.shortcuts import HttpResponse from app1 import models # Create your views here. def index(request): if request.method == "POST": username = request.POST.get("username",None) password = request.POST.get("password",None) #添加数据到数据库 models.UserInfo.objects.create(user=username,pwd=password) #从数据库中读取所有数据 userlist=models.UserInfo.objects.all() return render(request,"index.html",{"data":userlist})
view.py
from django.shortcuts import render from django.shortcuts import HttpResponse from app1 import models # Create your views here. def index(request): if request.method == "POST": username = request.POST.get("username",None) password = request.POST.get("password",None) #添加数据到数据库 models.UserInfo.objects.create(user=username,pwd=password) #从数据库中读取所有数据 userlist = models.UserInfo.objects.all() return render(request,"index.html",{"data":userlist})
登陆界面输入账号密码,
9、连mysql数据库
pycharm 里 file-Settings-
在pycharm里下载mysqlclient..
安装
settings.py 设置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #数据库驱动 'NAME': 'mydatabase', #数据库名 'USER': 'mydatabaseuser', #用户名 'PASSWORD': 'mypassword', #密码 'HOST': '127.0.0.1', 'PORT': '3306', } }