手把手教你学python-django开发入门略讲

By | 2014/05/18

版权所有,本文地址:http://www.androiddev.net/django-freshman/
操作系统:Ubuntu 12.04 stl
python环境:2.7.5
django版本:1.6.5

(一)、
打开终端,运行如下命令:

cd 桌面
django-admin.py startproject hiweb
cd hiweb
django-admin.py startapp hi

然后我们开始编辑一下这个项目,运行如下命令

vim settings.py

在TIME_ZONE=’America/Chicago’改为TIME_ZONE=’Asia/Shanghai’
将LANGUAGE_CODE=’en-us’改为LANGUAGE_CODE=’en-us’
然后在INSTALLED_APPS元组中添加’hi’
此时,运行如下命令:

cd hi
vim views.py

views.py的代码如下:

from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.templates import Context,loader

def frontpage(req):
    return HttpResponse("<h1>Hello World</h1>")

此时我们在执行如下命令,对urls.py进行编辑:

cd ..
vim 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'^$', 'FirstDjango.views.home', name='home'),
    # url(r'^FirstDjango/', include('FirstDjango.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)),
    url(r'^hi/frontpage/$','hi.view.frontpage')
)

此时访问http://localhost:8000/hi/frontpage就会出现一个 Hello World
那么,我们不能将所有的html代码写到python文件里,所以我们可以使用模板来处理,执行如下代码:

mkdir templates
touch templates/firstpage.html
vim templates/firstpage.html

输入如下代码:

<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>Hello world this is {{ user.say }} made in China</h1>
    <li>{{ user.age }}</li>
    <li>{{ user.gender }}</li>

    <li>{{ book_list.0 }}</li>
    <li>{{ book_list.1 }}</li>
    <li>{{ book_list.2 }}</li>
    <li>{{ book_list.3 }}</li>
    {% for book in book_list %}
        {{ forloop.revcounter }}<li>书本:{{ book }}</li>
    {% endfor %}
</body>
</html>

此时,我们再编辑views.py,代码如下:

# Create your views here.
from django.http import HttpResponse
from django.template import loader,Context
from django.shortcuts import render_to_response
from django import forms

class Person(object):
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender =gender

    def say(self):
        return "I'm"+self.name

class UserForm(forms.Form):
    name = forms.CharField()

'''
def index(req):X
    t = loader.get_template('index.html')
    c = Context({})
    return HttpResponse(t.render(c))
'''

def index(req):
    # user = {'name':'Karl','age':'23','gender':'male'}
    user = Person('Carl',20,'male')
    book_list = ['python','java','php','web'];
    return render_to_response('index.html',{'title':'my page','user':user,'book_list':book_list})

def register(req):
    if req.method == 'POST':
        form = UserForm(req.POST)
        if form.is_valid():
            print form.cleaned_data
            return HttpResponse('ok')
    else:
        form = UserForm()
    return render_to_response('register.html',{'form':form})

然后再来编辑一下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'^$', 'FirstDjango.views.home', name='home'),
    # url(r'^FirstDjango/', include('FirstDjango.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)),

    url(r'^FirstDjangoApp/index/$','FirstDjangoApp.views.index'),

    url(r'^FirstDjangoApp/register/$','FirstDjangoApp.views.register'),

    url(r'^FirstDjangoApp/book/$','FirstDjangoApp.views.book')
)

此时,django中的几种模板传值的方法就解决了,接下来我们在看一个简单的form传值:
新建register.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post">
        {{ form }}
        <input type="submit" value="ok"/>
    </form>
</body>
</html>

这时访问register就可以看看form表单啦。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.