Django actual combat (1) simple blog display

1. Create the basic framework

  Enter the following command in the console: 
django-admin startproject MyDjango 
(2) Create app 
cd MyDjango 
Python manage.py startapp MyBlog

2. Django application development (MVT framework development)

 1 The establishment of database table (M) 


Myblog/model.py: 
First, add MyBlog to INSTALLED_APPS in setting.py; 
create table content in model.py: (title, content, release time, etc.)

 
 
from django.db import models 

# Create your models here.
class Blogpost(models.Model):
title=models.CharField('title',max_length=50) #The Chinese character part can replace the name of the table 'title' on the admin management page, The second is the maximum length
content=models.CharField('content',help_text='blog content',max_length=5000) #help_text will display prompt information below the input box
pub=models.DateTimeField('published time')
class Meta: #Define metadata for your model through an embedded class "class Meta"
verbose_name='blog' #It is a more readable unique name for the object: 
verbose_name_plural=verbose_name #The plural of the object name:
ordering= ['-pub'] # sort the pub field in descending order

def __str__(self):
return self.title

After the database is established, run the following: 
python manage.py makemigrations 
python manage.py migrate 
was created successfully. 
Manage database content:

in admin.py

  from django.contrib import admin
  from .models import BlogPost
  # Register your models here.
#Blog模型的管理器 class BlogPostAdmin(admin.ModelAdmin): list_display = ('title','pub')
#Register binding in admin admin.site.register(BlogPost,BlogPostAdmin)

Create an administrator: python manage.py createsuperuser

Enter username and password;

Created successfully. Enter the following command:

python manage.py runserver

run server

Then enter the URL 127.0.0.1:8000/admin in the browser to log in:

Click add to add a blog

 

2 Add view control: (V) 
In view.py: (a view function corresponds to a url)

from django.shortcuts import render,render_to_response
from .models import BlogPost
# Create your views here.
def myBlog(request):
    blog_list=BlogPost.objects.all()
    return render_to_response('BlogTemplate.html',{'blog_list':blog_list})

In urls.py, define a new url: 
url(r'^myBlogs/$',myBlog),

First import view:

from MyBlog.view import *

3 Complete the template configuration (T) 
Create a new folder templates in the MyBlog folder, and then create a new BlogTemplate.html in the templates. 
We can define a base base.html as the base class for all pages: 
base.html

<!DOCTYPE html>
<html lang="zh">
<head>

<meta charset="UTF-8">
    <title>title</title>
</head>
<style type="text/css">
    body{
        color:#efd;
        background: #bbbbbb;
        padding: 12px 5em;
        margin: 7px;
    }
    h1 {
        padding: 2em ;
        background: #675;
    }
    h2{
        color: #85F2F2;
        border-top: 1px dotted #fff;
    }
    p{
        margin: 1em 0;
    }

</style>
<body>
<h1>My blog post</h1>
<h3>This is a concise blog post</h3> 
{ % block content % }
{% for post in blog_list %}
       <h2>{{ post.title }}</h2>
        <p>{{  post.pub}}</p>
        <p>{{ post.content }}</p>
{% endfor %}
{% endblock %}

</body>
</html>

Run the command: Python manage.py runserver, get the URL 
Enter in the browser: http://127.0.0.1:8000/myBlogs/ , get the content:

 

Here, a simple blog is over. 
To sum up: Django development is developed in the MVT mode, and you can develop a good program by being familiar with the role of each module.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325083085&siteId=291194637