創建項目
運行下面命令就可以創建壹個 django 項目,項目名稱叫 mysite :
$ django-admin.py startproject mysite
創建後的項目目錄如下:
mysite
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
說明:
__init__.py :讓 Python 把該目錄當成壹個開發包 (即壹組模塊)所需的文件。 這是壹個空文件,壹般妳不需要修改它。
manage.py :壹種命令行工具,允許妳以多種方式與該 Django 項目進行交互。 鍵入python manage.py help,看壹下它能做什麽。 妳應當不需要編輯這個文件;在這個目錄下生成它純是為了方便。
settings.py :該 Django 項目的設置或配置。
urls.py:Django項目的URL路由設置。目前,它是空的。
wsgi.py:WSGI web 應用服務器的配置文件。更多細節,查看 How to deploy with WSGI
接下來,妳可以修改 settings.py 文件,例如:修改 LANGUAGE_CODE、設置時區 TIME_ZONE
SITE_ID = 1
LANGUAGE_CODE = 'zh_CN'
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = True
上面開啟了 [Time zone](/en/1.7/topics/i18n/timezones/) 特性,需要安裝 pytz:
$ sudo pip install pytz
2. 運行項目
在運行項目之前,我們需要創建數據庫和表結構,這裏我使用的默認數據庫:
$ python manage.py migrate
Operations to perform:
?Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
?Applying contenttypes.0001_initial... OK
?Applying auth.0001_initial... OK
?Applying admin.0001_initial... OK
?Applying sessions.0001_initial... OK
然後啟動服務:
$ python manage.py runserver
妳會看到下面的輸出:
Performing system checks...
System check identified no issues (0 silenced).
January 28, 2015 - 02:08:33
Django version 1.7.1, using settings 'mysite.settings'
Starting development server at mon case, so Django provides a
# shortcut for primary-key exact lookups.
# The following is identical to Question.objects.get(id=1).
>>> Question.objects.get(pk=1)
<Question: What's up?>
# Make sure our custom method worked.
>>> q = Question.objects.get(pk=1)
# Give the Question a couple of Choices. The create call constructs a new
# Choice object, does the INSERT statement, adds the choice to the set
# of available choices and returns the new Choice object. Django creates
# a set to hold the "other side" of a ForeignKey relation
# (e.g. a question's choice) which can be accessed via the API.
>>> q = Question.objects.get(pk=1)
# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
[]
# Create three choices.
>>> q.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>
>>> q.choice_set.create(choice_text='The sky', votes=0)
<Choice: The sky>
>>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)
# Choice objects have API access to their related Question objects.
>>> c.question
<Question: What's up?>
# And vice versa: Question objects get access to Choice objects.
>>> q.choice_set.all()
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
>>> q.choice_set.count()
3
# The API automatically follows relationships as far as you need.
# Use double underscores to separate relationships.
# This works as many levels deep as you want; there's no limit.
# Find all Choices for any question whose pub_date is in this year
# (reusing the 'current_year' variable we created above).
>>> Choice.objects.filter(question__pub_date__year=current_year)
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
# Let's delete one of the choices. Use delete() for that.
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking')
>>> c.delete()
>>>
上面這部分測試,涉及到 django orm 相關的知識,詳細說明可以參考 Django中的ORM。
5. 管理 admin
Django有壹個優秀的特性, 內置了Django admin後臺管理界面, 方便管理者進行添加和刪除網站的內容.
新建的項目系統已經為我們設置好了後臺管理功能,見 mysite/settings.py:
INSTALLED_APPS = (
'django.contrib.admin', #默認添加後臺管理功能
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mysite',
)
同時也已經添加了進入後臺管理的 url, 可以在 mysite/urls.py 中查看:
url(r'^admin/', include(admin.site.urls)), #可以使用設置好的url進入網站後臺
接下來我們需要創建壹個管理用戶來登錄 admin 後臺管理界面:
$ python manage.py createsuperuser
Username (leave blank to use 'june'): admin
Email address:
Password:
Password (again):
Superuser created successfully.
總結
最後,來看項目目錄結構:
mysite
├── db.sqlite3
├── manage.py
├── mysite
│ ? ├── __init__.py
│ ? ├── settings.py
│ ? ├── urls.py
│ ? ├── wsgi.py
├── polls
│ ? ├── __init__.py
│ ? ├── admin.py
│ ? ├── migrations
│ ? │ ? ├── 0001_initial.py
│ ? │ ? ├── __init__.py
│ ? ├── models.py
│ ? ├── templates
│ ? │ ? └── polls
│ ? │ ? ├── detail.html
│ ? │ ? ├── index.html
│ ? │ ? └── results.html
│ ? ├── tests.py
│ ? ├── urls.py
│ ? ├── views.py
└── templates
└── admin
└── base_site.htm ?
通過上面的介紹,對 django 的安裝、運行以及如何創建視 圖和模型有了壹個清晰的認識,接下來就可以深入的學習 django 的自動化測試、持久化、中間件、國 際 化等知識。
如果解決了您的問題請采納!
如果未解決請繼續追問