leve1031 发表于 2011-12-21 08:43

Django Hello World教程.

<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">开始我们的项目 我们的第一步就是建立一个项目<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">django-admin.py startproject myproject1<br></span></span><div id="codeText" class="codeText"><ol style="margin:0 1px 0 0;padding:5px 0;" start="1" class="dp-css"><li><span style="color:#000000;">django<span style="color:#0000CC;">-</span>admin startproject myproject1</span></li></ol></div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">myproject1就是你的项目名称,django为我们建立了项目文件夹,以及必备的一些配置文件<br><br></span></span><div id="codeText" class="codeText"><ol style="margin:0 1px 0 0;padding:5px 0;" start="1" class="dp-css"><li><span style="color:#000000;"><span style="color:#FF0000;">__init__</span><span style="color:#0000CC;">.</span>pycsettings<span style="color:#0000CC;">.</span>py   urls<span style="color:#0000CC;">.</span>py<br></span></li><li>
<span style="color:#FF0000;">__init__</span><span style="color:#0000CC;">.</span>pymanage<span style="color:#0000CC;">.</span>py   settings<span style="color:#0000CC;">.</span>pycurls<span style="color:#0000CC;">.</span>pyc</li></ol></div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">开始我们的应用 进入你的项目文件夹,然后输入django-admin.py startapp app1<br></span></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">django为我们建立了应用所需的models.py和views.py<br></span></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">编辑settings.py,</span></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="font-family: Arial, sans-serif; font-size: 14px; text-align: left; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">加入我们的项目 在INSTALLED_APPS区域加入我们的项目<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">'myproject1.app1'<span class="Apple-converted-space"> <br></span></span></span><div id="codeText" class="codeText"><ol style="margin:0 1px 0 0;padding:5px 0;" start="1" class="dp-css"><li><span style="color:#000000;">INSTALLED_APPS <span style="color:#0000CC;">=</span> <span style="color:#0000CC;">(</span><br></span></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF00FF;">'django.contrib.auth'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF00FF;">'django.contrib.contenttypes'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF00FF;">'django.contrib.sessions'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF00FF;">'django.contrib.sites'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF00FF;">'django.contrib.messages'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# Uncomment the next line to enable the admin<span style="color:#0000CC;">:</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# <span style="color:#FF00FF;">'django.contrib.admin'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# Uncomment the next line to enable admin documentation<span style="color:#0000CC;">:</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# <span style="color:#FF00FF;">'django.contrib.admindocs'</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF00FF;">'myproject1.app1'</span><span style="color:#0000CC;">,</span><br></li><li>
<span style="color:#0000CC;">)</span></li></ol></div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">配置urls.py,</span></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">加入我们的url规则 当用户输入/hello时,访问myproject1.app1.views中的hello方法<br></span></span><div id="codeText" class="codeText"><ol style="margin:0 1px 0 0;padding:5px 0;" start="1" class="dp-css"><li><span style="color:#000000;">urlpatterns <span style="color:#0000CC;">=</span> patterns<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">''</span><span style="color:#0000CC;">,</span><br></span></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# Example<span style="color:#0000CC;">:</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000CC;">(</span>r<span style="color:#FF00FF;">'^hello/'</span><span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">'myproject.app1.views.hello'</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">,</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></li><li>
<br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# Uncomment the admin<span style="color:#0000CC;">/</span>doc line below to enable admin documentation<span style="color:#0000CC;">:</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#<span style="color:#0000CC;">(</span>r<span style="color:#FF00FF;">'^admin/doc/'</span><span style="color:#0000CC;">,</span> include<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'django.contrib.admindocs.urls'</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">,</span><br></li><li>
<br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;# Uncomment the next line to enable the admin<span style="color:#0000CC;">:</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#<span style="color:#0000CC;">(</span>r<span style="color:#FF00FF;">'^admin/'</span><span style="color:#0000CC;">,</span> include<span style="color:#0000CC;">(</span>admin<span style="color:#0000CC;">.</span>site<span style="color:#0000CC;">.</span>urls<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">,</span><br></li><li>
<span style="color:#0000CC;">)</span></li></ol></div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">编辑views.py, </span></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span class="Apple-style-span" style="font-family: Arial,sans-serif; font-size: 14px; text-align: left;">编写hello方法<br></span></span><div id="codeText" class="codeText"><ol style="margin:0 1px 0 0;padding:5px 0;" start="1" class="dp-css"><li><span style="color:#000000;"><span style="color:#0000FF;">from</span> django<span style="color:#0000CC;">.</span>http <span style="color:#0000FF;">import</span> HttpResponse<br></span></li><li>
<span style="color:#0000FF;">from</span> django<span style="color:#0000CC;">.</span>shortcuts <span style="color:#0000FF;">import</span> render_to_response<br></li><li>
<br></li><li>
<span style="color:#0000FF;">def</span> hello<span style="color:#0000CC;">(</span>request<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br></li><li>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> HttpResponse<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"&lt;b&gt;HW&lt;/b&gt;"</span><span style="color:#0000CC;">)</span></li></ol></div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="font-family: Arial, sans-serif; font-size: 14px; text-align: left; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">启动django python manage.py runserver</span></span>
页: [1]
查看完整版本: Django Hello World教程.