Getting started with building a blog using Django

Source: Internet
Author: User
Tags django server

1. Create a project file. In Windows, in cmd, go to the directory where the Django project is stored. For example, in the drive D, name mysite, enter drive D, and enter:
Python django-admin.py startproject mysite
We can see that a new Django project file named mysite is generated in drive D, which contains
_ Init _. py # in Python, The __init _. py file indicates that the project directory is a python package.
Manage. py # Use the Django management tool manage. py help to view its usage
Settings. py # default configuration of the project, some static variables placed
URLs. py # configure Django URL ing
2. Start the server. In this directory, enter:
Python./manage. py runserver # Start Django
Enter 127.0.0.1: 8000 to see the words it's working.
3. Create a project web application file. Input:
Python./manage. py Startapp blog
You can see that a new blog file is created under mysite. The file contains
_ Init _. py # is considered as a python package
Models. py # compile Django data classes and create databases
Views. py # compile a view function to obtain the data display from the data and map it to the template.
4. In models. py, enter:
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 ()
5. Set the database. Here, sqlite3 is used as an example to set in settings. py:

Database_engine = 'sqlite3 '# 'postgresql _ psycopg2', 'postgresql ', 'mysql', 'sqlite3 'or 'oracle '.
Database_name = r 'd:/djangoapp/DB/blog. db' # Or path to database file if using sqlite3.
Database_user = ''# Not used with sqlite3.
Database_password = ''# Not used with sqlite3.
Database_host = ''# Set to empty string for localhost. Not used with sqlite3.
Database_port = ''# Set to empty string for default. Not used with sqlite3.
6. Restart the Django server (Python./manage. py runserver) and use:
Python./manage. py syncdb # because the Django database is an ORM Object Relational ing, the corresponding table of the database is created through the classes defined by models. py.
When creating a database, the system will prompt you to set the Django admin super administrator user and password. enter email as the user name.
7. set to start the background of the automatic admin application, in settings. py:
Export age_code = 'zh-cn'

Installed_apps = (
'Django. contrib. auth ',
'Django. contrib. admin ',
'Django. contrib. contenttypes ',
'Django. contrib. session ',
'Django. contrib. Sites ',
'Mysite. blog ',
)
Enter:
Python./manage. py syncdb
Create the django_admin_log table
Modify URLs. py and remove the comment:
# Uncomment the next line to enable the admin:
(R' ^ admin/', include (Admin. Site. URLs )),
8. Modify models. py and register models in Admin.
From Django. DB import models
From Django. contrib import Admin

Class blogpost (models. Model): # inherit from Django Models
Title = models. charfield (max_length = 150)
Body = models. textfield ()
Timestamp = models. datetimefield ()

Admin. Site. Register (blogpost)
9. At 127.0.0.1: 8000/admin/, enter the admin username and password you just set.
Add data in blog posts.
10. Customize the admin background, add blogpostadmin to modify models. py, and register it in Admin:
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 blogpostadmin (Admin. modeladmin ):
List_display = ('title', 'timestamp ')

Admin. Site. Register (blogpost, blogpostadmin)
11. Create a template
In mysite/themes/default/templates/archive.html, use the Django template block tag, which renders the template to each element in the sequence.
{% Extends "base.html" %}
{% Block content %}
{% For post in posts %}
<H2 >{{ post. Title }}</H2>
<P >{{ post. timestamp | Date: "f j, Y" }}</P>
<P >{{ post. Body }}</P>
{% Endfor %}
{% Endblock %}
In base.html, add the following HTML tags:
{% Block content %}
{% Endblock %}
12. Modify view. py, write view functions, and render the template:
# Create your views here.
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 ))

13. Activate the blog URL ing in mysite/URLs. py
Urlpatterns = patterns ('',
# Example:
# (R '^ mysite/', include ('mysite. Foo. urls ')),

# Uncomment the admin/DOC line below and add 'django. contrib. admindocs'
# To installed_apps to enable admin documentation:
# (R '^ admin/doc/', include ('django. contrib. admindocs. urls ')),

# Uncomment the next line to enable the admin:
(R' ^ admin/', include (Admin. Site. URLs )),
(R' ^ blog/', include ('mysite. Blog. urls'), # Add ing
(R' ^ themes /(? P <path>. *) $ ', 'django. Views. Static. serv ',
{'Document _ root': OS. Path. dirname (OS. Path. abspath (_ file _) + '/themes /'}),
)

# (R' ^ themes /(? P <path>. *) $ ', 'django. Views. Static... CSS is invalid because Django's external call is invalid and needs to be called through URL ing. This is mapped to the static project file.
For example, in base.html, <link href = "/themes/default/style.css" rel = "stylesheet" type = "text/CSS"/>
By ing,/themes/will match to the mysite/themes/directory to load the CSS file.

Create mysite/blog/URLs. py
# Coding = UTF-8
From Django. conf. URLs. defaults import *
From mysite. Blog. Views import archive # import view Functions

# Ing URL
Urlpatterns = patterns ('',
(R' ^ $ ', archive ),
)
Enter http: // 127.0.0.1: 8000/mysite/blog
You can see Article Display
I used a free CSS template in base.html. For more information, see the Django Development Guide!

 

Download mysite.rar from the source code

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.