标签:
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.
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.
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.
Now, try logging in with the superuser account you created in the previous step. You should see the Django admin index page:
But where’s our poll app? It’s not displayed on the admin index page.
from django.contrib import admin from .models import Question admin.site.register(Question)
Now that we’ve registered Question, Django knows that it should be displayed on the admin index page:
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.
标签:
原文地址:http://www.cnblogs.com/BugQiang/p/4721717.html