这篇文章主要介绍“django搭建一个资产管理系统”,在日常操作中,相信很多人在django搭建一个资产管理系统问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”django搭建一个资产管理系统”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都企业网站建设,高端网页制作,对成都服务器租用等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业成都网站推广优化,H5建站,响应式网站。
Centos7.4 yum安装python3.6
然后安装MySQL(mariadb)
yum install -y mariadb mariadb-libs mariadb-devel mariadb-server
nginx
yum install epel-release yum install -y nginx
再安装django2.2.0
pip install django==2.2.0
-------------------------------------更新于2019.10.24--------------------------------------------
首先修改settings
把
ALLOWED_HOSTS = []修改为ALLOWED_HOSTS = ['*',] 即允许所有主机访问
把刚做好IP管理的项目整个文件夹放在服务器上,此处我选择的放在/data下
尝试执行用django自带的web服务器开启服务
cd /data/sams python manage.py run server 0:8000
发现有报错
RuntimeError: Model class login.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
然而百度了一圈我还是解决不了 所以我决定升级django版本
[root@sm-manage220 sams]# pip install Django==2.2.1 Collecting Django==2.2.1 Using cached https://files.pythonhosted.org/packages/b1/1d/2476110614367adfb079a9bc718621f9fc8351e9214e1750cae1832d4090/Django-2.2.1-py3-none-any.whl Requirement already satisfied: pytz in /usr/lib/python3.6/site-packages (from Django==2.2.1) (2019.2) Requirement already satisfied: sqlparse in /usr/lib/python3.6/site-packages (from Django==2.2.1) (0.3.0) Installing collected packages: Django Found existing installation: Django 2.2 Uninstalling Django-2.2: Successfully uninstalled Django-2.2 Successfully installed Django-2.2.1 [root@sm-manage220 sams]# pip list Package Version ----------- ------- Django 2.2.1 pip 19.3.1 PyMySQL 0.9.3 python-nmap 0.6.1 pytz 2019.2 setuptools 39.0.1 sqlparse 0.3.0 [root@sm-manage220 sams]#
然后在执行 python manage.py runserver,又特么报错了
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
这个解决就简单一点了
File "/usr/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 36, inraise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__) django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
直接去注释掉这一行就是了
好的注释掉之后出现第二个异常了
File "/usr/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query query = query.decode(errors='replace') AttributeError: 'str' object has no attribute 'decode'
将146行的decode改为encode即可
if query is not None: query = query.encode(errors='replace') return query
然后执行python manage.py runserver 0:8000就可以启动服务了(0:8000即允许所有机器都可以通过8000端口访问)
然后执行pip install uwsgi,还是报错
再安装gcc吧
yum install -y gcc
再执行 pip install uwsgi就可以了,然后开始配置它
在项目的根目录下新建一个script目录,新建一个uwsgi.ini
文件。文件名可以随便,但后缀必须是ini。
在里面写入下面的配置内容:
[uwsgi] chdir = /data/sams #整个项目的路径 module = sams.wsgi #项目的名称.wsgi master = true processes = 3 socket = 0.0.0.0:8001 #如果想用nginx再做反向代理的话就输入这个 http = 0.0.0.0:8000 #不用nginx做就输入这个,这个主要是测试当前配置的有没有问题 vacuum = true pidfile = /data/sams/script/uwsgi.pid daemonize = /data/sams/script/uwsgi.log
注意直接复制会报错找不到项目路径,实际最好把注释的文字自行删除,包括空格
可以直接用8000端口访问了,接下来我们再配置nginx
然后我们把uwsgi停掉,注释掉http那一行,改用nginx做代理
[root@sm-manage220 nginx]# ps -ef |grep uwsgi root 14200 1 0 19:01 ? 00:00:00 uwsgi --ini uwsgi.ini root 14202 14200 0 19:01 ? 00:00:00 uwsgi --ini uwsgi.ini root 14203 14200 0 19:01 ? 00:00:00 uwsgi --ini uwsgi.ini root 14204 14200 0 19:01 ? 00:00:00 uwsgi --ini uwsgi.ini root 14205 14200 0 19:01 ? 00:00:00 uwsgi --ini uwsgi.ini root 14532 13144 0 19:16 pts/0 00:00:00 grep --color=auto uwsgi [root@sm-manage220 nginx]# pkill -f uwsgi -9 [root@sm-manage220 nginx]# ps -ef |grep uwsgi root 14543 13144 0 19:16 pts/0 00:00:00 grep --color=auto uwsgi [root@sm-manage220 nginx]# vim /data/sams/script/uwsgi.ini [uwsgi] chdir = /data/sams module = sams.wsgi master = true processes = 3 socket = 0.0.0.0:8001 #http = 0.0.0.0:8000 vacuum = true pidfile = /data/sams/script/uwsgi.pid #daemonize = /data/sams/script/uwsgi.log #这种方式会以守护进程的方式存在,会导致后面设置systemctl的时候失败 logto = /data/sams/script/uwsgi.log #推荐采取这样子的方式记录log
server { listen 80; server_name localhost; access_log /data/sams/script/sams_access.log; error_log /data/sams/script/sams_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8001; uwsgi_param UWSGI_SCRIPT sams.wsgi; uwsgi_param UWSGI_CHDIR /data/sams; } location /static { alias /data/sams/static; } }
配置静态资源
cd /data/sams mkdir static vim /data/sams/sams/settings.py 在最后增加一行 STATIC_ROOT = os.path.join(BASE_DIR, 'static') 然后 cd /data/sams python manage.py collectstatic
然后启动nginx和uwsgi并设置服务的自启动
[root@sm-manage220 nginx]# systemctl restart nginx [root@sm-manage220 nginx]# cd /data/sams/script/ [root@sm-manage220 script]# uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini [root@sm-manage220 ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. #设置uwsgi为服务,方便管理 vim /usr/lib/systemd/system/uwsgi.service [Unit] Description=uWSGI instance to serve myproject After=network.target [Service] [Unit] Description=uWSGI instance to serve myproject After=network.target [Service] WorkingDirectory=/data/sams ExecStart=/usr/local/bin/uwsgi --ini /data/sams/script/uwsgi.ini ExecStop=/usr/local/bin/uwsgi --stop /data/sams/script/uwsgi.pid ExecReload=/usr/local/bin/uwsgi --reload /data/sams/script/uwsgi.pid [Install] WantedBy=multi-user.target #然后执行命令设置成开机自启动 systemctl enable uwsgi
--------------------------------------------------分割线-------------------------------------------------------
ps: 这样子的方式即可使用systemctl start/stop/reload/enable/disable进行管理,我之前的方式开机启动uwsgi是把启动命令/usr/local/bin/uwsgi --ini /data/sams/script/uwsgi.ini写在rc.local里面,并且uwsgi.ini这个文件里我使用的是daemonize记录log而不是logto,这样子就导致在重启服务器时会卡在界面,一直提示a stop job is running for /etc/rc.d/rc.local,只能强制关机,再强制开机.
--------------------------------------------------分割线-------------------------------------------------------
重启验证已ok~
到此,关于“django搭建一个资产管理系统”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!