ORM 對象映射關(guān)系
使用model操作數(shù)據(jù)庫。django提供豐富的、優(yōu)雅的操作數(shù)據(jù)模型的API,同時你也可以使用原生sql操作數(shù)據(jù)庫。
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
一個高質(zhì)量的網(wǎng)站應(yīng)該有一個干凈整潔的URL體系。django支持完美的URL設(shè)計,并且請求url不用像php、asp那樣帶后綴。為了在應(yīng)用中使用URLs,你需要創(chuàng)建一個URLconf模塊。常規(guī)做法是在你的app中,使用map的結(jié)構(gòu)保存url對應(yīng)跳轉(zhuǎn)的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設(shè)計的強(qiáng)大而容易。它可以讓那些使用HTML、設(shè)計師或者前端開發(fā)工程師快速的上手。另外,它的擴(kuò)展性非常強(qiáng)大,允許開發(fā)人員根據(jù)自己的需要自定義模板。
<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框架中提供強(qiáng)大的表單庫。該庫可以將表單渲染成html、驗(yàn)證表單提交的數(shù)據(jù)、將數(shù)據(jù)轉(zhuǎn)化成python類型。django也提供了根據(jù)modle來生成表單的方法,并且同步的創(chuàng)建和更新數(shù)據(jù)。
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)認(rèn)證模塊
django提供了完整的、安全的認(rèn)證系統(tǒng)。它管理用戶賬號、用戶組、權(quán)限、cookie、session,以便你可以快捷的創(chuàng)建賬號和安全的登陸、登出。
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支持將文本翻譯成不同的語言,包括時間、數(shù)字、時間區(qū)域。它讓開發(fā)人員選擇哪些文本需要翻譯成哪些語言,根據(jù)需求來編寫應(yīng)用。
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提供多種防護(hù)措施。
1、點(diǎn)擊劫持;
2、xss攻擊;
3、跨域訪問;
4、SQL注入;
5、遠(yuǎn)程代碼執(zhí)行;
新聞熱點(diǎn)
疑難解答