ORM 對象映射關係
使用model操作數據庫。django提供豐富的、優雅的操作數據模型的API,同時你也可以使用原生sql操作數據庫。
class Band(models.Model):
"""A model of a rock band."""
name = models.CharField(max_length=200)
can_rock = models.BooleanField(default=True)
class Member(models.Model):
"""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")
URLs and views
一個高質量的網站應該有一個乾淨整潔的URL體系。django支持完美的URL設計,並且請求url不用像php、asp那樣帶後綴。爲了在應用中使用URLs,你需要創建一個URLconf模塊。常規做法是在你的app中,使用map的結構保存url對應跳轉的views。
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'),
]
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})
模板
django將template設計的強大而容易。它可以讓那些使用HTML、設計師或者前端開發工程師快速的上手。另外,它的擴展性非常強大,允許開發人員根據自己的需要自定義模板。
<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>
表單
django框架中提供強大的表單庫。該庫可以將表單渲染成html、驗證表單提交的數據、將數據轉化成python類型。django也提供了根據modle來生成表單的方法,並且同步的創建和更新數據。
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)
認證模塊
django提供了完整的、安全的認證系統。它管理用戶賬號、用戶組、權限、cookie、session,以便你可以快捷的創建賬號和安全的登陸、登出。
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required
def my_protected_view(request):
"""A view that can only be accessed by logged-in users"""
return render(request, 'protected.html', {'current_user': request.user})
管理員模塊
django提供了自動管理模塊,該模塊可以方便的操作model。
from django.contrib import admin
from bands.models import Band, Member
class MemberAdmin(admin.ModelAdmin):
"""Customize the look of the auto-generated admin for the Member model"""
list_display = ('name', 'instrument')
list_filter = ('band',)
admin.site.register(Band) # Use the default options
admin.site.register(Member, MemberAdmin) # Use the customized options
國際化
django支持將文本翻譯成不同的語言,包括時間、數字、時間區域。它讓開發人員選擇哪些文本需要翻譯成哪些語言,根據需求來編寫應用。
from django.shortcuts import render
from django.utils.translation import ugettext
def homepage(request):
"""
Shows the homepage with a welcome message that is translated in the
user's language.
"""
message = ugettext('Welcome to our site!')
return render(request, 'homepage.html', {'message': message})
{% load i18n %}
<html>
<head>
<title>{% trans 'Homepage - Hall of Fame' %}</title>
</head>
<body>
{# Translated in the view: #}
<h1>{{ message }}</h1>
<p>
{% blocktrans count member_count=bands.count %}
Here is the only band in the hall of fame:
{% plural %}
Here are all the {{ member_count }} bands in the hall of fame:
{% endblocktrans %}
</p>
<ul>
{% for band in bands %}
<li>
<h2><a href="{{ band.get_absolute_url }}">{{ band.name }}</a></h2>
{% if band.can_rock %}<p>{% trans 'This band can rock!' %}</p>{% endif %}
</li>
{% endfor %}
</ul>
</body>
</html>
安全
django提供多種防護措施。
1、點擊劫持;
2、xss攻擊;
3、跨域訪問;
4、SQL注入;
5、遠程代碼執行;