Actual combat django: Simple Blog Part1

Source: Internet
Author: User

Willing to learn new skills, usually do not like silly reading-holding a book to elaborate on the skills, and then silly look, all the way down, the brain stuffed with new concepts, knowledge points, the head is more and more big, but the skills have barely mastered half points.

Over the years, I have been willing to develop the habit of learning new skills as examples. This is not willing to the original, in chess, go in the field, has always been "replay" A said, is based on the game, a step-by-step chess out, in the process of replay, to sentiment, to absorb, at this time to comprehend, absorbed into the, is your own things. The new skills developed gradually and grew.

Just like the Django Official document, an instance looks simple, but as a beginner, using the "replay" way to do this example over and over, compared to the book to pick up from the end of the silly reading, the progress is not more than one or two times times higher speed?

Therefore, in the introduction of new skills in books, willing to favor those who use examples to preach, tuition, the book of doubts.

The Django Web Development Guide (Python Web Development with Django) is one of the few Django boutique books that was published in 2009 and was introduced to China in the same year by the mechanical industry press. The vast majority of the book uses examples to illustrate the use of Django, which is just the type of appreciation. Unfortunately, the Django used at the time was version 1.0, and the code was somewhat different from the Django 1.7.1 we're using now. For new people, practice is rather troublesome. Be willing to rearrange the examples in the book, and use the Django 1.7.1 standard to fix the code to make it easier for beginners to use these examples to learn Django.

Today is willing to talk about, is the first example of the book: blog. This example is very simple, the basic operation and the official example is very similar, we can use this example to review the knowledge learned earlier.

0. Before you hit the road

Willing to strongly recommend that you do one by one to complete the details of this example. If your first contact with Django, it is recommended that you first make an example in the "Live Django: official instance" series, where the most basic techniques of Django are presented.

In the case of an instance, if you encounter a situation that differs from what you see here, stop to check the previous steps and then review the two to three steps. See if there is something that doesn't seem important, or what steps you don't understand are missing out. If it is still not found, it is better to delete the whole project from scratch, this method works very well.

1. Create Projects and Apps

Let's start by creating the project for this instance, go to the Scripts folder (such as "c:\python32\Scripts") at the DOS command prompt, and then run the following command:

$ django-admin Startproject Blogproject

Without any error, the project was created successfully, and then at the DOS command prompt, enter the command to enter the project folder:

CD Blogproject

To start creating an app, enter the command at the DOS command prompt:

$ python manage.py Startapp Blog

After the command executes, the Blogproject folder will be more than a folder called blog, the application is built.

2. Building a model

Edit the blog/models.py file as follows:

Blog/models.py:

 from Import Models  from Import Admin class blogpost (models. Model):    = models. Charfield (max_length=150)    = models. TextField ()    = models. Datetimefield ()    class  Meta:        = ('-timestamp' ,)

3. Activating the Model

First modify the blogproject/settings.py This file, find the Installed_apps this setting, change it to look like this:

blogproject/settings.py:

Installed_apps = (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Blog',)

When editing the settings.py, it is recommended to modify the language and time zone settings, the specific method, please refer to: "Actual Django: official instance Part1"

Then run the following command at the DOS command prompt:

$ python manage.py makemigrations Blog

To continue running the command at a DOS command prompt:

$ python manage.py Migrate

In this way, the database is built.

4. Create an Administrator account

Run the following command at a DOS command prompt:
$ python manage.py createsuperuser

Then enter the admin, your mailbox, enter the password two times to complete the operation of creating the administrator.

5. Registering apps in the admin interface

Edit the blog/admin.py file to make it look like this:

blog/admin.py:

 from Import Admin  from Import BlogPostadmin.site.register (blogpost)

6. Start the server

Run the following command at a DOS command prompt:

$ python manage.py runserver

After the command executes, you will see a character like this appear:

December, 2014-18:56:24'blogproject.settings'starting development Server at http://127.0.0.1:8000/Quit the server with CTRL-break.

This means that the server has successfully started.

We will first visit the management interface, open the browser, in the Address bar to enter:

http://127.0.0.1:8000/admin/

Then enter the admin account and password you just created, log in to the admin interface, and you'll see a screen like this:

If you see something different than what is shown here, go back and check the previous action.

Tips: The three most common reasons why you can't see my app in the Admin interface:1. No model was registered in the app's admin.py;2. The applied model configuration error;3. Forget to add your app to the aettings.py Installed_apps of the project;
7. Add an article

Click the "Add" button on the right side of the blog posts, and the admin screen will display a form that lets you add articles:

Fill in some content to try, fill out and click the "Save" button.

You will receive a confirmation message: "Blogpost object" was added successfully. At the same time Django lists your list of articles, and there is currently only one post.

Seeing the ugly name like blogpost object again, recall how we handled it in the actual Django: official instance Part2?

Edit the blog/admin.py file to make it look like this:

blog/admin.py:

 from Import Admin  from Import blogpost class blogpostadmin (admin. Modeladmin):    = ('title' 'timestamp' ) Admin.site.register (blogpost, blogpostadmin)

To refresh the browser, we can see:

Here, we can use the skills learned in the official examples, to add to the Management page search, custom sorting, filtering and other functions. Willing to no longer explain these questions, we practice it by ourselves.

"Not to be Continued"

This article copyright to willing to learn all, welcome reprint, reproduced please indicate the author and source. Thank you!
Willing
Starter: Willing to learn Yuan @ Blog Park

Actual combat django: Simple Blog Part1

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.