Quickly create a blog using Django

Source: Internet
Author: User

 

1,Create a project

33. Run the script file in the python directory.

Django-admin.py startproject mysite

In this way, a project named mysite is generated.

Execute. \ manage. py runserver to run the development server. At this time, the terminal will output

Log on to http: // 127.0.0.1: 8000/. The page is displayed.

 

At the same time, the token will output [13/Aug/2014 01:05:14] "Get/HTTP/1.1" 200 1957

Timestamp, request, HTTP status code, and number of bytes from left to right

2,CreateBlogApplication

Execute. \ manage. py Startapp blog

In this way, the blog app is created, and the blog directory is under the project (mysite) directory.

Then you need to find the installed_apps tuples in siteid. py under the mysite directory, and add 'blog. app' to the end of the tuples (note the comma)

3,DesignModel

Add the following code to models. py:

From Django. DB import models

From Django. contrib import Admin

 

Class blogpost (models. Model ):

Title = models. charfield (max_length = 150)

Body = models. textfield ()

Timestamp = models. datetimefield ()

4,Set Database

If sqlite3 is used, you only need to complete the first two items in the databases Dictionary of setting. py, namely, 'engine': 'django. DB. backends. sqlite3'

'Name' = *** \ Django. DB # indicates the data storage address, which is customized.

Execute. \ manage. py syncdb # synchronize the data terminal, and the following output is displayed.

Creating table auth_message

Creating table auth_group

........

You will be asked questions about Django. contrib. Auth app.

5. Set the automatic admin Application

Add the following line under 'django. contrib. auth' in settings. py:

'Django. contrib. admin'

Then run the syncdb command again,

After setting the app, you must specify a URL for it.

Remove the annotator before RL (R' ^ admin/', include (Admin. Site. URLs) in URLs. py.

Finally, import admin in models. py

From Django. contrib import Admin

Add a line of model registration code at the end.

Admin. Site. Register (blogpost)

6. log on to 127.0.0.1: 8000/admin using admin and enter the user name and password. The page is displayed.

 

 

Edit several blogs and save them as needed. Add the blogpostadmin class to the models. py file to make the notebook list more beautiful, and add it to the registration code.

From Django. DB import models

From Django. contrib import Admin

 

Class blogpost (models. Model ):

Title = models. charfield (max_length = 150)

Body = models. textfield ()

Timestamp = models. datetimefield ()

Class meta:

Ordering = ('-timestamp ',)

Class blogpostadmin (Admin. modeladmin ):

List_display = ('title', 'timestamp ')

 

Admin. Site. Register (blogpost, blogpostadmin) # regiet blogpost Model

Refresh the page and you will see

 

Compared with the original title and timestamp columns

7. Create a public part of the blog.

Create an archive.html file and save it to blog \ templates as follows:

{% For post in posts %}

<H2 >{{ post. Title }}</H2>

<P >{{ post. timestamp | date }}</P>

<P >{{ post. Body }}</P>

{% Endfor %}

Create a view function, open the blog \ view. py file, and enter:

From Django. template import loader, Context

From Django. Http import httpresponse

From mysite. Blog. Models import blogpost

 

Def archive (request ):

Posts = blogpost. Objects. All ()

T = loader.get_template('archive.html ')

C = context ({'posts': Posts })

Return httpresponse (T. Render (c ))

Then create the URL mode and add it in mysite/URLs. py.

URL (R' ^ blog/', include ('mysite. Blog. urls ')),

This will capture any requests starting with blog/and pass them to the following URL Mode

Mysite/blog/URLs. py:

From Django. conf. URLs. defaults import *

From mysite. Blog. Views import Archive

 

Urlpatterns = patterns ('',

URL (R' ^ $ ', archive ),

)

 

8. Last retouching

Create a base.html file and save it in mysite/blog/templates as follows:

<HTML>

<Style type = "text/CSS">

Body {color: # cdcdcd; backgroud: # cdcdcd; padding: 0 5em; margin: 0}

H1 {padding: 2em 1em; backgroud: #675}

H2 {color: # cdcdcd; border-top: 1px dotted # cdcdcd; margin-top; 2em}

P {margin: 1em 0}

</Style>

<Body>

<H1> mysite.example.com

{% Block content %}

{% Endblock %}

</Body>

</Html>

Then update achive.html:

{% Extends "base.html" %}

{% Block content %}

{% For post in posts %}

<H2 >{{ post. Title }}</H2>

<P >{{ post. timestamp | date }}</P>

<P >{{ post. Body }}</P>

{% Endfor %}

{% Endblock %}

Log on to http: // 127.0.0.1: 8000/blog/

 

You can add a nested class Meta to the models. py file: Sort blogs in order.

From Django. DB import models

From Django. contrib import Admin

 

Class blogpost (models. Model ):

Title = models. charfield (max_length = 150)

Body = models. textfield ()

Timestamp = models. datetimefield ()

 

Class meta:

Ordering = ('-timestamp ',)

 

 

Class blogpostadmin (Admin. modeladmin ):

List_display = ('title', '-timestamp ')

 

Admin. Site. Register (blogpost, blogpostadmin) # regiet blogpost Model

 

 

'-Timestamp' indicates the descending 'timestamp'.

 

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.