Python uses Django to develop its own blogging system

Source: Internet
Author: User
Tags install django
A long time ago want to do their own blog system, but on the internet looked like it needs to be some of the relevant knowledge of node. js, but also to install spicy and many of the library or something, do not want to touch. But I met a Django artifact like this, and I didn't expect my blogging system to be built up. Although it is the most basic type. But also a success, this blog is more suitable for Django have a certain understanding of children's shoes, if it is a novice, it is recommended to look at the basic knowledge of Django first to do experiments, so more efficient!

Well, don't say much, let's get started.

Building framework
• Create projects and applications

The idea of building a framework is to install Django and do the relevant configuration. Because I created it under Pycharm, I did a lot of things instead of the tool. But the bottom line is nothing more than the following lines of code:

Create a Django project named Mydjango

Django-admin Startproject Mydjango

Create a Django app named MyBlog. It should be noted here that the application belongs to a subset of projects. In layman's terms, the application folder exists as a subset of the project folder.

Django-admin Startapp MyBlog

• Create databases and underlying models

I simply use the default Sqlite3 database as the database of my blog system, of course, you can also make your own database, in general Sqlite3 can meet the needs. This can be set in setting.py.

# database# https://docs.djangoproject.com/en/1.9/ref/settings/#databasesDATABASES = {' default ': {  ' ENGINE ': ' Django.db.backends.sqlite3 ',  ' NAME ': ' myblog.db ',  ' USER ': ',  ' PASSWORD ': ', '  HOST ': ',  ' PORT ': ',}}

The database is built, and then the model is created. Because I created a blog system, so essential to publish content about the blog, so need to have the title, content, publish time these attributes, details such as models.py file

From __future__ import unicode_literalsfrom django.contrib import adminfrom django.db import models# Create the blog model Class blogpost (models. Model): title = models. Charfield (max_length=150) BODY = models. TextField () timestamp = models. Datetimefield () def __unicode__ (self):  return Self.title


Because administrators need to manage published blogs, we want to set up a management model for published blogs,

# Set the admin page for Blogpostclass blogpostadmin (admin. Modeladmin): List_display = (' title ', ' timestamp ') # Register the model (especially Importantadmin.site.register ( Blogpost)


So the whole models.py file should be like this.

From __future__ import unicode_literalsfrom django.contrib import adminfrom django.db import models# Create the blog model Class blogpost (models. Model): title = models. Charfield (max_length=150) BODY = models. TextField () timestamp = models. Datetimefield () def __unicode__ (self):  return self.title# set, the admin page for Blogpostclass blogpostadmin (admin. Modeladmin): List_display = (' title ', ' timestamp ') # Register the model (especially Importantadmin.site.register ( Blogpost)

The next step is to synchronize the connection between the database and the model. If you do not synchronize, it is possible to quote
django.db.utils.OperationalError:unable to open database file
And this is a very important part of it. That's the question about the Django version, which I've planted once before.

when Django < 1.7:
Python manage.py syncdb

When Django > 1.7:
Python manage.py makemigrations
Python manage.py Migrate

Perfecting the MVC pattern

In fact, the previous steps, we have completed the model of the function of the module, the next step is to do a good job of mapping the view.
V (views.py) View layer

We need to define the underlying logical processing in this file. This determines what kind of response to return to the user. As for which rendering method to use, we should not waste unnecessary time on this. The Render_to_response is enough.

# Create the View for blog Showdef myblogs (request): Blog_list = BlogPost.objects.all () return Render_to_response (' Blogtem Plate.html ', {' blog_list ': blog_list})

The template file is used here, and the template is passed to the parameter of a list type, which we will later.

C (Controller) urls.py

It can be said that this file connects the loosely-coupled functionality of the Django parts and completes the non-core core of the entire project's operation, and is about how the logic of mapping is handled. Next, let's set up our blog system.

From django.conf.urls import urlfrom django.contrib import adminfrom myblog.views import *urlpatterns = [url (r ' ^admin/'), admin.site.urls), url (r ' ^myblogs/$ ', myblogs),]

For a detailed description of how to map, in my previous article, Pycharm developed the Django base configuration. If you are interested, please refer to it. OK, this time we can run our program after we have finished the admin Admin user's settings.

Python manage.py runserver

Came up:

Performing system checks ... System Check identified no issues (0 silenced). June, 2016-11:39:27django version 1.9.6, using Settings ' mydjango.settings ' starting development server at http://www. Php.cn/:8000/quit the server with Ctrl-break.

You can then enter it in the browser
Http://127.0.0.1:8000/admin, after the successful landing can click the following blog posts Edit blog. Then click on the Save button to publish our blog. Next, enter in the browser
Http://127.0.0.1:8000/myBlogs/has visited our blog system.

This will enable the establishment of our blog system. But because no style is added, it doesn't look good, so we'll add the following template style.

Template configuration

Then just continue, about the template, which is a very deep philosophy of design. Everyone who has known will definitely have feelings, I will not say more.
Next, set up a template for our blog system.
• Parent Template base.html
We can make a parent template by following the Django inheritance settings for the template. As follows:

<! DOCTYPE html>

• Then the child template blogtemplate.html

{% extends "base.html"%} {% block content%}  {% for post in blog_list%}   

It is important to note that template tags and template variables in the template should match the dictionary variables in the function corresponding to the views.py file, or Django will not display the data, although it does not error.

Next refresh, enter http://127.0.0.1:8000/admin/

Click the Add button to start adding your blog post.

Then in the browser, enter
http://www.php.cn/:8000/myBlogs/You can see the list of your blogs,

As you may have seen, the problem is that clicking on the title does not go to the relevant details page because it has not been added yet. (^__^) hehe ...

Summarize

Today we made a simple blog system, although the appearance does not look very good, but the content of what is almost like this. There are plenty of places to be perfected.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.