Django Combat: Build a Blog

Source: Internet
Author: User

This article is about how to use the Django framework to implement a blog system, you can manage the blog in the background.
One, create the project

django-admin.py startproject mysite

As shown in the following:

After you run the command, a directory is created under the current directory MySite

Two, test the development server
Go to the MySite directory, and then run the./manage.py runserver 0.0.0.0:8001, as shown in:

Note that the port number I chose here is 8001, you can also choose a different port (such as 8002), but note that it cannot be occupied
We enter 129.168.129.140:8001 in the browser, as shown in:

Seeing the above shows that your development server is functioning properly. Note that the IP address I entered here is 129.168.129.140, you need to change your IP address
Can be seen ifconfig, as shown:

Third, new blog application
Enter the./manage.py Startapp Blog as shown:

A blog directory is created below the current directory, which corresponds to our blog app.
Add an app blog to the settings.py configuration file
Enter the Vim mysite/settings.py and add the blog to the Installed_apps as shown in:

Note that this step is necessary, we will add this app to the config file for each new app.

Four, design model
Our Vim blog/models.py
Then modify it as shown in the following:

We have created a new blog class that inherits from models. Model. Then we define the three member variables title, body, and time, and pay attention to their type.
Our models.py is to deal with the database, here to create a blog class, then in the database will generate a corresponding blog table, three members will correspond to the three columns of the blog table.

Five, setting up the database
We will set up the database (configured in mysite/settings.py) to generate a table that corresponds to the class defined in models.py.
Vim mysite/settings.py, then make the following modifications:

The database I set up here is Sqlite3, and of course you can set it up as MySQL or other database. Then name the path of the database file to be set, where the path is relative to the project's root directory .

Six, synchronizing the database
After you save the exit, enter the./manage.py syncdb in the terminal to generate the corresponding table, as shown in:

Note that when you first run, you will be asked if you need to create a super user, it is best to create it and use it later.
After the synchronization is complete, you can see the generated sqlite3 database file, as shown in:

Seven, admin
Admin is an automated back-Office application. Admin as an application, the status is the same as the application blog we just created, so we have to declare it in the configuration file, as shown in:

Let's remove this line of comments, as shown in:

Execute the command again./manage.py SYNCDB Synchronize the database, and note that each time we add an app, we need to synchronize the database to create the various tables that the app corresponds to, as shown in:

Eight, specify the URL
Modify the mysite/usrls.py file as shown in:

Remove the comment from this line and we will be able to access it later by entering ip:port/admin/in the browser.
As shown in the following changes:

Nine, register model
Why do you need this step? You need to tell Django which model to display in the admin window.
We modify the blog/models.py as shown in:

10, Test
Under Terminal, enter the./manage.py runserver 0.0.0.0:8001 as shown:

Then enter yourip:8001/admin in the browser, as shown in:

Login:
Enter the user name and password set in step 6th to log in and log in as shown below.

Django Development Video Tutorial: http://www.duobei.com/course/3812028831

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Django Combat: Build a Blog

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.