首页 技术 正文
技术 2022年11月6日
0 收藏 312 点赞 949 浏览 3214 个字

$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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860