标签:
$django-admin startproject mysite 创建一个django新工程
$python manage.py runserver 开启该服务器
$python manage.py startapp polls 在该工程中创建一个名为polls的新应用
#########
model: 用于描述应用的数据库结构和信息。一般用类的形式定义,如下例所示(models.py):
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")
结论: 类名首字母大写,等号两侧各空一个, 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)
标签:
原文地址:http://www.cnblogs.com/cbyzju/p/5478610.html