Python uses Django to develop its own blog system, pythondjango

Source: Internet
Author: User
Tags install django

Python uses Django to develop its own blog system, pythondjango

I wanted to create my own blog system a long time ago, but I checked it online and found that I needed some Node. js-related knowledge, but also need to install many library or something, don't want to touch. But I met Django, an artifact, and did not expect my blog system to be built up like this. It is the most basic type. However, this blog is successful. This blog is suitable for children who have a certain understanding of Django. If you are a newbie, you are advised to take a look at the basic knowledge points of django and try again. This is more efficient!
Okay. Let's get started.

Build a framework
• Create projects and applications

The purpose of building a framework is to install Django and make relevant configurations. Because I created it in PyCharm, the tool has done a lot of things in place of me. However, the underlying layer is nothing more than the following lines of code:

Create a Django project named MyDjango

Django-admin startproject MyDjango

Create a Django application named MyBlog. Note that the application is a subset of the project. In general, Application folders exist as a subset of project folders.

Django-admin startapp MyBlog

 • Create databases and underlying models

Here, I simply use the default sqlite3 database as the database of my blog system. Of course, you can also set your own database. In general, sqlite3 can meet your needs. You can set this 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':'', }}

After the database is created, the model is created. Because I created a blog system, it is essential to publish the content about the blog, so I need to have attributes such as the title, content, and release time, such as the models. py file.

from __future__ import unicode_literalsfrom django.contrib import adminfrom django.db import models# create the blog modelclass 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 need 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)

Therefore, this should be the case in the entire models. py file.

from __future__ import unicode_literalsfrom django.contrib import adminfrom django.db import models# create the blog modelclass 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)

Next, synchronize the connection between the database and the model. If no synchronization operation is performed, it is very likely that
Django. db. utils. OperationalError: unable to open database file
This is also an important part. That's about the Django version. I planted it once before.

 Django <1.7:
Python manage. py syncdb

Django> 1.7:
Python manage. py makemigrations
Python manage. py migrate

Improve the MVC Mode

In fact, in the previous steps, we have completed the functions of the model module. Next, we can map the views.
• V (views. py) view layer

We need to define the underlying logic processing in this file. This determines the response to be returned to the user. As for the rendering method, you should not waste unnecessary time on it. Render_to_response is enough.

# create the view for blog showdef myBlogs(request): blog_list = BlogPost.objects.all() return render_to_response('BlogTemplate.html',{'blog_list':blog_list})

The template file is used, and a list type parameter is passed to the template, which will be described later.

• C (controller) urls. py

It can be said that this file links various parts of Django's loosely coupled functions, and completes the non-core of the entire project's operation. It is about how to map the logic. 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 more information about how to map data, see PyCharm's Django basic configuration in my previous article. For more information, see. Now, after completing the settings of the admin administrator user, we can run our program.

Python manage. py runserver

The following error occurs:

Performing system checks...System check identified no issues (0 silenced).June 05, 2016 - 11:39:27Django version 1.9.6, using settings 'MyDjango.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.

In this case, you can enter
Http: // 127.0.0.1: 8000/admin. After successful login, click the following Blog Posts to edit the Blog. Click SAVE to publish our blog. Enter
Http: // 127.0.0.1: 8000/myBlogs/access our blog system.

In this way, the establishment of our blog system is realized. However, because no style is added, it does not look very nice, so we need to add the style of the following template.

Template Configuration

Next, let's continue. There is a deep design philosophy for the template. Everyone who knows it will surely feel it. I will not talk about it much.
Next, set a template for our blog system.
• Parent template base.html
According to django's inheritance settings, we can create a parent template. As follows:

<! DOCTYPE html> 

• After all, the sub-template blogtemplate.html

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

Note that the template tags and template variables in the template should be consistent with the dictionary variables in the functions corresponding to the views. py file. Otherwise, django will not report errors, but will not display data.

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

Click add to add your blog.

Enter
http://127.0.0.1:8000/myBlogs/. You can see your blog list,

As you may have seen, the problem is that the relevant details page is not displayed when you click the title, because this function has not yet been added. (^__^) Hey ......

Summary

Today, I made a simple blog system. Although it looks not very nice, it's almost like this. There are still many things to be improved.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.