Tải bản đầy đủ - 0 (trang)
Hour 2. Creating Your First Website

Hour 2. Creating Your First Website

Tải bản đầy đủ - 0trang

TryItYourself:CreateYourFirstDjangoProject

CreatingaDjangoprojectisrelativelysimpletodofromthe

commandprompt.Inthissection,youcreateaprojectcalled

iFriends.

1. Fromacommandprompt,changetothedirectorywhere

youwanttostorethecodefortheiFriendsproject.

2. CreateadirectorycallediFriends.Thiswillbetheroot

directoryfortheiFriendsproject.

3. ChangetotheiFriendsdirectory.

4. TypethefollowingcommandtocreatetheiFriendsproject:



pythondjango-admin.pystartprojectiFriends



WatchOut!

BecausetheprojectwillactasaPythonpackage,avoid

usingaprojectnamethatconflictswithanyexisting

built-inPythonpackages.ThedocumentationforbuiltinPythonpackagescanbefoundat

http://www.python.org.



BytheWay

Thereisnoneedtoputyourprojectcodeinadirectory

inthewebserver'sdocumentbase.TheDjango

frameworkwillberesponsibleforexecutingthecode.

Infact,itisamuchbetterideatostorethecode



somewhereoutsidethewebserver'sroot.Thatway

yourcodewillbeprotectedfrombeingaccessed

directlyfromawebbrowser.



Thestartprojectcommandfirstcreatesadirectorycalled

iFriends,andthenitstoresthebasicsetofPythonfilesthat

areneededtobegintheprojectintheiFriendsdirectory.The

startprojectcommandcreatesthefollowingfiles:

__init__.pyisanemptyfilethattellsPythonthatthe

websitedirectoryshouldbetreatedasaPythonpackage.

manage.pyisthecommand-lineutilitythatallowsthe

administratortostartandmanagetheDjangoproject.

settings.pyistheconfigurationfilethatcontrolsthe

behavioroftheDjangoproject.

urls.pyisaPythonfilethatdefinesthesyntaxand

configuresthebehavioroftheURLsthatwillbeusedto

accessthewebsite.

ThebasicpurposeofthesefilesistosetupaPythonpackage

thatDjangocanusetodefinethewebsite'sstructureand

behavior.Wewilldiscussthesefilesabitmoreinthishourand

insubsequenthoursasthewebsitegetsincreasinglycomplex.



Hour2.CreatingYourFirstWebsite

WhatYou'llLearninThisHour

HowtobegincreatingaDjangoproject

Howtostartandstopthebuilt-inwebserver

ThestepstoconfigureDjangotoaccessthe

database

Howtocreateandinstallanapplication

Thestepstoapplyamodeltoanapplication

ThestepstoactivateamodelinDjango

HowtoconfigureDjangotoacceptspecificURL

requests

Howtocreateasimpleviewforawebbrowser

InHour1,"UnderstandingDjango,"youlearnedsomeofthe

basicsabouttheDjangoframework.Thishourguidesyou

throughthestepsofcreatingafunctionalwebsitecalled

iFriends.Althoughthiswebsitewillbebasic,itwillbethebasis

forfuturehourstobuildonasyouareguidedthroughthe

variousaspectsoftheDjangoframework.



CreatingaDjangoProject

Let'sbegintheprocessofcreatingaworkingwebsiteby

creatingaDjangoproject.ADjangoprojectisacollectionof

settingsthatdefineaspecificinstanceofDjango.Thesesettings

includethingssuchasdatabaseconfiguration,URL

configuration,andotheroptionsthatyouwilllearnaboutasthe

hourstickby.



TryItYourself:CreateYourFirstDjangoProject

CreatingaDjangoprojectisrelativelysimpletodofromthe

commandprompt.Inthissection,youcreateaprojectcalled

iFriends.

1. Fromacommandprompt,changetothedirectorywhere

youwanttostorethecodefortheiFriendsproject.

2. CreateadirectorycallediFriends.Thiswillbetheroot

directoryfortheiFriendsproject.

3. ChangetotheiFriendsdirectory.

4. TypethefollowingcommandtocreatetheiFriendsproject:



pythondjango-admin.pystartprojectiFriends



WatchOut!

BecausetheprojectwillactasaPythonpackage,avoid

usingaprojectnamethatconflictswithanyexisting

built-inPythonpackages.ThedocumentationforbuiltinPythonpackagescanbefoundat

http://www.python.org.



BytheWay

Thereisnoneedtoputyourprojectcodeinadirectory

inthewebserver'sdocumentbase.TheDjango

frameworkwillberesponsibleforexecutingthecode.

Infact,itisamuchbetterideatostorethecode



somewhereoutsidethewebserver'sroot.Thatway

yourcodewillbeprotectedfrombeingaccessed

directlyfromawebbrowser.



Thestartprojectcommandfirstcreatesadirectorycalled

iFriends,andthenitstoresthebasicsetofPythonfilesthat

areneededtobegintheprojectintheiFriendsdirectory.The

startprojectcommandcreatesthefollowingfiles:

__init__.pyisanemptyfilethattellsPythonthatthe

websitedirectoryshouldbetreatedasaPythonpackage.

manage.pyisthecommand-lineutilitythatallowsthe

administratortostartandmanagetheDjangoproject.

settings.pyistheconfigurationfilethatcontrolsthe

behavioroftheDjangoproject.

urls.pyisaPythonfilethatdefinesthesyntaxand

configuresthebehavioroftheURLsthatwillbeusedto

accessthewebsite.

ThebasicpurposeofthesefilesistosetupaPythonpackage

thatDjangocanusetodefinethewebsite'sstructureand

behavior.Wewilldiscussthesefilesabitmoreinthishourand

insubsequenthoursasthewebsitegetsincreasinglycomplex.



StartingtheDevelopmentServer

AfteryouhavecreatedtheDjangoproject,youshouldbeable

tostartthedevelopmentservertotestit.Thedevelopment

serverisalightweightwebserverthatisincludedwiththe

Djangoproject.Itletsyoudevelopandtestyourwebsite

withouthavingtodealwithalltheconfigurationand

managementissuesofaproductionwebserver.



TryItYourself:StarttheDevelopmentServer

Inthissection,youlearnhowtostartthedevelopment

server.

1. Fromacommandprompt,changetotheroot

directoryfortheiFriendsproject.

2. Enterthefollowingcommandtostartthe

developmentserver,asshowninFigure2.1:



pythonmanage.pyrunserver



Figure2.1.Startingthedevelopment

serverfromacommandline.

[Viewfullsizeimage]



BytheWay

Themanage.pyutilityiscopiedintotherootofyour

projectbythecreateprojectcommanddiscussed



earlierinthishour.Themanage.pyutilityfirstvalidates

theprojectandreportsanyerrors.Ifnocriticalerrors

areencountered,youarenotifiedthatthedevelopment

severisrunningathttp://127.0.0.1:8000/.

3.

Verifythatthedevelopmentserverisworkingproperlyby

openingawebbrowserandenteringthefollowingaddress:

http://127.0.0.1:8000/

Ifthedevelopmentserverstartsproperly(andyouhaven't

changedthedebugsetting),youshouldseeapagesimilarto

theoneshowninFigure2.2.

Figure2.2.InitialbrowserviewofaDjangowebsite.

[Viewfullsizeimage]



BytheWay

Youcantellthedevelopmentservertouseadifferent

portthan8000ifthatportisalreadybeingusedby

addingtheporttothecommandline.Thefollowing

exampleshowsthesyntaxforconfiguringthe

developmentservertorunonport8008:



manage.pyrunserver8008



DidyouKnow?

Tostopthedevelopmentserver,pressCtrl+Breakor

Ctrl+C.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hour 2. Creating Your First Website

Tải bản đầy đủ ngay(0 tr)

×