django學(xué)習(一、關(guān)于安裝django)

關(guān)于安裝django

1.安裝python

到目前為止,django是1.8.2版本,django使用的python是3.2或者以上

一般linux默認的python版本是2.6,所以如果需要部署django 1.8.2的話,則需要安裝python3.2以上,現(xiàn)在的python已經(jīng)3.4了,所以直接安裝python3.4
https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz

安裝文檔:https://docs.python.org/3/using/index.html

大概步驟是:

1.yum install zlib(避免報錯:RuntimeError: Compression requires the (missing) zlib module)
2.yum install zlib-deve
3.下載軟件包
4.解壓并進入目錄
5.進行make編譯
    ./configure

    make

    make install

編譯安裝好之后
會出現(xiàn)python3.4的命令,python對于多版本處理的問題是這樣的,他會自動將版本區(qū)分:

[root@iZ ~]# python
python             python2            python2.6          python3            python3.4          python3.4-config   python3.4m         python3.4m-config  python3-config    

默認python是python2.6

[root@iZ2 ~]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14)

如果你要用python3的話,那么你可以用python3或者python3.4 這個命令,這樣的話你的python就會變成python3的,這是多版本共存的情況,另外如果你想直接用python就是python3的話,那么你可以

[root@iZ ~]# cp /usr/local/bin/python3.4 /usr/bin/python
[root@iZ ~]# python
Python 3.4.3 (default, May 12 2015, 15:06:25) 

2.安裝django

在安裝django1.82的過程中可能會遇到缺少setuptools的情況,

1.setuptools編譯安裝
2.下載setuptools軟件包 https://pypi.python.org/pypi/setuptools
3.解壓包并進入包目錄
4.安裝
    python setup.py install (如果沒有做修改python命令那步的話,就是python3.4 setup.py install)

安裝完settools后再安裝django1.8.2

1.下載離線包 https://www.djangoproject.com/download/
2.解壓包
3.進入包目錄
4.安裝
    python setup.py install

安裝完后檢查

python -c "import django; print(django.get_version())"
1.8.2

3.安裝完django之后開始部署應(yīng)用

先說明一下django的應(yīng)用的架構(gòu),django是用project來存放數(shù)據(jù)的,一個project下面會有多個應(yīng)用,一個應(yīng)用可以代表為一個網(wǎng)站。

關(guān)于project的含義:

project
A Python package – i.e. a directory of code – that contains all the settings for an instance of Django. This would include database configuration, Django-specific options and application-specific settings.

https://docs.djangoproject.com/en/1.8/glossary/#term-project

establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.

https://docs.djangoproject.com/en/1.8/intro/tutorial01/

Projects vs. apps

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.

https://docs.djangoproject.com/en/1.8/intro/tutorial01/

總而言之:
1.project 包含應(yīng)用數(shù)據(jù),數(shù)據(jù)庫連接配置,django配置等文件。
2.app是一個web 應(yīng)用,一個app可以存在多個project中。
3.project可以包含多個app。

4.創(chuàng)建一個project,叫mysite

創(chuàng)建project

django-admin startproject mysite

創(chuàng)建完之后會生成以下這些目錄和文件

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

These files are:

  • The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
    這里外面的mysite是你的project名字,可以改成任何的名字,無所謂的,只是宣布你這個目錄是一個project

  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py
    manage.py是一個命令功能文件,一些命令例如python manage.py migrate 就是調(diào)用這個文件進行一些功能命令的使用。.

  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
    里面的mysite目錄是真正的django project目錄,在需要的時候會用來進行包導(dǎo)入,包導(dǎo)入的時候需要包的名字,這個就是。

  • mysite/init.py: An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you’re a Python beginner.)
    初始化文件init.py,空文件,用來告訴python這個目錄是需要被納入使用的

  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
    配置文件settings.py,所有關(guān)于這個project的配置都會在這里配置。

  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
    urls.py配置文件,是用告訴django,根據(jù)來源的請求去不同url。

  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGIfor more details.
    跟wsgi有關(guān)系的配置文件。

5.在你的project里面建立一個應(yīng)用

創(chuàng)建應(yīng)用app

python manage.py startapp polls

mysite
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── polls
│   ├── __init__.py
│   ├── admin.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

應(yīng)用polls會含有一些新的文件:

  • models.py
    這個是跟數(shù)據(jù)庫溝通的python文件,根據(jù)mvc架構(gòu),這個屬于m,負責數(shù)據(jù)這塊。
  • admin.py
    django的后臺配置文件,可以利用它來建立一個管理后臺。
  • migrations
    這是一個目錄,是做類似操作的時候,存放相關(guān)文件的目錄,migration是一個方便的django管理數(shù)據(jù)庫的工具。例如python manage.py makemigrations polls
  • tests.py
    做調(diào)試程序的時候使用的。
  • views.py
    控制展現(xiàn)數(shù)據(jù)的python文件,mvc架構(gòu)的c,控制器controller。
  • mvc中的v就是web界面了,在django也可以用template來代表
    MVC 是一種使用 MVC(Model View Controller 模型-視圖-控制器)設(shè)計創(chuàng)建 Web 應(yīng)用程序的模式:

    1.Model(模型)表示應(yīng)用程序核心(比如數(shù)據(jù)庫記錄列表)。
    2.View(視圖)顯示數(shù)據(jù)(數(shù)據(jù)庫記錄)。
    3.Controller(控制器)處理輸入(寫入數(shù)據(jù)庫記錄)。

參考:

1.https://docs.djangoproject.com/en/1.8/intro/tutorial01/
2.http://djangogirlstaipei.gitbooks.io/django-girls-taipei-tutorial/content/django/introduction.html

原文鏈接:http://www.godblessyuan.com/2015/06/08/django_lerning_chapter_1_about_init_django/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容