python_慕课\django入门与实践\6-1 Models.py
2017-11-27 11:39
429 查看
-- 6-1 Models https://www.imooc.com/video/13966
-- D:\project_java\myblog\blog\models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Article(models.Model):
title=models.CharField(max_length=32,default='title')
content=models.TextField(null=True)
# python 2.7 用 __unicode__(self) ,否则用 :__str__(self)
def __unicode__(self):
return self.title
--
django 官方网站查看创建字段 :https://docs.djangoproject.com/en/1.10/ref/models/fields
python manage.py makemigrations blog
python manage.py migrate blog
查看sql : python manage.py sqlmigrate blog 0001 下载 SQLite Expert Personal 打开 db.sqlite3 http://www.sqliteexpert.com/download.html
-- D:\project_py\myblog\blog\Templates\1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
blog...
<h1>{{ article.title }}</h1>
<h1>{{ article.content }}</h1>
</body>
</html>
-- D:\project_py\myblog\blog\views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from . import models
# Create your views here.
# http://127.0.0.1:8000/blog/index/
def index(request):
article=models.Article.objects.get(pk=1)
return render(request,'1.html',{'article':article})
-- 7-1 Admin https://www.imooc.com/video/13967
python manage.py createsuperuser : 创建超级用户 http://localhost:8000/admin/ Admin入口
-- D:\project_java\myblog\myblog\settings.py
LANGUAGE_CODE = 'en-us' 改为 : zh_Hans
-- D:\project_java\myblog\blog\admin.py
# http://localhost:8000/admin/
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from . import models
admin.site.register(models.Article)
-- 8-1 博客主页面开发 https://www.imooc.com/video/13968
-- D:\project_py\myblog\blog\Templates\1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for article in articles %}
<a href="">{{ article.title }}</a>
<br/>
{% endfor %}
</body>
</html>
-- D:\project_java\myblog\blog\views.py
# http://127.0.0.1:8000/blog/index/
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from . import models
# Create your views here.
def index(request):
articles=models.Article.objects.all()
return render(request,'1.html',{'articles':articles})
-- 8-2 博客文章页面开发 https://www.imooc.com/video/13969
-- D:\project_java\myblog\blog\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index/$', views.index), # url(r'^$', views.index), url(r'^index/$', views.index),
url(r'^article/(?P<article_id>[0-9]+)/$', views.article_page),
]
-- D:\project_java\myblog\blog\views.py
# -*- coding: utf-8 -*- http://127.0.0.1:8000/blog/article/1
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from . import models
# Create your views here.
def index(request):
article = models.Article.objects.all()
return render(request,'1.html',{'article':article})
def article_page(request,article_id):
article=models.Article.objects.get(pk=article_id)
return render(request,'2.html',{'article':article})
-- D:\project_java\myblog\blog\Templates\2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<br/>
<h3>{{ article.content }}</h3>
<br/><br/>
<a href=""> 修改文正</a>
</body>
</html>
-- D:\project_java\myblog\blog\models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Article(models.Model):
title=models.CharField(max_length=32,default='title')
content=models.TextField(null=True)
# python 2.7 用 __unicode__(self) ,否则用 :__str__(self)
def __unicode__(self):
return self.title
--
django 官方网站查看创建字段 :https://docs.djangoproject.com/en/1.10/ref/models/fields
python manage.py makemigrations blog
python manage.py migrate blog
查看sql : python manage.py sqlmigrate blog 0001 下载 SQLite Expert Personal 打开 db.sqlite3 http://www.sqliteexpert.com/download.html
-- D:\project_py\myblog\blog\Templates\1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
blog...
<h1>{{ article.title }}</h1>
<h1>{{ article.content }}</h1>
</body>
</html>
-- D:\project_py\myblog\blog\views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from . import models
# Create your views here.
# http://127.0.0.1:8000/blog/index/
def index(request):
article=models.Article.objects.get(pk=1)
return render(request,'1.html',{'article':article})
-- 7-1 Admin https://www.imooc.com/video/13967
python manage.py createsuperuser : 创建超级用户 http://localhost:8000/admin/ Admin入口
-- D:\project_java\myblog\myblog\settings.py
LANGUAGE_CODE = 'en-us' 改为 : zh_Hans
-- D:\project_java\myblog\blog\admin.py
# http://localhost:8000/admin/
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from . import models
admin.site.register(models.Article)
-- 8-1 博客主页面开发 https://www.imooc.com/video/13968
-- D:\project_py\myblog\blog\Templates\1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for article in articles %}
<a href="">{{ article.title }}</a>
<br/>
{% endfor %}
</body>
</html>
-- D:\project_java\myblog\blog\views.py
# http://127.0.0.1:8000/blog/index/
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from . import models
# Create your views here.
def index(request):
articles=models.Article.objects.all()
return render(request,'1.html',{'articles':articles})
-- 8-2 博客文章页面开发 https://www.imooc.com/video/13969
-- D:\project_java\myblog\blog\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index/$', views.index), # url(r'^$', views.index), url(r'^index/$', views.index),
url(r'^article/(?P<article_id>[0-9]+)/$', views.article_page),
]
-- D:\project_java\myblog\blog\views.py
# -*- coding: utf-8 -*- http://127.0.0.1:8000/blog/article/1
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render
from . import models
# Create your views here.
def index(request):
article = models.Article.objects.all()
return render(request,'1.html',{'article':article})
def article_page(request,article_id):
article=models.Article.objects.get(pk=article_id)
return render(request,'2.html',{'article':article})
-- D:\project_java\myblog\blog\Templates\2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<br/>
<h3>{{ article.content }}</h3>
<br/><br/>
<a href=""> 修改文正</a>
</body>
</html>
相关文章推荐
- python_慕课\django入门与实践\2-1 课前准备.py
- Python Web入门:Django学习与实践之三(models)
- Python Web入门:Django学习与实践一
- python-Django models.py新建的表如何同步到数据路(八)
- python编程从入门到实践18章 Django入门
- Python的Django框架完成一个完整的论坛(3.创建app的models.py、views.py源码)
- Python web入门:Django学习与实践二(简单页面实现和创建一个模板)
- Python入门教程(一)manage.py和django-admin.py的区别
- 基于SAE的Python+Django的入门实践
- Python 从入门到实践 8-3 课后习题
- [置顶] python爬虫实践——零基础快速入门(一)
- 《python数据挖掘入门与实践》“电影推荐” 笔记3
- [置顶] 【python Django】django入门教程1
- python django快速入门教程
- 学习笔记_python_python编程从入门到实践2.0
- python web py入门-1-web.py简介和安装
- Python Django基础教程(一)(入门)
- Python学习 第一天任务 (四:【基于Python编程从入门到实践】动手试一试)
- Python编程 从入门到实践 第四章 作业参考答案 (部分)
- Python3 初学实践案例(7)tkinter 入门 GUI 的密码生成程序