Django develops its own blog system

Source: Internet
Author: User
Tags install django

For a long time to do their own blog system, but checked on the internet as if it is necessary to have some node.js knowledge, but also to install a lot of spicy library or something, do not want to touch. But I met Django such an artifact, did not expect my blog system is so built up. Although it is the most basic type. But also be successful, this blog is more suitable for Django have a certain understanding of the children's shoes, if it is a novice, it is recommended to look at the basics of Django and then do the experiment, so the efficiency is higher.
Well, don't say much, let's go. Build a framework

Creating Projects and Applications

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

# Create a Django project named Mydjango

django-admin startproject Mydjango # to


create a Django application named MyBlog. It should be noted here that the application is subordinate to the subset of the project. In layman's terms, the application folder exists as a subset of the project folder.

django-admin Startapp MyBlog

creating databases and underlying models

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

# Database


# https://docs.djangoproject.com/en/1.9/ref/settings/#databases


databases = {
    ' default ': {
        ' ENGINE ': ' Django.db.backends.sqlite3 ', '
        NAME ': ' myblog.db ', '
        USER ': ', ' PASSWORD ': '
        , '
        HOST ': ' ,
        ' PORT ': ',
    }
}

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

From __future__ import unicode_literals to
django.contrib import admin from
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

Since administrators are required to manage the published blog, we need to set up a management model for the published blog.

# Set the admin page for blogpost

class blogpostadmin (admin. Modeladmin):
    list_display = (' title ', ' timestamp ')


# Register the model (especially important

Admin.site.register (blogpost)

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

From __future__ import unicode_literals to
django.contrib import admin from
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 blogpost

class Blogpostadmin (admin. Modeladmin):
    list_display = (' title ', ' timestamp ')


# Register the model (especially important

Admin.site.register (blogpost)

The next step is to synchronize the relationship 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 it's also a very important part of it. That's the problem with the Django version, which I planted once before.

Django < 1.7: Python
    manage.py syncdb

Django > 1.7:
    python manage.py makemigrations
    python mana ge.py Migrate
perfecting the MVC pattern

In fact, the previous steps, we have completed the model of the function of the module, the next is to do the mapping of the view can be.

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 show

def myblogs (request):
    blog_list = BlogPost.objects.all () return
    render_to_ Response (' blogtemplate.html ', {' blog_list ': blog_list})

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

C (Controller) urls.py

It can be said that this file connects the loosely coupled functions of the Django parts together, completing the non-core core of the entire project's operation, and is about how to map the logical processing. Next, let's set up our blog system.

From django.conf.urls import URL from
django.contrib import admin from
myblog.views Import *

urlpatterns = [ C3/>url (R ' ^admin/', admin.site.urls),
    url (r ' ^myblogs/$ ', myblogs),
]

on how to map, my previous article contains a detailed introduction to the Pycharm development of the Django infrastructure configuration. Interested can refer to.

OK, this time we can run our program after we have completed admin Admin user settings.

Python manage.py runserver

showed up:

Performing system Checks

... System Check identified no issues (0 silenced).
June, 2016-11:39:27
Django version 1.9.6, using Settings ' mydjango.settings '
starting development Server at H Ttp://127.0.0.1:8000/
Quit the server with Ctrl-break.

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

This is the realization of our blog system has been established. But it doesn't look good because it doesn't add a style, so we're going to add the following template style. Template Configuration

Then just continue, about the template, which is a very deep philosophy of design. Know that everyone will 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 according to the Django inheritance settings for the template. As follows:

<! DOCTYPE html>
And then there's the sub template blogtemplate.html
{% extends ' base.html '%}
    {% block content%}
        {% for post in blog_list%}
            

Note that template tags and template variables in the template should be consistent with the dictionary variables in the function corresponding to the views.py file, or Django will not display the data without an error.

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

Click the Add button to start adding your blog post.

And then enter in the browser
http://127.0.0.1:8000/myBlogs/. You'll be able to see your blog list, as shown in

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

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

Interested friends can DMS discussion or comments in the following Oh, we study together.

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.