Django Tutorial Part2

Source: Internet
Author: User
Tags django tutorial

Writing_first_django_app_part2

Create Super User

Start creating a manager account below:

$ python manage.py createsuperuser

Enter user name and password, create account after mailbox

After running Runserver, enter http://127.0.0.1:8000/admin/to login to the admin interface

After logging in the management interface has not yet seen our poll application, we need to tell admin our question object needs to display in the management interface, modify the polls/admin.py:

From Django.contrib import adminfrom polls.models import Questionadmin.site.register (Question)

After restarting the server, you'll see questions.

Customize the Admin form

Just now the register () function is simply registering the question class, adding a Questionadmin class in the inside to customize the admin form and modify the polls/admin.py:

From Django.contrib import adminfrom polls.models import questionclass questionadmin (admin. Modeladmin): Fields    = [' pub_date ', ' Question_text ']admin.site.register (question, questionadmin)

In the register () function, add one more parameter to the Questionadmin class, by admin. The Modeladmin class inherits, and the above changes will arrange the classes according to the order of the fields list.

Modify the polls/admin.py to break the table into FieldSets:

From Django.contrib import adminfrom polls.models import questionclass questionadmin (admin. Modeladmin):    fieldsets = [        (None,               {' Fields ': [' Question_text ']}),        (' Date information ', {' Fields ': [' Pub_date ']}),    ]admin.site.register (Question, Questionadmin)

The first element of each tuple in the list is the title of this fieldset

Collapse sometimes some fieldset have a lot of content that is not commonly used and can be initialized to shrink:

From Django.contrib import adminfrom polls.models import questionclass questionadmin (admin. Modeladmin):    fieldsets = [        (None,               {' Fields ': [' Question_text ']}),        (' Date information ', {' Fields ': [' Pub_date '], ' classes ': [' Collapse ']}),    

Just add a ' classes ' to that fieldset tuple: [' collapse '], so you can see it by clicking Expand when you need to see the content

Now we have the question administration page, but we have not yet shown the choices of each question

The first method can also register choice like question, but each choice is associated with a question, so the management is inconvenient, so you can add the choice in inline mode:

# polls/admin.pyfrom Django.contrib Import adminfrom polls.models import Choice, Questionclass choiceinline (admin. Stackedinline):    model = Choice    extra = 3class questionadmin (admin. Modeladmin):    fieldsets = [        (None,               {' Fields ': [' Question_text ']}),        (' Date information ', {' Fields ': [' Pub_date '], ' classes ': [' Collapse ']},    ]    inlines = [Choiceinline]admin.site.register (Question, Questionadmin)

The Choiceinline class will tell Django to edit choice on the admin page of question, which provides 3 locations by default, and we can manually increase the quantity or reduce

Customize the admin look and feelcustomizing project ' s templates

First create the templates directory at the root of the project, and then add the template directory to the mysite/settings.py:

Template_dir = [Os.path.join (base_dir, ' templates ')]

Then create the directory admin under the Templates directory and copy the System Django directory (django/contrib/admin/templates/admin/base_site.html) file into the new admin directory. If you can't find the system Django directory, you can run the following command:

$ python-c "Import Syssys.path = Sys.path[1:]import djangoprint (django.__path__)"

Then modify the template, here we will manage the page above the title to "polls administration", modify the template code, replace the original{{site_header|default:_(‘Django administration‘)}}

{% block branding%}

Here Django's default Administrative Templates can be overloaded, just like base_site.html, copying the original file into the new template folder and making modifications

Django Tutorial Part2

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.