├── .gitignore
├── It's django 範例碼使用指南.pdf
├── LICENSE
├── README.md
├── cover.jpg
├── mysite
├── bank
│ ├── __init__.py
│ ├── actions.py
│ ├── admin.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── configs
│ ├── apache2
│ │ ├── 000-default.conf
│ │ └── 000-default_with_ssl.conf
│ ├── nginx
│ │ ├── default
│ │ └── default_with_ssl
│ └── uwsgi
│ │ ├── apt
│ │ ├── mysite.ini
│ │ └── uwsgi.conf
│ │ └── pip
│ │ ├── mysite.ini
│ │ └── uwsgi.conf
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── restaurants
│ ├── __init__.py
│ ├── admin.py
│ ├── forms.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0002_auto_20141210_1828.py
│ │ ├── 0003_comment.py
│ │ ├── 0004_auto_20141216_1211.py
│ │ └── __init__.py
│ ├── models.py
│ ├── permissions.py
│ ├── templates
│ │ ├── comments.html
│ │ ├── menu.html
│ │ └── restaurants_list.html
│ ├── templatetags
│ │ ├── __init__.py
│ │ └── myfilters.py
│ ├── tests.py
│ └── views.py
├── templates
│ ├── base.html
│ ├── index.html
│ ├── math.html
│ ├── register.html
│ ├── registration
│ │ ├── logged_out.html
│ │ └── login.html
│ └── welcome.html
└── zoo
│ ├── __init__.py
│ ├── admin.py
│ ├── fixtures
│ └── dog.json
│ ├── migrations
│ ├── 0001_initial.py
│ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── requirements.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 |
5 | # C extensions
6 | *.so
7 |
8 | # Distribution / packaging
9 | .Python
10 | env/
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | lib/
17 | lib64/
18 | parts/
19 | sdist/
20 | var/
21 | *.egg-info/
22 | .installed.cfg
23 | *.egg
24 |
25 | # PyInstaller
26 | # Usually these files are written by a python script from a template
27 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
28 | *.manifest
29 | *.spec
30 |
31 | # Installer logs
32 | pip-log.txt
33 | pip-delete-this-directory.txt
34 |
35 | # Unit test / coverage reports
36 | htmlcov/
37 | .tox/
38 | .coverage
39 | .cache
40 | nosetests.xml
41 | coverage.xml
42 |
43 | # Translations
44 | *.mo
45 | *.pot
46 |
47 | # Django stuff:
48 | *.log
49 |
50 | # Sphinx documentation
51 | docs/_build/
52 |
53 | # PyBuilder
54 | target/
55 |
--------------------------------------------------------------------------------
/It's django 範例碼使用指南.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/its-django/mysite-django18/dc69cb4294eb53a1907ace4d9823c7d2163f7b46/It's django 範例碼使用指南.pdf
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## It's Django -- 用Python迅速打造Web應用
2 |
3 | 這是就 Django1.8 以上的版本所提供的範例碼專案。我們將陸續就升級至1.8版作說明。
4 | 其餘有關於本書的各種資訊,還是請讀者於原本專案(適用於Django1.7版)的github wiki上取得:
5 | * [前往本書wiki](https://github.com/its-django/mysite/wiki)
6 |
7 | ### wiki目錄
8 | * [wiki首頁](https://github.com/its-django/mysite/wiki/)
9 | * [本書資訊](https://github.com/its-django/mysite/wiki/本書資訊)
10 | * [如何取得本書](https://github.com/its-django/mysite/wiki/本書資訊#如何取得本書)
11 | * [作者介紹](https://github.com/its-django/mysite/wiki/本書資訊#作者介紹)
12 | * [範例碼](https://github.com/its-django/mysite/wiki/範例碼)
13 | * [適用版本](https://github.com/its-django/mysite/wiki/範例碼#適用版本)
14 | * [使用說明](https://github.com/its-django/mysite/wiki/範例碼#使用說明)
15 | * [範例碼分支架構](https://github.com/its-django/mysite/wiki/範例碼#範例碼分支架構)
16 | * [使用git下載與操作](https://github.com/its-django/mysite/wiki/範例碼#使用git下載與操作)
17 | * [代碼打包下載](https://github.com/its-django/mysite/wiki/範例碼#代碼打包下載)
18 | * [社群與互動](https://github.com/its-django/mysite/wiki/社群與互動)
19 | * [勘誤](https://github.com/its-django/mysite/wiki/勘誤)
20 | * [錯漏多字勘誤總表](https://github.com/its-django/mysite/wiki/勘誤/#錯字勘誤總表)
21 | * [重要參數勘誤表](https://github.com/its-django/mysite/wiki/勘誤/#重要參數勘誤表)
22 | * [開發步驟勘誤](https://github.com/its-django/mysite/wiki/勘誤/#開發步驟勘誤)
23 | * [程式碼勘誤表](https://github.com/its-django/mysite/wiki/勘誤/#程式碼勘誤表)
24 | * [學習資源](https://github.com/its-django/mysite/wiki/學習資源)
25 | * [部落格教學](https://github.com/its-django/mysite/wiki/學習資源/#部落格教學)
26 | * [相關網站](https://github.com/its-django/mysite/wiki/學習資源/#相關網站)
27 | * [好評](https://github.com/its-django/mysite/wiki/好評)
28 |
29 | ### 如何取得本書
30 |
31 | 實體書的購買管道請參考各出版社合作之[通路](http://books.gotop.com.tw/v_ACL043800)
32 |
33 | 電子書可以在[Google Play](https://play.google.com/store/books/details/%E8%A2%81%E5%85%8B%E5%80%AB_%E6%A5%8A%E5%AD%9F%E7%A9%8E_It_s_Django_%E7%94%A8Python%E8%BF%85%E9%80%9F%E6%89%93%E9%80%A0Web%E6%87%89%E7%94%A8_%E9%9B%BB%E5%AD%90%E6%9B%B8?id=C5UVCgAAQBAJ)上購買
34 |
--------------------------------------------------------------------------------
/cover.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/its-django/mysite-django18/dc69cb4294eb53a1907ace4d9823c7d2163f7b46/cover.jpg
--------------------------------------------------------------------------------
/mysite/bank/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/its-django/mysite-django18/dc69cb4294eb53a1907ace4d9823c7d2163f7b46/mysite/bank/__init__.py
--------------------------------------------------------------------------------
/mysite/bank/actions.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # encoding: utf-8
3 |
4 | import logging
5 |
6 | from django.db import transaction
7 |
8 | logging.basicConfig()
9 | logger = logging.getLogger(__name__)
10 | logger.setLevel(logging.INFO)
11 |
12 |
13 | @transaction.atomic
14 | def transfer_money(_from, _to, quota):
15 |
16 | if _from.money < 15:
17 | raise ValueError("連手續費都付不起,請回吧!!")
18 |
19 | _from.money = _from.money - 15
20 | _from.save()
21 |
22 | sid = transaction.savepoint()
23 |
24 | try:
25 | _from.money = _from.money - quota
26 | if _from.money < 0:
27 | raise ValueError("超額提領!")
28 | _from.save()
29 | _to.money = _to.money + quota
30 | if _to.money > 100000:
31 | raise ValueError("超額儲存!")
32 | _to.save()
33 | transaction.savepoint_commit(sid)
34 | except ValueError as e:
35 | logger.error("金額操作錯誤, 訊息:<{}>".format(e))
36 | transaction.savepoint_rollback(sid)
37 | except Exception as e:
38 | logger.error("其他錯誤,訊息:<{}>".format(e))
39 | transaction.savepoint_rollback(sid)
40 |
--------------------------------------------------------------------------------
/mysite/bank/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 |
3 | # Register your models here.
4 |
--------------------------------------------------------------------------------
/mysite/bank/migrations/0001_initial.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | from __future__ import unicode_literals
3 |
4 | from django.db import models, migrations
5 |
6 |
7 | class Migration(migrations.Migration):
8 |
9 | dependencies = [
10 | ]
11 |
12 | operations = [
13 | migrations.CreateModel(
14 | name='Account',
15 | fields=[
16 | ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
17 | ('name', models.CharField(max_length=64)),
18 | ('money', models.IntegerField()),
19 | ],
20 | options={
21 | },
22 | bases=(models.Model,),
23 | ),
24 | ]
25 |
--------------------------------------------------------------------------------
/mysite/bank/migrations/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/its-django/mysite-django18/dc69cb4294eb53a1907ace4d9823c7d2163f7b46/mysite/bank/migrations/__init__.py
--------------------------------------------------------------------------------
/mysite/bank/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 |
3 |
4 | class Account(models.Model):
5 |
6 | """Simple Bank account with name and money. """
7 |
8 | name = models.CharField(max_length=64)
9 | money = models.IntegerField()
10 |
--------------------------------------------------------------------------------
/mysite/bank/tests.py:
--------------------------------------------------------------------------------
1 | from django.test import TestCase
2 |
3 | # Create your tests here.
4 |
--------------------------------------------------------------------------------
/mysite/bank/views.py:
--------------------------------------------------------------------------------
1 | from django.shortcuts import render
2 |
3 | # Create your views here.
4 |
--------------------------------------------------------------------------------
/mysite/configs/apache2/000-default.conf:
--------------------------------------------------------------------------------
1 |
2 |
3 | # 改為你的email
4 | ServerAdmin your_email@gmail.com
5 |
6 | # 改至Django根目錄
7 | DocumentRoot /var/www/source/django/mysite
8 |
9 | # 參考前一小節設定,將settings.py中設定的STATIC_URL導向STATIC_ROOT位置
10 | # 需寫在WSGIScriptAlias前面,攔截靜態路徑,否則傳至django也不會被處理
11 | Alias /static/ /var/www/source/django/mysite/assets/
12 |
13 | # 第一個"/"為網址例的匹配,這裡用"/"表示全部的匹配都導向django
14 | # 所以第二個參數填寫至wsgi.py的完整路徑
15 | WSGIScriptAlias / /var/www/source/django/mysite/mysite/wsgi.py
16 |
17 | # 加入django根目錄及系統上python lib的路徑
18 | WSGIDaemonProcess mysite python-path=/var/www/source/django/mysite:/usr/loca/lib/python2.7/dict-packages
19 | WSGIProcessGroup mysite
20 |
21 |
22 |
23 | = 2.4>
24 | Require all granted
25 |
26 |
27 | Order deny,allow
28 | Allow from all
29 |
30 |
31 |
32 |
33 | ErrorLog ${APACHE_LOG_DIR}/error.log
34 | CustomLog ${APACHE_LOG_DIR}/access.log combined
35 |
36 |
37 |
--------------------------------------------------------------------------------
/mysite/configs/apache2/000-default_with_ssl.conf:
--------------------------------------------------------------------------------
1 | # 檢查是否有ssl模組
2 |
3 | # 改port為443 (https預設的port)
4 |
5 |
6 | ServerAdmin your_email@gmail.com
7 | DocumentRoot /var/www/source/django/mysite
8 |
9 | Alias /static/ /var/www/source/django/mysite/assets/
10 |
11 | WSGIScriptAlias / /var/www/source/django/mysite/mysite/wsgi.py
12 | WSGIDaemonProcess mysite python-path=/var/www/source/django/mysite:/home/user1/.virtualenvs/django/lib/python2.7/site-packages
13 | WSGIProcessGroup mysite
14 |
15 | # 加入 ssl 設定
16 | SSLEngine on
17 | SSLCertificateFile /etc/apache2/self_signed_ssl/apache.crt
18 | SSLCertificateKeyFile /etc/apache2/self_signed_ssl/apache.key
19 |
20 |
21 |
22 | = 2.4>
23 | Require all granted
24 |
25 |
26 | Order deny,allow
27 | Allow from all
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/mysite/configs/nginx/default:
--------------------------------------------------------------------------------
1 | # 定義一個名為django的來源,用Nginx反向代理的特性,跟定義的server交換資料
2 | upstream django {
3 | # 定義一個假的server,記得我們設定uwsgi是從mysite_uwsgi.sock交換資料?
4 | # 而Nginx也從這個檔案交換資料,如此一來便把兩邊串接起來
5 | server unix:///var/run/mysite_uwsgi.sock;
6 | }
7 |
8 | server {
9 | # 監聽的 port
10 | listen 80 default_server;
11 |
12 | # 伺服器網域或ip
13 | server_name yourdomain.com;
14 |
15 | location / {
16 | # 把匹配的請求傳至 django
17 | uwsgi_pass django;
18 | # 記得載入nginx uwsgi_params
19 | include /etc/nginx/uwsgi_params;
20 | }
21 |
22 | # 匹配到static時讓nginx直接到static folder取靜態檔,無需透過uwsgi及django
23 | location ~ /static/ {
24 | alias /var/www/source/django/mysite/assets/;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/mysite/configs/nginx/default_with_ssl:
--------------------------------------------------------------------------------
1 | upstream django {
2 | server unix:///var/run/mysite_uwsgi.sock;
3 | }
4 |
5 | server {
6 |
7 | server_name yourdomain.com;
8 |
9 | # 監聽 443,80 port可以關閉或並存
10 | listen 443 ssl;
11 | # 憑證檔位置
12 | ssl_certificate /etc/nginx/self_signed_ssl/nginx.crt;
13 | ssl_certificate_key /etc/nginx/self_signed_ssl/nginx.key;
14 |
15 | # ssl相關設定
16 | ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
17 | ssl_ciphers RC4:HIGH:!aNULL:!MD5;
18 | ssl_prefer_server_ciphers on;
19 |
20 | location / {
21 | uwsgi_pass django;
22 | include /etc/nginx/uwsgi_params;
23 | }
24 |
25 | location ~ /static/ {
26 | alias /path/to/your/static/;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/mysite/configs/uwsgi/apt/mysite.ini:
--------------------------------------------------------------------------------
1 | [uwsgi]
2 | # Django相關設定
3 | # 根目錄(完整路徑)
4 | chdir = /var/www/source/django/mysite
5 | # django project 下的 wsgi.file
6 | module = mysite.wsgi
7 | # 系統 python 及 site-packages 位置
8 | pythonpath = /usr/local/lib/python2.7
9 | pythonpath = /usr/local/lib/python2.7/dist-packages
10 |
11 | # Process (行程)設定
12 | master = true
13 | processes = 4
14 | # socket 檔案讓伺服器和應用程式透過系統溝通,而非再經由網路
15 | socket = /var/run/mysite_uwsgi.sock
16 | pidfile = /var/run/mysite_uwsgi.pid
17 | # socket 檔的權限
18 | chmod-socket = 666
19 | # 清除產生的檔案
20 | vacuum = true
21 |
22 | # !!! 主要與pip不同在這裡 !!!
23 | logto = /var/log/mysite_uwsgi.log
24 | plugins = python
25 |
26 | optmize = true
27 |
--------------------------------------------------------------------------------
/mysite/configs/uwsgi/apt/uwsgi.conf:
--------------------------------------------------------------------------------
1 | # simple uWSGI script in /etc/init/
2 |
3 | description "uwsgi upstart service"
4 | start on runlevel [2345]
5 | stop on runlevel [!2345]
6 | respawn
7 |
8 | exec /usr/bin/uwsgi --ini /var/www/source/django/mysite/mysite.ini
9 |
--------------------------------------------------------------------------------
/mysite/configs/uwsgi/pip/mysite.ini:
--------------------------------------------------------------------------------
1 | [uwsgi]
2 | # Django相關設定
3 | # 根目錄(完整路徑)
4 | chdir = /var/www/source/django/mysite
5 | # django project 下的 wsgi.file
6 | module = mysite.wsgi
7 | # 系統 python 及 site-packages 位置
8 | pythonpath = /usr/local/lib/python2.7
9 | pythonpath = /usr/local/lib/python2.7/dist-packages
10 |
11 | # Process (行程)設定
12 | master = true
13 | processes = 4
14 | # socket 檔案讓伺服器和應用程式透過系統溝通,而非再經由網路
15 | socket = /var/run/mysite_uwsgi.sock
16 | pidfile = /var/run/mysite_uwsgi.pid
17 | # socket 檔的權限
18 | chmod-socket = 666
19 | # 清除產生的檔案
20 | vacuum = true
21 |
22 | # 指定日誌檔路徑便可 daemon 化
23 | daemonize = /var/log/mysite_uwsgi_daemonize.log
24 |
25 | optmize = true
26 |
--------------------------------------------------------------------------------
/mysite/configs/uwsgi/pip/uwsgi.conf:
--------------------------------------------------------------------------------
1 | # simple uWSGI script in /etc/init/
2 |
3 | description "uwsgi upstart service"
4 | start on runlevel [2345]
5 | stop on runlevel [!2345]
6 | respawn
7 |
8 | exec /usr/local/bin/uwsgi --ini /var/www/source/django/mysite/mysite.ini
9 |
--------------------------------------------------------------------------------
/mysite/manage.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import os
3 | import sys
4 |
5 | if __name__ == "__main__":
6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
7 |
8 | from django.core.management import execute_from_command_line
9 |
10 | execute_from_command_line(sys.argv)
11 |
--------------------------------------------------------------------------------
/mysite/mysite/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/its-django/mysite-django18/dc69cb4294eb53a1907ace4d9823c7d2163f7b46/mysite/mysite/__init__.py
--------------------------------------------------------------------------------
/mysite/mysite/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for mysite project.
3 |
4 | Generated by 'django-admin startproject' using Django 1.8.2.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/1.8/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/1.8/ref/settings/
11 | """
12 |
13 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14 | import os
15 |
16 | from django.conf import global_settings
17 |
18 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19 |
20 |
21 | # Quick-start development settings - unsuitable for production
22 | # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
23 |
24 | # SECURITY WARNING: keep the secret key used in production secret!
25 | SECRET_KEY = 'gz909_-)w*ppzuj=*kppx0nrari4v27i9@02ic7+=i*9yq8%r5'
26 |
27 | # SECURITY WARNING: don't run with debug turned on in production!
28 | DEBUG = True
29 |
30 | ALLOWED_HOSTS = []
31 |
32 |
33 | # Application definition
34 |
35 | INSTALLED_APPS = (
36 | 'django.contrib.admin',
37 | 'django.contrib.auth',
38 | 'django.contrib.contenttypes',
39 | 'django.contrib.sessions',
40 | 'django.contrib.messages',
41 | 'django.contrib.staticfiles',
42 | 'restaurants',
43 | 'bank',
44 | 'zoo',
45 | )
46 |
47 | MIDDLEWARE_CLASSES = (
48 | 'django.contrib.sessions.middleware.SessionMiddleware',
49 | 'django.middleware.common.CommonMiddleware',
50 | 'django.middleware.csrf.CsrfViewMiddleware',
51 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
52 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
53 | 'django.contrib.messages.middleware.MessageMiddleware',
54 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
55 | 'django.middleware.security.SecurityMiddleware',
56 | )
57 |
58 | ROOT_URLCONF = 'mysite.urls'
59 |
60 | TEMPLATES = [
61 | {
62 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
63 | 'DIRS': [
64 | os.path.join(BASE_DIR, 'templates'),
65 | ],
66 | 'APP_DIRS': True,
67 | 'OPTIONS': {
68 | 'context_processors': [
69 | 'django.template.context_processors.debug',
70 | 'django.template.context_processors.request',
71 | 'django.contrib.auth.context_processors.auth',
72 | 'django.contrib.messages.context_processors.messages',
73 | ],
74 | },
75 | },
76 | ]
77 |
78 | WSGI_APPLICATION = 'mysite.wsgi.application'
79 |
80 |
81 | # Database
82 | # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
83 |
84 | DATABASES = {
85 | 'default': {
86 | 'ENGINE': 'django.db.backends.sqlite3',
87 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
88 | }
89 | }
90 |
91 |
92 | # Internationalization
93 | # https://docs.djangoproject.com/en/1.8/topics/i18n/
94 |
95 | LANGUAGE_CODE = 'zh-TW'
96 |
97 | TIME_ZONE = 'Asia/Taipei'
98 |
99 | USE_I18N = True
100 |
101 | USE_L10N = True
102 |
103 | USE_TZ = True
104 |
105 |
106 | # Static files (CSS, JavaScript, Images)
107 | # https://docs.djangoproject.com/en/1.8/howto/static-files/
108 |
109 | STATIC_URL = '/static/'
110 |
111 | SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
112 |
113 | LOGIN_REDIRECT_URL = "/index/"
114 |
115 | TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
116 | 'django.core.context_processors.request',
117 | )
118 |
119 | FIXTURE_DIRS = (
120 | os.path.join(BASE_DIR, 'fixtures'),
121 | )
122 |
--------------------------------------------------------------------------------
/mysite/mysite/tests.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # encoding: utf-8
3 |
4 |
5 | from django.test import TestCase
6 | from django.contrib.auth.models import User
7 | from django.test import client
8 |
9 |
10 | class IndexWebpageTestCase(TestCase):
11 |
12 | """Simple Webpage visiting test"""
13 |
14 | def setUp(self):
15 | """setUp function
16 | :returns: TODO
17 |
18 | """
19 | self.c = client.Client()
20 |
21 | def test_index_visiting(self):
22 | """simple index visiting
23 | :returns: TODO
24 |
25 | """
26 | resp = self.c.get('/index/')
27 | self.assertEqual(resp.status_code, 200)
28 | self.assertContains(resp, '