Django Learning (3)-writing the first application (APP) and the second part

Source: Internet
Author: User

This articleArticleFollowed by the first part, original address http://docs.djangoproject.com/en/1.2/intro/tutorial02/#intro-tutorial02

1. Activate the admin site.

Three things are required to activate the admin site

1> Add "Django. contrib. admin" to installed_apps of settings. py"

2> run the manage. py syncdb command.

3> edit the mysite/URLs. py file and remove the comments in the following three lines.

 

 
From Django. conf. URLs. defaults import * # uncomment the next two lines to enable the admin: # from Django. contrib import admin # admin. autodiscover () urlpatterns = patterns ('', # uncomment the next line to enable the admin: # (r '^ admin/', include (Admin. site. URLs )),)

2> run the manage. py runserver 80 command to start the server. in the address bar, enter http: // 127.0.0.1/admin/. The admin logon interface is displayed.

If you log on with the super user name and password in the first part (if the first part is not created, you can also use manage. py createsuperuser to create a user logon ). The admin homepage of Django is as follows:

3. Add our poll app to the admin management interface.

In the mysite/polls directory, create the admin. py file so that the content is as follows:

 

 
# Coding: cp936from mysite. Polls. Models import poll, choicefrom Django. contrib import adminadmin. Site. Register (poll) Admin. Site. Register (choice)

At this time, the corresponding poll and choice will appear in the admin Management Interface

Here we can add, query, modify, and delete our poll and choice.

4. Admin form restructured (customized)

Modify the mysite/polls/admin. py file to make it look as follows:

 
# Coding: cp936from mysite. polls. models import poll, choicefrom Django. contrib import adminclass polladmin (Admin. modeladmin): fields = ["pub_date", "Question"] admin. site. register (poll, polladmin)

Before Modification

Let's see the modified results.

Let's continue to modify it with fieldset:

# Coding: cp936from mysite. polls. models import poll, choicefrom Django. contrib import adminclass polladmin (Admin. modeladmin): fieldsets = [(none, {'fields': ['Question ']}), ('date information', {'fields ': ['pub _ date']}),] admin. site. register (poll, polladmin)

Then let's look at the effect:

Django also provides an additional class in fieldsets.

 
# Coding: cp936from mysite. polls. models import poll, choicefrom Django. contrib import adminclass polladmin (Admin. modeladmin): fieldsets = [(none, {'fields': ['Question ']}), ('date information', {'fields ': ['pub _ date'],"Classes": ['collapse']}),] Admin. Site. Register (poll, polladmin)

View results

 

5. Add correlated object

Continue to modify Admin. py,

 

From mysite. Polls. Models import choiceadmin. Site. Register (choice)

 

In the form above, the poll drop-down list box is all poll in the database, and Django knows to use <SELECT> to display foreignkey in Admin.

When we click "add another", a "add poll" window will pop up. if you add a poll in the pop-up window, Django will save the poll to the database and use it as the poll associated with choice.

This display method is different from our daily life. We usually list poll and the choice information below. Change Admin. py.

 
# Coding: cp936from mysite. polls. models import poll, choicefrom Django. contrib import adminclass choiceinline (Admin. stackedinline): Model = choice # extra is the number of extra lists. Extra = 1 class polladmin (Admin. modeladmin): fieldsets = [(none, {'fields': ['Question ']}), ('date information', {'fields ': ['pub _ date'], "classes": ['collapse']}),] inlines = [choiceinline]

Let's look at the effect directly:

You can also replace stackedinline in Admin. py with tabularinline to see the effect.

6. Customize admin change list

First, let's take a look at what it looks like before the change.

Modify Admin. py

 
Class polladmin (Admin. modeladmin): ...... list_display = ("Question", "pub_date", "was_published_today ")

View results

You can click the column header to sort by them, but was_published_today is an exception because Django does not know how to sort by method yet. At the same time, we also noticed that the was_published_today column header is the method name by default, but we can change this default type by giving this method a short_description attribute in models. py.

 
Class poll (models. model ):........... def was_published_today (Self): return self. pub_date.date () = datetime. datetime. today () was_published_today.short_description = "published today? "

We can also add other options to customize the change list.

For example, search_fields = ['question',], list_filter = ["pub_date "],CodeAdd to mysite/polls/admin. py

 
List_filter = ['pub _ date',] search_fields = ['Question ',] date_hierarchy = "pub_date"

The effect is as follows:

 

7. Define admin appearance

If we also display "Django administration" on our application management interface, it will be funny. Now let's modify it here. We need to do two things.

1> modify template_dirs in settings. py. template_dirs are the tuples of some directories. These directories are the search paths of Django templates.

Template_dirs to make the content as follows:

 

  Template_dirs=(
"D:/mysite/templates",
)

2> copy the base_site.html file (Django/contrib/admin/templates/admin/In the Django installation path) to the D: \ mysite \ Templates \ ADMIN directory (note, here is an admin subdirectory ). Edit the base_site.html file.

View results:

At the same time, we can also customize the admin index page. What we need to do this time is to copy Django/contrib/admin/templates/admin/index.html to D: \ mysite \ Templates \ admin, then edit. There is an app_list in the original file, and app_list is all apps installed in Django.

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.