您的位置:首页 > 移动开发

Django~NewProject and APP

2016-02-25 14:07 344 查看
New Project

1.新建 django-admin startproject mysite

2.运行 manage.py runserver 8080

New APP

1.manage.py startapp polls

2.write view.py: def index 等等





3.create urls.py: urlpatterns=[ url() ]





4.url链接到project中:修改添加mysite/urls.py,

url(r'^polls/', include('polls.urls')),





5.安装数据库 manage.py migrate

6.creating models





7.Activating models 在mysite/settings.py 中添加一行





8创建编译 manage.py makemigrationgs polls





修改编译manage.py migrate





9运作,输入数据

manage.py shell

import django

django.setup()

-------------

from polls.models import Question,Choice

Question.objects.all()

from django.utils import timezone

q = Question(question_text="What's new?", pub_date=timezone.now())





More Vies

1.vies.py 新增 def detail results vote

2.polls/urls.py新增url

添加模板Template

1.在APP的文件下新建template/app/index.html

2.编辑html文件

3.更新polls/views.py

New 2 APPS

1.manage.py startapp polls2,在project中添加注册

2.copy polls 的templates,新建文件夹修改app name

3.copy修改models,

4.修改urls.

5.修改views

主要所有polls修改为polls2

urls.py中的app_name

views.py中的好多

还有xxx.html中的

Make the poll app modifiable in the admin¶ polls2/admim.py 中添加model

from django.contrib import admin

from .models import Question

admin.site.register(Question)


ListView and DetailView

We’re using two generic views here:
ListView
and
DetailView
. Respectively, those two views abstract the concepts of “display a list of objects” and “display a detail page for a particular type of object.”
通用视图

除了object_list外,Django还提供了许多通用视图函数,分布在几个模块中:
django.views.generic.list_detail模块

object_list 显示模型对象列表 object_detail 显示单个模型对象

django.views.generic.create_update模块

create_object 创建模型对象
update_object 修改模型对象
delete_object 删除模型对象

django.views.generic.simple模块

direct_to_template 直接使用指定的模板渲染给定的context对象
redirect_to 重定向到指定的url

django.views.generic.date_based模块
这个模块主要处理“按时间查看存档”的功能,来源于新闻出版行业。具体包括:

archive_index 最顶级的归档,列出所有年份及指定数量的最新对象
archive_year 按年归档,列出所有拥有对象的月份
archive_month 按月归档,列出本月的所有对象,找到拥有对象的上一个、下一个月份
archive_week 按周归档,列出本周的所有对象
archive_day 按日归档,列出当天的所有对象,找到拥有对象的上一个、下一个日期
archive_today 当前日期(今天)的按日归档
object_detail 显示按照年/月/日/序号找到的对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: