您的位置:首页 > 编程语言 > Python开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: