第一个django项目
分享一下我老師大神的人工智能教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
說在前面,這篇文章是為記錄下我個人的操作記錄,不一定適合你的情況,但你可以當作參考 :)
關于環境搭建你可以看這里
這個第一個django項目我是參考《django web開發指南》這本書的,注意這本書是09年出版的,現在django已經更新了,所以書上的講解和現在的操作會有一些出入,如果你也剛好是看這本書,苦于書上的介紹與實際有出入,那我的文章可能剛好能幫助你解決這個問題
我的環境:?
ubuntu 12.04?
python 2.7.3
django 1.5.1?
mysql ?Ver 14.14 Distrib 5.5.29, for debian-linux-gnu (x86_64) using readline 6.2
服務器 : 這里我選擇了django自帶的
流程:
開始:
首先選擇一個目錄(你喜歡), 在其下面運行下面的命令
django-admin.py startproject firstprojfirstproj? 是你自己的設定在我的環境下,得到了這些文件
firstproj/? manage.py那么這些文件在我的環境上去了哪里呢? 沒錯,看到我的目錄下存在 firstproj 和 manage.py ,所以manage.py 獨立放在了外面,而其他文件(_init__.py setting.py urls.py)就放在firstproj/firstproj/下,在我的環境下是這樣的:
注意是?firstproj/firstproj/
__init__.py? settings.py? urls.py? wsgi.py注意這里是多了 wsgi.py
這時候你運行??./manage.py runserver ,然后在瀏覽器輸入 http://127.0.0.1:8000就能看到一個提示頁面,“ It Worked!"
繼續:
現在我需要用mysql新建一個數據庫
mysql -u root -pcreate database firstproj;新建了一個數據庫 firstproj?show databases;+--------------------+| Database?????????? |+--------------------+| information_schema || firstproj????????? || mysql????????????? || performance_schema || test?????????????? |+--------------------+5 rows in set (0.00 sec)數據庫建好了,這時候就去 setting.py 里修改關于數據庫的信息DATABASES = {??? 'default': {??????? 'ENGINE': 'django.db.backends.mysql', # 這里添加了mysql??????? 'NAME': 'firstproj',????????????????????? # 注意這里改了??????? # The following settings are not used with sqlite3:??????? 'USER': 'root', # 這里改了root??????? 'PASSWORD': '你數據庫的密碼',??????? 'HOST': '',????????????????????? # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.??????? 'PORT': '',????????????????????? # Set to empty string for default.??? }}
保存退出,回到 firstproj/ 路徑, 注意不是 firstproj/firstproj/
執行命令
./manage.py startapp blog創建應用 blog, 完成之后 ls 一下, 如下:blog? firstproj? manage.py由于創建了應用, 進入setting.py 注冊一下,修改如下:INSTALLED_APPS = (??? 'django.contrib.auth',??? 'django.contrib.contenttypes',??? 'django.contrib.sessions',??? 'django.contrib.sites',??? 'django.contrib.messages',??? 'django.contrib.staticfiles',?'blog', #添加這行! 不是寫 'firstproj.blog'??? # Uncomment the next line to enable the admin:??? #'django.contrib.admin',??? # Uncomment the next line to enable admin documentation:??? # 'django.contrib.admindocs',)保存退出, 進入blog目錄下,修改 models.py , 如下:from django.db import modelsclass BlogPost(models.Model):?title = models.CharField(max_length=150)?boby = models.TextField()?timestamp = models.DateTimeField()保存退出。
這時候在 firstproj/ 下運行:
./manage.py syncdb由于之前提到的步驟中我是按照書上介紹的操作,所以都出現了如下的錯誤:ImportError: No module named fisrtpoj.blogImportError: No module named firstpoj.blogImportError: No module named blogOperationalError: (1045, "Access denied for user 'sheng'@'localhost' (using password: YES)")OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")./manage.py syncdb這條指令操作成功會出現以下信息, 初始化信息:Creating tables ...Creating table auth_permissionCreating table auth_group_permissionsCreating table auth_groupCreating table auth_user_groupsCreating table auth_user_user_permissionsCreating table auth_userCreating table django_content_typeCreating table django_sessionCreating table django_siteCreating table blog_blopostYou just installed Django's auth system, which means you don't have any superusers defined.Would you like to create one now? (yes/no): yesUsername (leave blank to use 'sheng'): Email address: 你的郵箱Password: Password (again): Superuser created successfully.Installing custom SQL ...Installing indexes ...Installed 0 object(s) from 0 fixture(s)
到現在, 就要設置自動 admin 應用:
打開 setting.py , 修改如下
INSTALLED_APPS = (??? 'django.contrib.auth',??? 'django.contrib.contenttypes',??? 'django.contrib.sessions',??? 'django.contrib.sites',??? 'django.contrib.messages',??? 'django.contrib.staticfiles',?'blog',??? # Uncomment the next line to enable the admin:??? 'django.contrib.admin', #修改了這里??? # Uncomment the next line to enable admin documentation:??? # 'django.contrib.admindocs',)保存退出打開urls.py , 修改如下(注意這里跟書上是不同的):
from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:from django.contrib import admin #修改這里admin.autodiscover() #修改這里urlpatterns = patterns('',??? # Examples:??? # url(r'^$', 'firstproj.views.home', name='home'),??? # url(r'^firstproj/', include('firstproj.foo.urls')),??? # Uncomment the admin/doc line below to enable admin documentation:??? # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),??? # Uncomment the next line to enable the admin:???? url(r'^admin/', include(admin.site.urls)), #修改這里)其實文件中的注釋已經很好的做了一個說明接著修改 blog/models.py ,修改如下:
from django.db import modelsfrom django.contrib import admin #添加這句class BlogPost(models.Model):?title = models.CharField(max_length=150)?boby = models.TextField()?timestamp = models.DateTimeField()admin.site.register(BlogPost) #添加這句保存退出再執行一次
./manage.py syncdb最后,開啟 django 自帶服務器, 執行 ./manage.py runserver
然后在瀏覽器中輸入?http://127.0.0.1:8000/admin/
如無意外你會看到這個畫面
輸入在 setting.py 中設置的用戶名和密碼,然后你會看到下面這個畫面:
如果你成功看到這個畫面,說明你成功了, 恭喜!
給我老師的人工智能教程打call!http://blog.csdn.net/jiangjunshow
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的第一个django项目的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 个人笔记-vuex
 - 下一篇: 电脑桌面数字时钟c语言,DesktopD