Toggle navigation


Django镜像手册

Version 1.0.0

Django是一个开放源代码的Web应用框架,由Python写成。采用了MVC的框架模式,即模型M,视图V和控制器C。它最初是被开发来用于管理劳 伦斯出版集团旗下的一些以新闻内容为主的网站的,即是CMS(内容管理系统)软件。并于2005年7月在BSD许可证下发布。这套框架是以比利时的吉普赛 爵士吉他手Django Reinhardt来命名的。

首页验证:

镜像安装完成后,服务已经自动启动。使用浏览器打开首页demo:http://IP地址,首页正常显示,则表示环境安装成功。

如果安装后,不能访问首页,到云的管理控制台重启一下服务器,待服务器启动完全完成后再次打开首页。(原因:镜像安装后,首次启动服务没有自动启动,重启一般就解决了,以后重启也都会自动启动)

软件安装在/data/django*(*表示django不同的版本号,以下不再单独说明)。

首页对应目录:/data/django*/apache2/htdocs,该目录包含首页index等文件。

版本

  - Django 1.9.7
  - Python 2.7.11
  - SQLite 3.7.15.1
  - MySQL 5.5.50
  - Apache 2.4.20
  - PostgreSQL 9.5.3

服务随操作系统自动启动,如果修改配置参数后,需要手工启停服务。

[root@iZ94bojtvkpZ ~]# cd /data/django***
[root@iZ94bojtvkpZ django1.9.7]# ./ctlscript.sh help
usage: ./ctlscript.sh help
./ctlscript.sh (start|stop|restart|status)
./ctlscript.sh (start|stop|restart|status) mysql
./ctlscript.sh (start|stop|restart|status) apache
./ctlscript.sh (start|stop|restart|status) tomcat

help – this screen
start – start the service(s)
stop – stop the service(s)
restart – restart or start the service(s)
status – show the status of the service(s)

命令举例:

[root@iZ94bojtvkpZ django-9.1.2.0]# ./ctlscript.sh restart

软件安装在C:websoft9django*(*表示django不同的版本号,以下不再单独说明)。

首页对应目录:C:websoft9django*apache2htdocs,该目录包含首页index等文件。

版本

  - Django 1.9.7
  - Python 2.7.11
  - SQLite 3.7.15.1
  - MySQL 5.5.50
  - Apache 2.4.20
  - PostgreSQL 9.5.3

服务随操作系统自动启动,如果手工修改配置参数后,需要重新启停服务,有两种方法。

方法一:在“开始”-> “管理工具”->“服务”中重启django***服务。

方法二:在C:websoft9django*中找到“manager-windows”。

双击打开“manager-windows”,可以进行服务启停、参数配置。

Linux默认管理员/密码:root/123456

Windows默认管理员/密码:root/websoft9

Linux默认管理员/密码:root/123456

Windows默认管理员/密码:root/websoft9

How can I run a command in the Django Stack?

All native installers for Bitnami Stacks are completely self-contained and run independently of the rest of the software or libraries installed on your system. To work correctly, certain environment variables need to be set before accessing different binaries in the stack.

The Bitnami console is a script to load the stack environment. This console is useful to run any command included in the stack: pythonpipopenssleasy_install among others.

On Linux:

At the server console, enter the following commands:

$ cd ~/djangostack-VERSION
$ ./use_django

On Windows:

From the Start Menu, click the Start -> Bitnami Django Stack -> Use Django shortcut.

 

How to start with the sample Django project?

Access the Bitnami Django Stack via your browser at http://localhost (Windows) or http://localhost:8080(Mac OS X and Linux).

Access the sample project via your browser at http://localhost/Project (Windows) orhttp://localhost:8080/Project (Mac OS X and Linux).

The Django project files are located at installdir/apps/django/django_projects.

Here is an example of what you will see:

How to create a new Django project?

The location of the Django projects folder depends on the platform, as shown below:

On Linux:

The projects folder is located at installdir/apps/django/django_projects.

On Windows:

  • For 2000, XP and 2003 Server, the projects folder is located at C:Documents and SettingsUSERBitnami DjangoStack Projects.
  • For Vista, 7 and 2008 Server, the projects folder is located at C:UsersUSERBitnami DjangoStack Projects.

 

How to start a Django project?

For development, use the standalone server in Django by executing the following command inside your project folder:

$ python manage.py runserver IP:PORT

IP and PORT are optional parameters. 127.0.0.1:8000 will be used by default if you don’t specify any of them. If you plan to remotely access the server, you should use the IP address 0.0.0.0, or the server’s network IP address.

IMPORTANT: Remember to open the port used by the Django server in the server firewall. Refer to these instructions for more information.
Production deployment

For a production environment, we recomend configuring Apache with the mod_wsgi module before starting to serve your application. On Bitnami Cloud Images and Virtual Appliances, mod_wsgi is already installed and activated by default.

1. First, modify the WSGI application script file atinstalldir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py. Remember to replace PROJECT with the actual path and name to your Django project.

Although the exact content may be different depending on your Django version, ensure that the content is similar to the code below and that you add your project to the path with sys.path.append.

import os
import sys
sys.path.append('installdir/apps/django/django_projects/PROJECT')
os.environ.setdefault("PYTHON_EGG_CACHE", "installdir/apps/django/django_projects/PROJECT/egg_cache")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
2. Create a conf/ folder in the installdir/apps/django/django_projects/PROJECT/ directory.
$ mkdir installdir/apps/django/django_projects/PROJECT/conf

3. Create the following files:

touch installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
touch installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf
4. On Linux and Mac OS X, you can run the application with mod_wsgi in daemon mode. Add the following code in installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf:
<IfDefine !IS_DJANGOSTACK_LOADED>
  Define IS_DJANGOSTACK_LOADED
  WSGIDaemonProcess wsgi-djangostack processes=2 threads=15 display-name=%{GROUP}
</IfDefine>

WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
<Directory "installdir/apps/django/django_projects/PROJECT/PROJECT/">
    WSGIProcessGroup wsgi-djangostack
    WSGIApplicationGroup %{GLOBAL}
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>
</Directory>
On Windows, the daemon mode for mod_wsgi is not support and the configuration will be slightly different:
WSGIScriptAlias /PROJECT 'installdir/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
<Directory "installdir/apps/django/django_projects/PROJECT/PROJECT/">
    WSGIApplicationGroup %{GLOBAL}
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>
</Directory>

5. In the installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-app.conf"

6. Add the line below to the installdir/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"

7. Restart the Apache server.

$ installdir/ctlscript.sh restart apache

You should now be able to browse to your project at http://[server-IP-address]/PROJECT (Windows) orhttp://[server-IP-address]/PROJECT (Linux and Mac OS X).

 

If you wish to configure a database for your project, configure the settings.py file with the following settings. The examples below show how to configure the database connection with a database called “djangostack”. Remember to replace /opt/bitnami with the actual path to your stack installation directory for native installers.

For MySQL:

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangostack',
        'HOST': '/opt/bitnami/mysql/tmp/mysql.sock',                                                 
        'PORT': '3306',
        'USER': 'USERNAME',
        'PASSWORD': 'PASSWORD'
    }
}

For PostgreSQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangostack',
        'HOST': '/opt/bitnami/postgresql',
        'PORT': '5432',
        'USER': 'USERNAME',
        'PASSWORD': 'PASSWORD'
    }
}
NOTE: The database connection in the above configuration file is configured to use a socket. If you are using the Bitnami Django Stack on Windows, you should instead replace the HOST parameter with ‘HOST’: ‘127.0.0.1’.

For SQLite:

 DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/opt/bitnami/apps/django/django_projects/Project/Project/Project.db',
    }
}

查看中文手册:http://python.usyiyi.cn/django_182/index.html