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 urlfrom . import viewsurlpatterns = [ 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 renderdef 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 formsclass 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_requiredfrom django.shortcuts import render@login_requireddef 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 adminfrom bands.models import Band, Memberclass 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 optionsadmin.site.register(Member, MemberAdmin) # Use the customized options國際化
django支持將文本翻譯成不同的語言,包括時間、數字、時間區域。它讓開發人員選擇哪些文本需要翻譯成哪些語言,根據需求來編寫應用。
from django.shortcuts import renderfrom django.utils.translation import ugettextdef 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、遠程代碼執行;
新聞熱點
疑難解答