Django uses the template page, block label, model

1.Django use template page

For Django page to the system proposed way template inheritance and templates loaded.

1. Import static pages

2. Import static files (css, js, images)

3. Modify page among the static address

Modify the configuration in 1.setting

Create a template page (public page) 2.templates under different sections into label blocks

{% block blockname %}

{% endblock %}

base.html file as follows

<!doctype html>
<html>
<head>
<meta charset="utf8">
<title>{% block title %}木子先生{% endblock %}</title>
<meta name="keywords" content="木子先生" />
<meta name="description" content="木子先生" />
<link href="/static/css/base.css" rel="stylesheet">
<link href="/static/css/main.css" rel="stylesheet">
 {% block style %}
 {% endblock %}
<!--[if lt IE 9]>
<script src="/static/js/modernizr.js"></script>
<![endif]-->
<script type="text/javascript" src="/static/js/jquery.js"></script>
</head>
<body>
<div id="wrapper">
<header>
  <div class="headtop"></div>
  <div class="contenttop">
    <div class="logo f_l">木子先生</div>
    <div class="search f_r">
      <form action="/e/search/index.php" method="post" name="searchform" id="searchform">
        <input name="keyboard" id="keyboard" class="input_text" value="请输入关键字" style="color: rgb(153, 153, 153);" onfocus="if(value=='请输入关键字'){this.style.color='#000';value=''}" onblur="if(value==''){this.style.color='#999';value='请输入关键字'}" type="text">
        <input name="show" value="title" type="hidden">
        <input name="tempid" value="1" type="hidden">
        <input name="tbname" value="news" type="hidden">
        <input name="Submit" class="input_submit" value="搜索" type="submit">
      </form>
    </div>
    <div class="blank"></div>
    <nav>
      <div  class="navigation">
        <ul class="menu">
          <li><a href="/index/">网站首页</a></li>
          <li><a href="/new/">关于我</a>
            <ul>
              <li><a href="about.html">个人简介</a></li>
              <li><a href="/listpic/">个人相册</a></li>
            </ul>
          </li>
          <li><a href="/newList/">我的日记</a>
            <ul>
              <li><a href="/newList/">个人日记</a></li>
              <li><a href="/newList/">学习笔记</a></li>
            </ul>
          </li>
          <li><a href="/newList/">技术文章</a> </li>
          <li><a href="#">给我留言</a> </li>
        </ul>
      </div>
    </nav>
    <SCRIPT type=text/javascript>
	// Navigation Menu
	$(function() {
		$(".menu ul").css({display: "none"}); // Opera Fix
		$(".menu li").hover(function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown("normal");
		},function(){
			$(this).find('ul:first').css({visibility: "hidden"});
		});
	});
</SCRIPT>
  </div>
</header>
<div class="container">
  <div class="con_content">
    {% block content %}

    {% endblock %}
  </div>
  <div class="blank"></div>
    <div class = "footer">
  <footer>
</ div>
  <-! Container Code End ->
      <div class = "f_L">
        <p> All Rights Reserved Copyright: <a href="http://www.yangqq.com"> Mr. Muzi personal blog </a> </ the p-> 
      </ div> 
      <div class = "F_r textr"> 
        <P> Design by DanceSmile </ P> 
      </ div> 
    </ div> 
  </ footer> 

</ body> 
</ HTML>

Create a home page index.html, only to inherit the template, and then write about their own unique content in the block label.

Content within the block as the default content, if after inheritance, do not use the block, part of the contents of the block is the default content, otherwise covered

index.html file as follows

The extends% { 'base.html'%} 
{%}% Block style 
<Link the href = "/ static / CSS / index.css" the rel = "this stylesheet"> 
{% endblock%} 
{%}% Block Content 
  <div class = "jztop"> </ div> 
  <div class = "Container"> 
    <div class = "bloglist f_L"> 
      <H3> <a href="/jstt/bj/2017-07-13/784.html"> [mentality] Please do not linger in the design of this road you </a> </ h3> 
      <Figure> <img src = "/ static / ImagesRF Royalty Free / img_1.jpg" alt = "[mentality] Do not design this road wandering friends "> </ Figure> 
      <ul> 
        <the p-> I compiled some friends gave me a letter, if you are still hesitant, take a look, in the end or not to stick to it! I also welcome to write to me, I hope to help more people. </ the p-> 
        <A title = "[mentality] Please do not linger in the design of this road it" href = "
      <p class = "dateview"> <span> 2017-07-13 </ span> <span> Author: </ span> <span> personal blog: [<a href="/jstt/bj/"> s Notebook </a>] </ span> </ the p-> 
      <h3> <a href="/jstt/bj/2015-01-09/740.html"> [those of] a hurry personal blog summarizes the experience of four years </a> ... </ h3> 
      <Figure> <img src = "/ static / ImagesRF Royalty Free / img_2.jpg" alt = "[hurry] summarizes those years of personal experience of this blog four years ..."> </ Figure> 
      < ul> 
        <the p-> blog buy from the original domain name, to the on-line has been four years, and this four years, there is laughter too, have blamed, there is repentance, there is persistent before, also gave up ... but in the end He stuck with it, so hurry time, and so it was too late to go back and make up </ the p-> 
        <a title = "[hurry] summarizes those years of personal experience of this blog four years ..." href = "/ jstt / bj / 2015- 01-09 / 740.html "target =" _ blank "class =" readmore "> read more </a> >> 
      </ ul> 
      <p class = "dateview"> <span> 2015-01-09 </ span> <span> Author: Yang Qing </ span> <span> personal blog: [<a href="/jstt/bj/"> experience Notes </a>] </ span> </ the p-> 
      <h3> <a href="/jstt/bj/2014-11-06/732.html"> how to share my personal blog traffic from IP to do 10-600 of (Photo) </ h3> </a>
      <figure> <img src = " / static / images / img_3.jpg" alt = " share my personal blog how to do IP traffic from 10 to 600 (graphic)"> </ Figure> 
      <ul> 
        < p> my personal blog shows a total of three versions, the interface is also gone from "simple" to "complicated" to "simple" color from the "single color" to "colorful" to "local embellishment" process . The original version a year! And each revision is an opportunity to be Baidu punished! Do not frequent replacement interface! Good check code, there is no redundancy, there is no unreasonable structure. </ the p-> 
        <A title = "share my personal blog how to do IP traffic from 10 to 600 (graphic)" href = "/ jstt / bj / 2014-11-06 / 732.html" target = "_blank" class = "readmore" > read more </a> >> 
      </ ul> 
      <the p-class = "dateview"> <span> 2014-11-06 </ span> <span> author: Yang Qing </ span> <span> personal blog: [<a href="/jstt/bj/"> review notes </a>] </ span> </ P> 
      <H3> <A the href = "/ jstt / BJ /, 2014 -10-18 / 731.html "> 
        <A title = "Empire cms Methods commonly used tags call (forced collection oh)" href = "/ jstt / bj / 2014-10-18 / 731.html" target = "_ blank" class = "readmore "> read more </a> >> 
      </ ul> 
      <the p-class =" dateview "> <span> 2015-01-09 </ span> <span> author: Yang Qing </ span> <span> personal blog : [<a href="/jstt/bj/"> review notes </a>] </ span> </ P> 
      <H3> <A the href = "/ jstt / BJ / 2017-07-13 / 784. html "> [mentality] Please do not linger in the design of this road it </a> </ h3> 
      <Figure> <img src =" / static / ImagesRF Royalty Free / img_1.jpg "alt =" [mentality] Please do not in the design of this road wandering friends "> </ Figure> 
      <ul> 
        <p> I compiled some friends gave me a letter, if you are still hesitant, take a look, in the end or not to stick to it! I also welcome to write to me, I hope to help more people. </ the p-> 
        <A title = "[mentality] Please do not linger in the design of this road it" href = "/ jstt / bj / 2017-07-13 / 784.html" target = "_ blank" class = "readmore "> read more >> </a> 
      </ UL>
      <figure> <img src = " / static / images / img_2.jpg" alt = " [hurry] summarizes those years of personal experience of this blog four years ..."> </ Figure> 
      <ul> 
        <the p-> blog from the original domain name purchase, to the on-line has been four years, and this four years, there is laughter too, have blamed, there is repentance, there is persistent before, also gave up ... but eventually stuck with it, time is so hurried, etc. and then come back too late to make up </ the p-> 
        <a title = "[hurry] summarizes those years of personal experience of this blog four years ..." href = "/ jstt / bj / 2015-01-09 / 740.html" target = "_ blank" class = " readmore"> read more </a> >> 
      </ ul> 
      <the p-class = "dateview"> <span> 2015-01-09 </ span> <span> author: Yang Qing < / span> <span> personal blog: [<a href="/jstt/bj/"> experience notes </a>] </ span> </ the p-> 
    </ div> 
    <div class = "r_box F_r"> 
      <div class = "tit01">
        <h3 class="tit">关注{{ username }}</h3>
        <div class="gzwm">
          <ul>
            <li><a class="email" href="#" target="_blank">我的电话</a></li>
            <li><a class="qq" href="#mailto:[email protected]" target="_blank">我的邮箱</a></li>
            <li><a class="tel" href="#" target="_blank">我的QQ</a></li>
            <li><a class="prize" href="#">个人奖项</a></li>
          </ul>
        </div>
      </div>
      <!--tit01 end-->

      <div class="tuwen">
        <h3 class="tit">图文推荐</h3>
        <ul>
          <li><a href="/"><img src="/static/images/01.jpg"><b>住在手机里的朋友</b></a>
            <p> <span class = "tulanmu "> <a href="/"> mobile phone accessories </a> </ span> <span class = "tutime"> 2015-02-15 </ span> </ the p-> 
          </li>
          teach you how to use the arrears phone call </ b> </a>< li> <a href="/"> <img src =
            <p> <span class = " tulanmu"> <a href="/"> mobile phone accessories </a> </ span> <span class = "tutime"> 2015-02-15 </ span> </ p> 
          </ Li> 
          <Li> <a href="/" title="手机的16个惊人小秘密,据说99.999%的人都不知"> <img src = "/ static / images / 03.jpg"> < b> phone 16 amazing little secret, it is said ... </ b> </a> 
            <the p-> <span class = "tulanmu"> <a href="/"> mobile phone accessories </a> </ span > <span class = "tutime"> 2015-02-15 </ span> </ P> 
          </ Li> 
          <Li> <a href="/"> <IMG the src = "/ static / Images / 06.jpg "> <b> live in the mobile phone of a friend </ b> </a> 
            <the p-> <span class ="tulanmu "> <a href="/"> phone accessories </a> </ span> <span class =" tutime "> 2015-02-15 </ span> </ P> 
          </li>
          <Li> <A the href =" / "> <img src =" / static / images / 04.jpg "> <b> teach you how to use the arrears phone call </ b> </a>
            <p> <span class = " tulanmu"> <a href="/"> mobile phone accessories </a> </ span> <span class = "tutime"> 2015-02-15 </ span> </ p> 
          </ li> 
          <li> <a href="/"> <img src = "/ static / ImagesRF Royalty Free / 02.jpg"> <b> teach you how to use the arrears phone call </ b> </a> 
            <p> <span class = " tulanmu"> <a href="/"> mobile phone accessories </a> </ span> <span class = "tutime"> 2015-02-15 </ span> </ p> 
          </ Li> 
          <Li> <a href="/" title="手机的16个惊人小秘密,据说99.999%的人都不知"> <img src = "/ static / images / 03.jpg"> < b> phone 16 amazing little secret, it is said ... </ b> </a> 
            <the p-> <span class = "tulanmu"><a href="/">手机配件</a></span><span class="tutime">2015-02-15</span></p>
          </li>
        </ul> 
      </ div >
      <div class="ph">
        <h3 class="tit">点击排行</h3>
        <ul class="rank"> 
          <li> <A href = "/ jstt / BJ / 2017-07-13 / 784.html" title = "[mentality] Do not design it this way wander" target = "_blank"> [mentality] Please do not linger in the design of this road it </a> </ li> 
          <li> <A href = "/ News / S / 2016-05-20 / 751.html" title = "IP 2000+ to break the" target = "_ blank"> IP 2000+ to break the </a> </ Li> 
          <Li> <a the href = "/ jstt / Web / 2015-07-03 / 749.html "title =" Home cms empire, to customize how the page implement paging "target =" _ blank "> Empire Home cms, custom page how to implement paging </a> </ li> 
          <li> <A href =" / jstt / web / 2015-02-25 / 745.html "title = " [] has been selected template PSD source file to me, I'll give you design HTML! "target =" _ blank "> [already] named me template PSD source file, I'll give you design HTML! </a> </ Li> 
          <Li> <A the href = " 
          "Target =" _ blank "> [] solemnly declare this site provides only a static template download! </a> </ li> <li> <A href =" / News / S / 2015-01-23 / 741.html " title = "[] pregnancy diary life thus present the" target = "_ blank">] [diary during pregnancy so that the life of the present </a> </ li>
          <li> <a href="/jstt/bj/2015-01-09/740.html" title="【匆匆那些年】总结个人博客经历的这四年..." target="_blank"> [those in a hurry in summary] experienced personal blog </a> four years ... </ li>[Sharing] css3 different directions sidebar navigation wipe effects "target =" _ blank ">] [share different directions css3 sidebar navigation wipe effects </a> </ Li> 
          <Li> <A the href =" / jstt / bj / 2014-11-06 / 732.html " title =" share my personal blog how to do IP traffic from 10 to 600 (graphic) "target =" _ blank " > share my personal blog access how to do an amount of from 10 to 600, IP (graphic) </a> </ li>
          <li> <a href="/jstt/web/2015-01-01/739.html" title=" 2014年度优秀个人博客排名公布" target="_blank"> 2014 Outstanding personal blog rankings published </ a > </ li> 
          <li> <a href="/jstt/web/2014-12-18/736.html" title="2014年度优秀个人博客评选活动" target="_blank"> 2014 outstanding personal blog contest </a> </ Li> 
          <Li> <a href="/jstt/css3/2014-12-09/734.html" title="使用CSS3制作文字,图片倒影" target="_blank"> use CSS3 create text, pictures reflection </a> </ li> 
          <li> <A href = "/ jstt / css3 / 2014-11-18 / 733.html" title = "[share] css3 different sidebar navigation the direction of the wipe effects "target =" _ blank ">] [sharing css3 different directions sidebar navigation wipe effects </a> </ Li> 
        </ UL> 
      </ div> 
      <div class =" AD "> <img src="/static/images/03.jpg"> </div>
    </div>
  </div>
  <!-- container代码 结束 -->
  <div class="jzend"></div>
  {% endblock %}

Note that static files at:

css file address: "/ static / css / filename" 
Example 
<link href = "/ static / css / index.css" rel = "stylesheet"> 
Image file address: "/ static / images / filename" 
Example 
<img src = "/ static / images / img_1.jpg" alt = " [mentality] Do not design it this way wander"> 
JS file address: "/ static / js / filename" 
example 
<script type = "text / javascript "src =" / static / js / jquery.js "> </ script>

3. Use the template

Use {% extends 'template page'%} tag page template to inherit

Blocks with the same name as the content page template specified part

In the project of which 98% can use template inheritance, there is a small portion of the template used to load.

Include template loaded, use {% the include "loading page"%} will be loaded into the specified page full page

Views code view Optimization

 

from django.template.loader import get_template
from django.http import HttpResponse
from django.shortcuts import render_to_response


def newList(request):
    tempalte = get_template("newlist.html")
    result = tempalte.render({})
    return HttpResponse(result)


def index(request):
    return render_to_response("index.html", {"username": "songdan"})


def new(request):
    name = "1234"
    return render_to_response("about.html", locals())


def listpic(request):
    return render_to_response("listpic.html")

 

urls added to the routing 

from django.contrib import admin
from django.urls import path,re_path
from django.views.static import serve
from ArticleBlog_v1_1.views import *
from ArticleBlog_v1_1.settings import MEDIA_ROOT

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', index),
    re_path(r"^$", index),
    path('newList/', newList),
    path('new/', new),
    path('listpic/', listpic),
]

Then you can start the project, visit the home page. Issues and questions the path of note file encoding.

2.Django model

Django supports loosely coupled development model, it is strongly recommended that project developers will be divided into several independent modules developed, based on the idea, Django requirements for database operations, must act on the APP .

1. Create app in DjangoProject, command

python manage.py startapp  appName

Directory structure is as follows

Migrations directory database synchronization, database records synchronized recording 

__init__ package file 

Admin.py django comes Administration File 

Apps app file management, currently useless 

Models.py file database modeling, ORM files 

Tests.py test file django project 

Views. py django project view file

2. Database Modeling

Data model is described using a class, the class name for the table, for a field attribute

Modify models.py 

from django.db import models


# Create your models here.
class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    gender = models.CharField(max_length=16)
    birthday = models.DateField()
    email = models.EmailField()
    address = models.TextField()
    photo = models.ImageField(upload_to="images")

    def __str__(self):# 便于查看
        return self.name


class ArticleType(models.Model):
    label = models.CharField(max_length=32)
    description = models.TextField()

    def __str__(self):
        return self.label

class Article(models.Model):
    = models.CharField title (max_length = 32) 
    article_author = models.ForeignKey (to = Author, on_delete = models.CASCADE) 
    # models.CASCADE cascading deletes, author deleted, delete the article 
    # models.SET_NULL empty set, delete authors, article the authors set to null 
    # models.SET_DEFAULT set defaults, the author deleted, the authors set to default values, you need to use with the default parameters 
    desciption = models.TextField () 
    Content = models.TextField () 
    article_type = models.ManyToManyField (to ArticleType =) 
    public_time = models.DateField (auto_now = True) 
    Picture = models.ImageField (upload_to = "ImagesRF Royalty Free") 

    DEF __str __ (Self): 
        return self.title

 

name

description

Remark

CharField

Character Types

There must be max_length

IntegerField

Integer

 

FloatField

Decimal

 

DataField

Time (date)

Date auto_now

Data Time Field

time

Year, month, day, hour auto_now

EmailField

mailbox

Meet email string format

TextField

text

 

ImageField

image

1, there is upload parameters

2, must be installed pillow block

ForeignKeyField

Foreign key

1, to map object

2、On_delete

models.CASCADE cascading delete, delete authors, delete the article
# models.SET_NULL set empty, the author deleted, the authors set to null
# models.SET_DEFAULT set defaults, the author deleted, the authors set to the default value and in line with default parameters

 

ManyToManyField

Many to many

To map object

Guess you like

Origin www.cnblogs.com/songdanlee/p/11426350.html