第一个django项目
分享一下我老師大神的人工智能教程!零基礎(chǔ),通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉(zhuǎn)載本篇文章。分享知識(shí),造福人民,實(shí)現(xiàn)我們中華民族偉大復(fù)興!
說(shuō)在前面,這篇文章是為記錄下我個(gè)人的操作記錄,不一定適合你的情況,但你可以當(dāng)作參考 :)
關(guān)于環(huán)境搭建你可以看這里
這個(gè)第一個(gè)django項(xiàng)目我是參考《django web開發(fā)指南》這本書的,注意這本書是09年出版的,現(xiàn)在django已經(jīng)更新了,所以書上的講解和現(xiàn)在的操作會(huì)有一些出入,如果你也剛好是看這本書,苦于書上的介紹與實(shí)際有出入,那我的文章可能剛好能幫助你解決這個(gè)問(wèn)題
我的環(huán)境:?
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
服務(wù)器 : 這里我選擇了django自帶的
流程:
開始:
首先選擇一個(gè)目錄(你喜歡), 在其下面運(yùn)行下面的命令
django-admin.py startproject firstprojfirstproj? 是你自己的設(shè)定在我的環(huán)境下,得到了這些文件
firstproj/? manage.py那么這些文件在我的環(huán)境上去了哪里呢? 沒(méi)錯(cuò),看到我的目錄下存在 firstproj 和 manage.py ,所以manage.py 獨(dú)立放在了外面,而其他文件(_init__.py setting.py urls.py)就放在firstproj/firstproj/下,在我的環(huán)境下是這樣的:
注意是?firstproj/firstproj/
__init__.py? settings.py? urls.py? wsgi.py注意這里是多了 wsgi.py
這時(shí)候你運(yùn)行??./manage.py runserver ,然后在瀏覽器輸入 http://127.0.0.1:8000就能看到一個(gè)提示頁(yè)面,“ It Worked!"
繼續(xù):
現(xiàn)在我需要用mysql新建一個(gè)數(shù)據(jù)庫(kù)
mysql -u root -pcreate database firstproj;新建了一個(gè)數(shù)據(jù)庫(kù) firstproj?show databases;+--------------------+| Database?????????? |+--------------------+| information_schema || firstproj????????? || mysql????????????? || performance_schema || test?????????????? |+--------------------+5 rows in set (0.00 sec)數(shù)據(jù)庫(kù)建好了,這時(shí)候就去 setting.py 里修改關(guān)于數(shù)據(jù)庫(kù)的信息DATABASES = {??? 'default': {??????? 'ENGINE': 'django.db.backends.mysql', # 這里添加了mysql??????? 'NAME': 'firstproj',????????????????????? # 注意這里改了??????? # The following settings are not used with sqlite3:??????? 'USER': 'root', # 這里改了root??????? 'PASSWORD': '你數(shù)據(jù)庫(kù)的密碼',??????? '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/
執(zhí)行命令
./manage.py startapp blog創(chuàng)建應(yīng)用 blog, 完成之后 ls 一下, 如下:blog? firstproj? manage.py由于創(chuàng)建了應(yīng)用, 進(jìn)入setting.py 注冊(cè)一下,修改如下: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',)保存退出, 進(jìn)入blog目錄下,修改 models.py , 如下:from django.db import modelsclass BlogPost(models.Model):?title = models.CharField(max_length=150)?boby = models.TextField()?timestamp = models.DateTimeField()保存退出。
這時(shí)候在 firstproj/ 下運(yùn)行:
./manage.py syncdb由于之前提到的步驟中我是按照書上介紹的操作,所以都出現(xiàn)了如下的錯(cuò)誤: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這條指令操作成功會(huì)出現(xiàn)以下信息, 初始化信息: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)
到現(xiàn)在, 就要設(shè)置自動(dòng) admin 應(yīng)用:
打開 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)), #修改這里)其實(shí)文件中的注釋已經(jīng)很好的做了一個(gè)說(shuō)明接著修改 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) #添加這句保存退出再執(zhí)行一次
./manage.py syncdb最后,開啟 django 自帶服務(wù)器, 執(zhí)行 ./manage.py runserver
然后在瀏覽器中輸入?http://127.0.0.1:8000/admin/
如無(wú)意外你會(huì)看到這個(gè)畫面
輸入在 setting.py 中設(shè)置的用戶名和密碼,然后你會(huì)看到下面這個(gè)畫面:
如果你成功看到這個(gè)畫面,說(shuō)明你成功了, 恭喜!
給我老師的人工智能教程打call!http://blog.csdn.net/jiangjunshow
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的第一个django项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 个人笔记-vuex
- 下一篇: 电脑桌面数字时钟c语言,DesktopD