码迷,mamicode.com
首页 > Web开发 > 详细

Django web 开发入门教程 (三)

时间:2015-08-11 21:12:22      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:

reference: https://docs.djangoproject.com/en/1.8/intro/tutorial02/

 This tutorial begins where Tutorial 1 left off. We’re continuing the Web-poll application and will focus on Django’s automatically-generated admin site.

Philosophy

Generating admin sites for your staff or clients to add, change and delete content is tedious work that doesn’t require much creativity. 
For that reason, Django entirely automates creation of admin interfaces for models. Django was written in a newsroom environment, with a very clear separation between “content publishers” and the “public” site. Site
managers use the system to add news stories, events, sports scores, etc., and that content is displayed on the public site. Django solves
the problem of creating a unified interface for site administrators to edit content. The admin isn’t intended to be used by site visitors. It’s for site managers.

Creating an admin user

First we’ll need to create a user who can login to the admin site. Run the following command:

$ python manage.py createsuperuser

Enter your desired username and press enter.

Username: admin

You will then be prompted for your desired email address:

Email address: admin@example.com

The final step is to enter your password. You will be asked to enter your password twice, the second time as a confirmation of the first.

Password: **********
Password (again): *********
Superuser created successfully.

Start the development server

The Django admin site is activated by default. Let’s start the development server and explore it.

Recall from Tutorial 1 that you start the development server like so:

$ python manage.py runserver

Now, open a Web browser and go to “/admin/” on your local domain – e.g., http://127.0.0.1:8000/admin/. You should see the admin’s login screen:

技术分享
Doesn’t match what you see?

If at this point, instead of the above login page, you get an error page reporting something like:
ImportError at /admin/
cannot import name patterns
...

then you’re probably using a version of Django that doesn’t match this tutorial version. You’ll want to either switch to the older tutorial

or the newer Django version.

Enter the admin site

Now, try logging in with the superuser account you created in the previous step. You should see the Django admin index page:

技术分享

 You should see a few types of editable content: groups and users. They are provided by django.contrib.auth, the authentication framework shipped by Django.
 

Make the poll app modifiable in the admin

But where’s our poll app? It’s not displayed on the admin index page.

Just one thing to do: we need to tell the admin that Question objects have an admin interface. To do this, open the polls/admin.py file, and edit it to look like this:
 技术分享
from django.contrib import admin

from .models import Question

admin.site.register(Question)

Explore the free admin functionality

Now that we’ve registered Question, Django knows that it should be displayed on the admin index page:

 技术分享
Click “Questions”. Now you’re at the “change list” page for questions. This page displays all the questions in the database and lets you choose one to
change it. There’s the “What’s up?” question we created in the first tutorial:
技术分享
Click the “What’s up?” question to edit it:
 技术分享
 Things to note here:
  • The form is automatically generated from the Question model.
  • The different model field types (DateTimeField, CharField) correspond to the appropriate HTML input widget. Each type of field knows how to display itself in the Django admin.
  • Each DateTimeField gets free JavaScript shortcuts. Dates get a “Today” shortcut and calendar popup, and times get a “Now” shortcut and a convenient popup that lists commonly entered times.
 The bottom part of the page gives you a couple of options:
  • Save – Saves changes and returns to the change-list page for this type of object.
  • Save and continue editing – Saves changes and reloads the admin page for this object.
  • Save and add another – Saves changes and loads a new, blank form for this type of object.
  • Delete – Displays a delete confirmation page.
If the value of “Date published” doesn’t match the time when you created the question in Tutorial 1, it probably means you forgot to set the correct
value for the TIME_ZONE setting. Change it, reload the page and check that the correct value appears. 
Change the “Date published” by clicking the “Today” and “Now” shortcuts. Then click “Save and continue editing.” Then click “History” in the upper right. You’ll see a page listing all changes made to this object via the Django admin, with the timestamp and username of the person who made the change:
技术分享

Customize the admin form

Take a few minutes to marvel at all the code you didn’t have to write. By registering the Question model with

admin.site.register(Question), Django was able to construct a default form representation. Often, you’ll want to customize how the admin

form looks and works. You’ll do this by telling Django the options you want when you register the object.

Let’s see how this works by re-ordering the fields on the edit form. Replace the admin.site.register(Question) line with:

 技术分享
from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = [pub_date, question_text]

admin.site.register(Question, QuestionAdmin)

You’ll follow this pattern – create a model admin object, then pass it as the second argument to admin.site.register() – any time you need to

change the admin options for an object.

 This particular change above makes the “Publication date” come before the “Question” field:
 技术分享
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Django web 开发入门教程 (三)

标签:

原文地址:http://www.cnblogs.com/BugQiang/p/4721717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!