Chapter 1.3 Creating a blog app

Source: Internet
Author: User
Tags sqlite unique id

1 Creating a blog
Go to the project catalog, execute python manage.py startapp Blog

Generated a series of files in the red box below, there are empty files, there is no real code, just occupy the location, just tell Django that the app is part of the project.

Find Installed_apps in settings.py, add ' blog ', Django uses Installed_apps to determine the configuration of different parts of the system, including automated admin application, test framework.

2 ORM
Models.py is the core part of Django's blog application, the following code defines the blogpost class, which is the subclass of Django.db.models.Model and the core of the ORM. This class defines 3 variables, as well as a hidden self-increment, unique ID variable.

From __future__ import unicode_literals from

django.db import models

class blogpost (models. Model):
    title = models. Charfield (max_length=150)
    BODY = models. TextField ()
    timestamp = models. Datetimefield ();

Database using SQLite, mainly for SQLite needs to be installed, easy to test. Execute command python manage.py migrate create data table, use tool Sqlitespy to see

Perform the following two command creation to create a blog_blogpost table

Python manage.py makemigrations
python manager.py migrate

Execute python manage.py createsuperuser create Admin user
3 admin Section
Add Admin.site.register in models.py ( Blogpost)

Enter http://127.0.0.1:8000/admin with Admin user login


Click Add to enter the following image:

Save succeeded as follows:

I'm not the first A person who feels blogpost object is not happy, so he made the following box adjustment

Refresh the page, the content is displayed

4 Public Section
from a Django perspective, A page has three typical components:
A Template: The template is responsible for displaying the information passed in (in a Python-like Dictionary object context)
A view function: Responsible for getting the information to be displayed, Usually you get the
one URL pattern from the database: it is used to match the received request to the view function, and sometimes some parameters are passed to the view.
Edit views.py

From django.template import loader,context from
django.http import HttpResponse from
blog.models Import Blogpost
# Each Django view function will django.http.HttpRequest the object as its first parameter
, Def archive (Request):
# This line uses ORM, Gets all the blogpost objects in the database.
    posts = BlogPost.objects.all ()
# This tells the name of the Django template to create the template object
    t = loader.get_template ("archive.html")
# The data that the Django template renders is the
    c = context ({' Posts ':p OSTs}) provided by the object Context of a dictionary class
Each Django view function will return a Django.http.HttpResponse object.
    return HttpResponse (T.render (c))

Create a Templates folder in your blog and create a archive.html

{% for post in posts%}

Create the urls.py file under the blog directory, as follows:

From django.conf.urls import URL from
blog.views import archive

urlpatterns = [
    url (R ' ^$ ', archive),
]

In MySite, add the following urls.py:

From Django.conf.urls import URL, include from
django.contrib import admin

urlpatterns = [
    url (R ' ^admin/', admin.site.urls),
    url (r ' ^blog/', include (' Blog.urls ')),
]

Enter http://127.0.0.1:8000/blog/and run as follows:

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.