django

最後更新: 2016-05-10

介紹

BSD license

 


Install

 

yum install epel-release

yum install python-pip httpd mod_wsgi

yum install python-devel mysql-devel gcc wget python-setuptools

# 建立virtualenv

virtualenv mybox

source mybox/bin/activate

pip install Django==1.7

pip install MySQL-python

測試

python

>>> import django
>>> print(django.get_version())

1.9

 


設定

 

Create and Configure a New Django Project

# 它會建立 manage.py 及 "myproject" Folder 在 current directory

django-admin.py startproject myproject .

 

# Adjust the Project Settings

vim myproject/settings.py

# 設定 Static 內容的位置

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

# 由 sqlite 改成 mysql

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

改成

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        # The following settings are not used with sqlite3:
        'USER': 'dbuser',
        'PASSWORD': 'xxxx',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

 

# Complete Initial Project Setup

cd ~/myproject

./manage.py makemigrations

No changes detected

./manage.py migrate

  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK

 

makemigrations

Creates new migrations based on the changes detected to your models.

Migrations, their relationship with apps and more are covered in depth in the migrations documentation.

Providing one or more app names as arguments will limit the migrations created to the app(s) specified and any dependencies needed (the table at the other end of a ForeignKey, for example).

migrate

Synchronizes the database state with the current set of models and migrations.

Migrations, their relationship with apps and more are covered in depth in the migrations documentation.

 

# Create an administrative user

./manage.py createsuperuser

# collect all of the static content into the directory location we configured

./manage.py collectstatic
 

 

 


測試 Django

 

./manage.py runserver 0.0.0.0:8080

Test: http://server_domain_or_IP:8080

Test: http://x.x.x.x:8080/admin

deactivate

 

 


Dependence

 

yum install python-devel libjpeg-devel zlib-devel

pip install pillow

 


Install Django Module

 

pip install django-registration

 

 


Apache 設定

 

vim /etc/httpd/conf.modules.d/10-wsgi.conf

LoadModule wsgi_module modules/mod_wsgi.so

vhost 設定

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mydomain.com
    ServerAlias www.mydomain.com

    DocumentRoot /srv/www/example.com/public_html

    WSGIScriptAlias / /var/www/mydomain.com/application/index.wsgi
    WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
    WSGIProcessGroup myproject

    <Directory /var/www/mydomain.com/application>
        Options -Indexes
        <Files index.wsgi>
            Require all granted
        </Files>
    </Directory>

    ErrorLog /srv/www/example.com/logs/error.log
    CustomLog /srv/www/example.com/logs/access.log combined

    Alias /robots.txt /srv/www/example.com/public_html/robots.txt
    Alias /favicon.ico /srv/www/example.com/public_html/favicon.ico
    Alias /images /srv/www/example.com/public_html/images

    Alias /static/ /var/www/mydomain.com/static/
    <Directory /var/www/mydomain.com/static>
        Options -Indexes
        Require all granted
    </Directory>

</VirtualHost >

index.wsgi

import os
import sys

sys.path.append('/srv/www/example.com/application')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/example.com/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

WSGIDaemonProcess

WSGIDaemonProcess name [ options ]

WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages

Since we used a virtual environment, we will have to set up two path components.
The first is our project's parent directory, where the project files can be found.
The second is the lib/pythonx.x/site-packages path within our virtual environment folder
(where the Xs are replaced by the Python version number components).

settings.py

STATIC_ROOT = '/var/www/mydomain.com/static/'
STATIC_URL = '/static/'

collectstatic

python manage.py collectstatic

 

 

 

Creative Commons license icon Creative Commons license icon