簡要步驟。(Ubuntu14.04)
記錄一下我的部署過程,也方便一些有需要的童鞋,大神勿噴~
一、Python環境搭建
操作系統Ubuntu14.04,自帶Python2.7.6
im@58user:/$ pythonPython 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>
二、Django環境搭建
目前Django的版本已經到1.11了。先去官網下載Linux對應的文件,然后解壓&安裝。(官網下載地址)
tar xzvf Django-1.11.x.tar.gzcd Django-1.11.xsudo python setup.py install
這時可能會提示ImportError: No module named setuptools
執行
sudo https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
然后執行
python setyp.py install```
到此Django安裝成功~!
三、Mysql安裝
執行一下命令,運行過程中可能需要輸入root密碼并進行確認。
sudo apt-get install mysql-server mysql-clientsudo apt-get install libmysqld-dev
然后鏈接MySQL和Python
sudo apt-get install python-devsudo wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zipunzip MySQL-python-1.2.5.zipcd MySQL-python-1.2.5/sudo python setup.py install
進入mysql數據庫的方式:
> * sudo mysql* mysql -u root -p 然后輸入密碼
四、給mysql設置root密碼
先以第一種方式進入mysql
mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> update user set Password = PASSWORD(‘root') where User ='root';Query OK, 3 rows affected (0.00 sec)Rows matched: 3 Changed: 3 Warnings: 0mysql> exit
括號里面的'root'就是新的密碼
五、新建項目
到了驗證結果的時候了
將當前目錄切換到Python的worspace下,輸入新建的項目名稱:
im@58user:~/PythonProjects$django-admin.py startproject Helloim@58user:~/PythonProjects$ cd Hello/im@58user:~/PythonProjects/Hello$ tree├── Hello│ ├── init.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py└── manage.py
接下來我們寫一個HelloWorld頁面。
在Hello文件下的一級目錄創建views.py文件
im@58user:~/PythonProjects/Hello$ touch views.pyim@58user:~/PythonProjects/Hello$ lsHello manage.py views.pyim@58user:~/PythonProjects/Hello$ tree.├── Hello│ ├── init.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py├── manage.py└── views.py1 directory, 6 files
在views.py文件中寫入下面代碼
from django.http import HttpResponsedef hello(request):return HttpResponse(“Hello World~!~!”)
然后再將路徑添加在urls.py文件中
from django.conf.urls import urlfrom django.contrib import adminfrom views import hellourlpatterns = [url(r'^admin/‘, admin.site.urls),url(r'^hello/‘, hello),]
然后在Hello目錄下執行python manage.py runserver 0.0.0.0:8080
啟動服務器
打開瀏覽器訪問http://127.0.0.1:8000/hello/
可以看到展示結果。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
新聞熱點
疑難解答
圖片精選