Django连接数据库
坏境的需要:python、pip、django、mysql和其他的一些必要类库
windows系统下载mysql官网链接:https://dev.mysql.com/downloads/installer/
下载安装好配置用户名和密码
然后再django工程的setting.py设置database
我创建的是Admin应用
下面是的配置对应mysql的账号密码和端口号(我把django默认的sqlite注释,用mysql)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME':'video_demo',//对应mysql
'USER':'root',
'PASSWORD':'MaMYONG198',
'HOST':'127.0.0.1',
'PORT':'3306',
}
}
在init.py添加(提前下载django的pymysql类库)
import pymysql
pymysql.install_as_MySQLdb()
运行工程就可以可连接数据库了
设置model
from django.db import models
class user(models.Model):
u_name = models.CharField(max_length=10) # 保存用户名
u_password = models.CharField(max_length=255) # 保存密码
class Meta:
db_table = 'users'
views
from django.shortcuts import render, render_to_response
from django import forms
from django.http import HttpResponseRedirect, HttpResponse
from .models import user
from django.contrib.auth.hashers import make_password, check_password
from django.views.decorators.csrf import csrf_exempt
class UserForm(forms.Form):
username = forms.CharField(label='用户', max_length=10)
password = forms.CharField(label='密码', widget=forms.PasswordInput())
# 处理登录的方法
@csrf_exempt
def login01(request):
if request.method == 'GET':
return render(request, 'registration/login.html')
if request.method == 'POST':
data = UserForm(request.POST)
if data.is_valid():
# 获取到表单提交的值
username = data.cleaned_data['username']
password = data.cleaned_data['password']
# 把表单中取到的值和数据库里做对比
try:
use = user.objects.get(u_name=username)
if password == use.u_password:
return HttpResponseRedirect('/login/index01/')
else:
return HttpResponse("密码错误")
except:
return HttpResponse("密码错误")
else:
return HttpResponse("用户不存在")
def logout01(request): # 退出登录的方法
if request.method == 'GET':
response = HttpResponseRedirect('/login/login01/')
return response
def index01(request):
if request.method == 'GET':
return render(request, 'registration/firstpage.html')
def loginpage(request):
return render(request,'registration/firstpage.html')
urls
我的这个有点乱,主要理解函数
from django.urls import path
from Admin import views
from django.contrib import admin
app_name='polls'
urlpatterns=[
path('' ,views.index01, name='index'),
path('login/', views.login01, name='login'),
path('page/',views.loginpage,name='loginpage'),
path('login01/',views.login01,name='login01'),
path('index01/',views.index01,name='index01'),
path('logout01/',views.logout01,name='logout01'),
]
index01.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>测试</p>
<a href="/login/logout01/"><span>退出</span></a>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin: 15% 40%;">
<h1>欢迎登录!</h1>
<form action="/login/login01/" method="post">{% csrf_token %}
<p>
<label for="id_username">用户:</label>
<input type="text" id="id_username" name="username" placeholder="用户名" autofocus required />
</p>
<p>
<label for="id_password">密码:</label>
<input type="password" id="id_password" placeholder="密码" name="password" required >
</p>
<input type="submit" value="确定">
</form>
</div>
</body>
</html>
firstpage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
{% load static %}
<body background="{% static 'images/bg.png' %}"></body>
<center><p>登陆成功</p>
<form action="/login/" method="post">{% csrf_token %}
<input type="submit" value="退出">
</form>
</center>
</body>
</html>
设置数据库users表的用户
运行登陆账号
输入账号登陆成功
有问题欢迎留言!