前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS 7 安装Django

CentOS 7 安装Django

作者头像
shaonbean
发布2019-05-26 09:41:42
1.3K0
发布2019-05-26 09:41:42
举报
文章被收录于专栏:运维前线

版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://cloud.tencent.com/developer/article/1434716

一、Django简介

  • Django是python中目前风靡的Web Framework, 那么什么叫做Framework呢, 框架能够帮助你把程序的整体架构搭建好, 而我们所需要做的工作就是填写逻辑, 而框架能够在合适的时候调用你写的逻辑, 而不需要我们自己去调用逻辑, 让Web开发变的更敏捷.
  • Django是一个高级Python Web框架, 鼓励快速,简洁, 以程序设计的思想进行开发. 通过使用这个框架, 可以减少很多开发麻烦, 使你更专注于编写自己的app, 而不需要重复造轮子. Django免费并且开源.
  • Django特点: 完全免费并开源源代码、快速高效开发、使用MTV架构(熟悉Web开发的应该会说是MVC架构) 强大的可扩展性.
  • 用户在浏览器中输入URL后的回车, 浏览器会对URL进行检查, 首先判断协议,如果是http就按照 Web 来处理, 然互调用DNS查询, 将域名转换为IP地址, 然后经过网络传输到达对应Web服务器, 服务器对url进行解析后, 调用View中的逻辑(MTV中的V), 其中又涉及到Model(MTV中的M), 与数据库的进行交互, 将数据发到Template(MTV中的T)进行渲染, 然后发送到浏览器中, 浏览器以合适的方式呈现给用户
  • 备注:上面内容摘自极客学院Django简介:http://wiki.jikexueyuan.com/project/django-set-up-blog/introduction.html

二、安装Django

  • 安装Django,它是Python Web应用程序框架。
代码语言:javascript
复制
[1]  安装一些软件包    .
# install from EPEL
[root@linuxprobe ~]# yum --enablerepo=epel -y install python-virtualenv

[2]  在Virtualenv环境下安装Django,它可以为任何普通用户,这里使用root,建议使用普通用户。   
[root@linuxprobe ~]$ virtualenv venv
[root@linuxprobe ~]$ cd ~/venv
[root@linuxprobe venv]$ source bin/activate
(venv)[root@linuxprobe venv]$ pip install django
Downloading/unpacking django
  Downloading Django-1.10.2.tar.gz (7.7MB): 7.7MB downloaded
  Running setup.py egg_info for package django

    no previously-included directories found matching 'django/contrib/admin/bin'
    warning: no previously-included files matching '__pycache__' found anywhere in distribution
Installing collected packages: django
  Running setup.py install for django

    no previously-included directories found matching 'django/contrib/admin/bin'
    warning: no previously-included files matching '__pycache__' found anywhere in distribution
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755
    changing mode of /root/venv/bin/django-admin.py to 755
    Installing django-admin script to /root/venv/bin
Successfully installed django
Cleaning up...
# 上面的警告不是问题
(venv)[root@linuxprobe venv]# django-admin --version
1.10.2
# 退出virtualenv
(venv)[root@linuxprobe venv]# deactivate
代码语言:javascript
复制
[3] 创建测试项目
[root@linuxprobe ~]$ cd ~/venv 
[root@linuxprobe venv]$ source bin/activate
# create "testproject"
(venv)[root@linuxprobe venv]$ django-admin startproject testproject 
(venv)[root@linuxprobe venv]$ cd testproject 
# configure database (default is SQLite)
(venv)[root@linuxprobe testproject]$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
# create admin user
(venv)[root@linuxprobe testproject]# python manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: root@linuxprobe.org
Password: 
Password (again): 
Superuser created successfully.
# start server
(venv)[root@linuxprobe testproject]# python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
October 31, 2016 - 14:03:16
Django version 1.10.2, using settings 'testproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
代码语言:javascript
复制
[4]从客户端计算机访问“http://(服务器的主机名或IP地址):8000 /”。如果以下网站正常显示,表示启动成功。 
代码语言:javascript
复制
[5]用户管理网站可以在“http://(服务器的主机名或IP地址):8000 / admin”
代码语言:javascript
复制
[6]创建测试应用程序
代码语言:javascript
复制
[root@linuxprobe ~]# cd venv/
[root@linuxprobe venv]# source bin/activate
(venv)[root@linuxprobe venv]# cd testproject/
(venv)[root@linuxprobe testproject]# python manage.py startapp testapp
(venv)[root@linuxprobe testproject]# vi testapp/views.py
#
# add to the end

from django.http import HttpResponse
def main(request):
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'Django Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    return HttpResponse(html)
 #
(venv)[root@linuxprobe testproject]# mv testproject/urls.py testproject/urls.py.org
(venv)[root@linuxprobe testproject]# vi testproject/urls.py
#
# create new

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^testapp/$', 'testapp.views.main'),
)
#
(venv)[root@linuxprobe testproject]# vi testproject/settings.py

# end add testapp

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'testapp',

) 
#
(venv)[root@linuxprobe testproject]$ python manage.py runserver 0.0.0.0:8000
代码语言:javascript
复制
[7]从客户端计算机访问“http://(服务器的主机名或IP地址):8000 / testapp /”。如果testapp正常显示,就OK了。 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年11月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Django简介
  • 二、安装Django
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档