python_慕课\django入门与实践\6-1 Models.py





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













































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值