Django quickly build a blog
Django is a framework based on MVC constructs. But in Django, the part of the controller that accepts user input is handled by the framework itself, so Django is more concerned with Models, Templates, and Views, called MTV mode.
Django is an open source web application framework written in Python. The frame mode of MT'V is adopted, namely model M, template T and view V. It was originally developed to manage some of the news content-based websites of the Lawrence Publishing Group, namely CMS (Content Management System) software. It was released under the BSD license in July 2005. The frame is named after the Gypsy jazz guitarist Django Reinhardt of Belgium.
Blog=blog, only transliteration, the English name is Blogger, which is a hybrid of Web Log. Its official name is the network diary; it is also transliterated as a blog or tribal cabinet, etc., is the use of specific software, publishing, publishing and posting personal articles on the Internet, or a person usually managed by individuals, occasionally posting new The article's website. Articles on blogs usually appear as netizens and are sorted in reverse order based on posting time.
The blog is the fourth kind of network communication method after MSN.BBS.ICQ. It has been welcomed by everyone. It is a personal "reader's digest" in the Internet age. It is a network diary with a hyperlink as a weapon. It represents a new one. The way you live, work and learn. Many blogs focus on providing comments or news on specific topics, while others are used as personal diaries. A typical blog that combines text, images, links to other blogs or websites, and other topic-related media, allowing readers to leave comments in an interactive way is an important element of many blogs. Most of the blog content is mainly text, and there are still some blogs focusing on various topics such as art, photography, video, music, and podcasting. Blogs are part of the social media network. More famous are Sina, Netease and other blogs.
Django quickly build a blogDjango claims to be "the perfect WEB framework for developing limited periods." This article refers to the "Django web development guide", quickly build a blog out, involving many knowledge points in the middle
Create project
Create a mysite project:
D:/djpy" django-admin.py startproject mysite
Project directory structure:
Manage.py ----- A tool in the Django project that lets you call the django shell and database.
setTIngs.py ---- Contains the default settings for the project, including database information, debug flags, and other working variables.
Urls.py ----- Responsible for mapping URL patterns to applications.
Create a blog app
Create a blog app in the mysite directory
D:/pydj" cd mysite
D:/djpy/mysite$ python manage.py startapp blog
Directory Structure:
Initialize the admin backend database
Python comes with SQLite database, Django supports a variety of mainstream databases, here is recommended to use SQLite, if you use other databases, please set in the setTIngs.py file.
Switch to mysite to create a database:
D:/djpy/mysite$ python manage.py syncdb
C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
Warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
OperaTIons to perform:
Synchronize unmigrated apps: staTIcfiles, messages
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables. .
Running deferred SQL. .
Installing custom SQL. .
Running migrations:
Rendering model states. . DONE
Applying contenttypes.0001_initial. . OK
Applying auth.0001_initial. . OK
Applying admin.0001_initial. . OK
Applying contenttypes.0002_remove_content_type_name. . OK
Applying auth.0002_alter_permission_name_max_length. . OK
Applying auth.0003_alter_user_email_max_length. . OK
Applying auth.0004_alter_user_username_opts. . OK
Applying auth.0005_alter_user_last_login_null. . OK
Applying auth.0006_require_contenttypes_0002. . OK
Applying sessions.0001_initial. . OK
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'fnngj'): username (default current system username)
Email address: Email address
Password: password
Password (again): Repeat password
Superuser created successfully.
Set the admin application
Admin is a background management system that comes with Django.
1. Add the blog app and open the mysite/mysite/settings.py file:
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
Add a blog app at the end of the list
2. When we create the django project, the admin is created and the mysite/mysite/urls.py file is opened:
From django.conf.urls import include, url
From django.contrib import admin
Urlpatterns = [
Url(r'^admin/', include(admin.site.urls)),
]
3, start the django container
D:\pydj\mysite"python manage.py runserver
Performing system checks. .
System check identified no issues (0 silenced).
October 04, 2015 - 20:56:45
Django version 1.8.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
4, access the background application
Http://127.0.0.1:8000/admin
Enter the user, password, and username and password that were created the first time the database was created. Recall the settings when setting up the database.
Design Model (that is, design database table)1, design model
Now we open the models.py file in the blog directory, which is where we define the blog data structure. Open the mysite/blog/models.py file to modify it:
From django.db import models
From django.contrib import admin
# Create your models here.
Class BlogsPost(models.Model):
Title = models.CharField(max_length = 150)
Body = models.TextField()
Timestamp = models.DateTimeField()
Admin.site.register(BlogsPost)
2, initialize the database again
D:\pydj\mysite"python manage.py makemigrations blog
Migrations for 'blog':
0001_initial.py:
- Create model BlogsPost
D:\pydj\mysite"python manage.py syncdb
C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
Warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, blog, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables. .
Running deferred SQL. .
Installing custom SQL. .
Running migrations:
Rendering model states. . DONE
Applying blog.0001_initial. . OK
3, runserver again to start the service, access the admin background, create an article.
Login successfully select add to create a blog
Enter the blog title, body, date and time, and click save to create a blog.
Set admin's BlogsPost interface
Open the mysite/blog/models.py file and make the following changes:
From django.db import models
From django.contrib import admin
# Create your models here.
Class BlogsPost(models.Model):
Title = models.CharField(max_length = 150)
Body = models.TextField()
Timestamp = models.DateTimeField()
Class BlogPostAdmin(admin.ModelAdmin):
List_display = ('title','timestamp')
Admin.site.register(BlogsPost,BlogPostAdmin)
Create a BlogPostAdmin class, inherit the admin.ModelAdmin parent class, and display the title and time of the BlogPost in a list.
Create a public part of the blog
From a Django perspective, a page has three typical components:
A template: The template is responsible for displaying the information passed in.
A view: The view is responsible for getting the information that needs to be displayed from the database.
A URL pattern: it is responsible for matching the received request with your attempted function, and sometimes passing some parameters to the view.
Create a template
Create a templates directory (mysite/blog/templates/) under the blog project, and create a template file index.html in the directory, as follows:
{% for post in posts %}
"h2"{{ post.title }}"/h2"
"p"{{ post.timestamp }}"/p"
"p"{{ post.body }}"/p"
{% endfor%}
Create a view function
Open the mysite/blog/views.py file:
#coding=utf-8
From django.shortcuts import render
From blog.models import BlogsPost
From django.shortcuts import render_to_response
# Create your views here.
Def index(request):
Blog_list = BlogsPost.objects.all()
Return render_to_response('index.html',{'blog_list':blog_list})
Blog_list = BlogPost.objects.all() : Get the BlogPost object owned by the database
Render_to_response() returns a page (index.html), along with all the blog content (blog_list) queried in the database.
Create a URL pattern for the blog
Add the url of the blog to mysite/urls.py:
#coding=utf-8
From django.conf.urls import patterns, include, url
From django.contrib import admin
Urlpatterns = patterns('',
Url(r'^admin/', include(admin.site.urls)),
Url(r'^index/$', 'blog.views.index'),
)
Start the service again ($ python manage.py runserver), access the blog application (http://127.0.0.1:8000/index/) The picture below is wrong, for reference only.
The page is as follows:
Of course, the reader can continue to add a blog to the admin background to refresh the page to display the newly added blog.
Add style
Create a base template
Create a template for base.html in the mysite/blog/templates directory:
"html"
"style type="text/css"
Body{color:#efd;background:#453;padding:0 5em;margin:0}
H1{padding:2em 1em;background:#675}
H2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
P{margin:1em 0}
"/style"
"body"
"h1" worm blog "/h1"
"h3" is not a big man, gentleman is pragmatic "/h3"
{% block content %}
{% endblock %}
"/body"
"/html"
Modify the index.html template to reference the base.html template and its "content" block.
{% extends "base.html" %}
{% block content %}
{% for post in posts %}
"h2"{{ post.title }}"/h2"
"p"{{ post.timestamp | date:"1,F jS"}}"/p"
"p"{{ post.body }}"/p"
{% endfor %}
{% endblock %}
Refresh the blog page again:
Enclosure Junction Box,Ip44 Waterproof Terminal,Electronic Connector Box, Outdoor Cable Connectors,junction box
Guangdong Ojun Technology Co., Ltd. , https://www.ojunconnector.com