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、远程代码执行;
新闻热点
疑难解答