Django入門css
學習了第一章《跟老齊學Python Django實戰》 齊偉 編著html
1,建立項目 django-admin startproject mysite 或者PyCharm新建DJango項目 2,與項目交互 python manage.py 回車,能夠看到交互的內容 python manage.py startserver # 啓動程序 修改端口 python manage.py startserver 9999 3,建立應用 python manage.py startapp blog 或者django-admin startapp blog 4,修改settings.py 添加應用 'blog', 修改編碼 zh-hans 5,添加blog/models.py中的內容 from django.db import models from django.utils import timezone from django.contrib.auth.models import User class BlogArticles(models.Model): title = models.CharField(max_length=300) author = models.ForeignKey(User, related_name="blog_posts") body = models.TextField() publish = models.DateTimeField(default=timezone.now) class Meta: ordering = ("-publish",) def __str__(self): return self.title 須要pip install pytz 來使用timezone 6,根據模型進行數據庫遷移 python manage.py makemigrations # 數據庫遷移準備 python manage.py sqlmigrate blog 0001 # 查看sql語句 python manage.py migrate # 進行遷移 7,建立超級用戶 python manage.py createsuperuser 進行啓動測試; 8,修改 blog/admin.py from .models import BlogArticles admin.site.register(BlogArticles) 進行啓動測試; 9,修改 blog/admin.py增長效果 from .models import BlogArticles class BlogArticlesAdmin(admin.ModelAdmin): list_display = ("title", "author", "publish") list_filter = ("publish","author") search_fields = ("title","body") raw_id_fields = ("author",) date_hierarchy = "publish" ordering = ['publish', 'author'] admin.site.register(BlogArticles, BlogArticlesAdmin) 10,修改 blog/views.py,增長視圖函數 from .models import BlogArticles def blog_title(request): blogs = BlogArticles.objects.all() return render(request, "blog/titles.html", {"blogs":blogs}) 11,編寫模板頁面,注意文件須要存儲爲utf-8格式; templates/base.html <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <meta charset="utf-8"> <meta name="viewport" content="width=device-with, initial-scale=1"> <title>{%block title%}{%endblock%}</title> <link rel="stylesheet" href="http://necolas.github.io/normalize.css/"> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <div class="container"> {%block content%} {%endblock%} </div> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </body> </html> templates/blog/index.html # 必須加個blog文件夾,不然不一樣app下面的index.html頁面會重複 {% extends "base.html" %} {% block title %}blog titles{% endblock %} {% block content %} <div class="row text-center vertical-middle-sm"> <h1>個人博客</h1> </div> <div class="row"> <div class="col-xs-12 col-md-8"> <ul> {% for blog in blogs %} <li>{{blog.title}}</li> {% endfor %} </ul> </div> <div class="col-xs-6 col-md-4"> <h2>廣告</h2> <p>跟老齊學:www.itdiffer.com</p> <img width="200px" src="http://www.mrwallpaper.com/wallpapers/Fluffy-Cat.jpg"> </div> </div> {% endblock %} 12,修改urls.py,引入blog.urls from django.conf.urls import url, include url(r'^blog/',include('blog.urls', namespace='blog', app_name='blog')), 13,修改titles.html,增長文件詳情標籤 <li><a href="{{blog.id}}">{{blog.title}}</a></li> 14,增長詳情模板blog/content.html {% extends "base.html" %} {% block title %}blog article{% endblock %} {% block content %} <div class="row text-center vertical-middle-sm"> <h1>{{article.title}}</h1> </div> <div class="row"> <div class="col-xs-12 col-md-8"> <p class="text-center"><span>{{article.author.username}}</span><span style="margin-left:20px">{{publish}}</span></p> <div>{{article.body}}</div> </div> <div class="col-xs-6 col-md-4"> <h2>廣告</h2> <p>跟老齊學:www.itdiffer.com</p> <img width="200px" src="http://www.mrwallpaper.com/wallpapers/Fluffy-Cat.jpg"> </div> </div> {% endblock %} 15,修改urls.py,引入content.html url(r'(?P<article_id>\d)/$', views.blog_article, name="blog_detail") 16,在views.py中引入get_object_or_404 from django.shortcuts import render, get_object_or_404 修改獲取對象的方法: article = BlogArticles.objects.get(id=article_id) 修改成 article = get_object_or_404(BlogArticles, id=article_id)