Django剖析

 

$django-admin startproject mysite  建立一個django新工程css

$python manage.py runserver 開啓該服務器html

$python manage.py startapp polls 在該工程中建立一個名爲polls的新應用python

#########數據庫

model: 用於描述應用的數據庫結構和信息。通常用類的形式定義,以下例所示(models.py):express

class Band(models.Model):django

    ###A model of a rock band.###服務器

    name = models.CharField(max_length=200)app

    can_rock = models.BooleanField(default=True)ide

class Member(models.Model):ui

    ###A model of a rock band member###

    name = models.CharField("Member's name", max_length=200)

    instrument = models.CharField(choices=(('g', "Guitar"), ('b', "Bass"), ('d', "Drums"),), max_length=1)

    band = models.ForeignKey("Band")

結論: 類名首字母大寫,等號兩側各空一個, models經常使用的屬性:CharField, BooleanField, ForeignKey, 單詞首字母均大寫。

 

#########

view: 當django服務器接收到URL請求時,以view(視圖)的形式反饋給client。

1. To get from a URL to a view, Django uses what are known as 'URLconfs'. A URLconf maps URL patterns (described as regular expressions) to views.

URLconf將接收到的URL信息映射到對應的view視圖。

(urls.py)

####解析接收到的URL請求,映射到相應的view頁面。

from django.conf.urls import url

from . import views

urlpatterns = [

    url(r'^bands/$', views.band_listing, name='band-list'),

    url(r'^bands/(\d+)/$', views.band_detail, name='band-detail'),

    url(r'^bands/search/$', views.band_search, name='band-search'),

]

(views.py)

###響應給用戶的視圖###

from django.shortcuts import render

def band_listing(request):

    ### A view of all bands.###

    bands = models.Band.objects.all()

    return render(request, 'bands/band_listing.html', {'bands': bands})

 

 

########

template: 相似於css文件,將視圖設計與python分離開。

let's use Django's template system to seprate the design from Python by creating

a template that the view can use.

在application的目錄下建立templates目錄,Django會在這個目錄中尋找模板。

<html>

  <head>

      <title>Band Listing</title>

  </head>

  <body>

      <h1>All bands</h1>

      <ul>

      {% for band in bands %}

      <li>

           <h2><a href="{{ band.get_absolute_url }}"> {{ band.name }} </a></h2>

            {% if band.can_rock %} <p> This band can rock!</p>{% endif %}

      </li>

      {% endfor %}

      </ul>

  </body>

</html>

your project's templates setting describes hw Django will load and render templates.

Within the templates directory you have just created, create another directory called polls, and within that create a file calledindex.html. In other words, your template should be at polls/templates/polls/index.html. Because of how theapp_directories template loader works as described above, you can refer to this template within Django simply aspolls/index.html.

模板的命名空間:Now we might be able to get away with putting our templates directly in polls/templates (rather than creating anotherpolls subdirectory), but it would actually be a bad idea. Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those templates inside another directory named for the application itself.

 

##########

form: 表格。Django provides a powerful form library that handles rendering forms as HTML, validating user-submitted data, and converting that data to native Python types. Django also provides a way to generate forms from your existing models and use those forms to create and update data. (Django提供了強大的表庫,以HTML的格式渲染表格,驗證用戶提交的數據,並將其轉變爲python類型。Django也提供了將已存在的models生成表格的方法,並使用這些表格來創造和更新數據)。

示例:

from django import forms

class BandContactForm(forms.Form):

    subject = forms.CharField(max_length=100)

    message = forms.CharField()

    sender = forms.EmailField()

    cc_myself = forms.BooleanField(required =False)

相關文章
相關標籤/搜索