Django 连接mysql数据库与部署
Posted IT自动化测试与运维
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 连接mysql数据库与部署相关的知识,希望对你有一定的参考价值。
1、修改c:/apache24/conf/httpd.conf 配置文件,把项目文件的文件夹更改为现在的 E:\info_manage\info_manage
该文件的关键代码是:
LoadFile "e:/ProgramData/Anaconda3/envs/python34/python34.dll" LoadModule wsgi_module "e:/ProgramData/Anaconda3/envs/python34/Lib/site-packages/mod_wsgi/server/mod_wsgi.pyd" #LoadModule wsgi_module modules/mod_wsgi.so WSGIPythonHome "e:/ProgramData/Anaconda3/envs/python34"
LoadFile "e:/ProgramData/Anaconda3/envs/python34/python34.dll" LoadModule wsgi_module "e:/ProgramData/Anaconda3/envs/python34/Lib/site-packages/mod_wsgi/server/mod_wsgi.pyd" #LoadModule wsgi_module modules/mod_wsgi.so WSGIPythonHome "e:/ProgramData/Anaconda3/envs/python34"
启动测试环境
conda activate python34
e:\info_manage
python manage.py runserver
服务器启动正常,浏览器打开正常
关闭测试环境,c:\apache24\bin
httpd.exe -k install httpd.exe -k start
启动正常,浏览器打开失败
检查apache24/conf/httpd.conf或工程目录下的wsgi文件
三是没有添加前端控件,django_bootstrap3,还有更大的问题是我的调试电脑python安装在D盘,而部署的服务器的python 安装在E盘,咋办呀。。。。
info_manage/setting.py 文件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'assets'
'bootstrap3',
]
ALLOWED_HOSTS = ['127.0.0.1', 'localhost',"192.168.67.89"]
Listen 0.0.0.0:8089
Listen [::0]:8089
ScriptAlias /cgi-bin/ "C:/Apache24/cgi-bin/"
<Directory "C:/Apache24/cgi-bin">
</Directory>
#指定myweb项目的wsgi.py配置文件路径
WSGIScriptAlias / E:/info_manage/info_manage/wsgi.py
#指定项目路径
WSGIPythonPath E:/info_manage/
<Directory E:/info_manage/info_manage>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
hack 电脑 httpd.conf
LoadFile "d:/Anaconda3/envs/python34/python34.dll"
LoadModule wsgi_module "d:/Anaconda3/envs/python34/Lib/site-packages/mod_wsgi/server/mod_wsgi.pyd"
#LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome "d:/Anaconda3/envs/python34"
#LoadModule php5_module "e:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
#PHPIniDir e:/wamp/bin/php/php5.5.12
DocumentRoot "c:/wsgi/www/"
ScriptAlias /cgi-bin/ "C:/Apache24/cgi-bin/"
<Directory "C:/Apache24/cgi-bin">
</Directory>
#指定myweb项目的wsgi.py配置文件路径
WSGIScriptAlias / d:/info_manage/info_manage/wsgi.py
#指定项目路径
WSGIPythonPath d:/info_manage/
<Directory d:/info_manage/info_manage>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
连接mysql数据哭,工程目录改为info_manage-mysql ,
mysql> set password for root@localhost = password('root');#修改mysql数据库密码
https://blog.csdn.net/zcwforali/article/details/60597124
wamper下也跟着修改
mysql数据库的名字不能叫check,改名checkrecord,手动创建数据库后
修改info_manage文件加下的setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST':'127.0.0.1',
'PORT':'3306',
'NAME': 'assets',
'USER':'root',
'PASSWORD':'root',
'OPTIONS':{
'init_command':"SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
_init_.py
import pymysql
pymysql.install_as_MySQLdb()
然后执行数据库迁移
python manage.py migrate
最后重新创建超级管理员密码
python manage.py createsuperuser
from assets.models import Assets
#资产管理
@login_required
def assets_manage(request):
#username=request.COOKIES.get('user','') #读取浏览器cookie
assets_list=Assets.objects.all()
username=request.session.get('user','') #读取浏览器session
return render(request,"assets_manage.html",{"user":username,"assets":assets_list})
照猫画虎,初步实现了django和bootstrap3的集成,下面的还需要修改,就这样吧
<html>
<head>
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
<title> 资产管理主界面</title>
</head>
<body role="document">
<!-- 导航栏目-->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/assets_manage/">资产管理系统</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">发布会</a></li>
<li><a href="/guest_manage/">嘉宾</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">{{user}}</a></li>
<li><a href="/logout/">退出</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container theme-showcase" role="main">
<!--查询发布会-->
<div class="page-header" style="margin-top: 80px;">
<div id="navbar" class="navbar-collapse collapse" style="float:left;">
<form class="navbar-form" method="get" action="/search_name/">
<div class="form-group">
<input name="name" type="text" placeholder="名称" class="form-control">
</div>
<button type="submit" class="btn btn-success">搜索</button>
</form>
</div><!--/.navbar-collapse -->
<div "float:right;">
<button type="submit" class="btn btn-primary" onclick="window.location.href='/add_event/'" style="margin-top: 8px;">添加</button>
</div>
</div>
<!--发布会表单-->
<div class="row" >
<div class="col-md-6" style="width: 900px;">
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>名称</th>
<th>时间</th>
<th>人数</th>
<th>状态</th>
<th>签到</th>
<!-- <th>签到正式</th> -->
</tr>
</thead>
<tbody>
{% for asset in assets %}
<tr>
<td>{{ asset.id }}</td>
<td>{{ asset.name }}</td>
<td>{{ asset.unit }}</td>
<td>{{ asset.start_time | date:"Y-m-d H:i:s" }}</td>
<td>{{ asset.memo }}</td>
<td>{{ asset.status }}</td>
<td><a href="/sign_index/{{ asset.id }}/" target="{{ event.id }}_blank">sign</a></td>
<!-- <td><a href="/sign_index2/{{ event.id }}/" target="{{ event.id }}_blank">sign_web</a></td>
-->
</tr>
{% endfor %}
{{ hint }}
</tbody>
</table>
</div>
</div>
</body>
</html>
以上是关于Django 连接mysql数据库与部署的主要内容,如果未能解决你的问题,请参考以下文章
基于Ubuntu Server 16.04 LTS版本安装和部署Django之:安装MySQL数据库