Django's backend requires only a small amount of code to enable powerful functionality.
This article is tested with Django 1.8.4-bit version, based on Python3.4,ubuntu 14.10. Run as root account. Add a blog post example to the next table.
1. Create a new account called Blog_project, and an app called blog
#django-admin startproject blog_project#cd blog_project#django-admin Startapp Blog
2. Add a blog to the Installed_apps in the setting.py file
#vim blog_project/setting.py
Installed_apps = (' Django.contrib.admin ', ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.contr Ib.sessions ', ' django.contrib.messages ', ' django.contrib.staticfiles ', ' blog ',
3. Modify the models.py in the blog folder to create a database
# Coding:utf-8from DJANGO.DB Import models class article (models. Model): title = models. Charfield (U ' title ', max_length=256) content = models. TextField (U ' content ') Pub_date = models. Datetimefield (U ' Publish Time ', auto_now_add=true, editable = True) Update_time = models. Datetimefield (U ' update Time ', Auto_now=true, null=true) def __str__ (self): #在Python2中用__unicode__替换__str__ retur N Self.title
4. Synchronize all data Sheets
#python3 manage.py syncdb#python3 manage.py makemigrations#python3 manage.py Migrate
Display the following, the yellow mark below is to add the background of the superuser, add your own account is good. Of course, there are other ways to add superuser , which we'll cover in detail below.
#python3 manage.py syncdb
Operations to perform:
Synchronize unmigrated apps:staticfiles, Gunicorn, messages
Apply all migrations:sessions, auth, contenttypes, admin
Synchronizing Apps without migrations:
Creating tables ...
Running Deferred SQL ...
Installing Custom SQL ...
Running Migrations:
Rendering model states ... Done
Applying contenttypes.0001_initial ... Ok
Applying auth.0001_initial ... Ok
Applying admin.0001_initial ... Ok
Applying Contenttypes.0002_remove_content_type_name ... Ok
Applying auth.0002_alter_permission_name_max_length ... Ok
Applying auth.0003_alter_user_email_max_length ... Ok
Applying auth.0004_alter_user_username_opts ... Ok
Applying Auth.0005_alter_user_last_login_null ... Ok
Applying auth.0006_require_contenttypes_0002 ... Ok
Applying sessions.0001_initial ... Ok
You have installed Django ' s auth system, and don ' t has any superusers defined.
Would to create one now? (yes/no): Yes
Username (leave blank to use ' root '): Tu
Email address: [Email protected]
Password:
Password (again):
Superuser created successfully.
# Python3 manage.py makemigrations Django1.7 version above need to run these two commands
Migrations for ' blog ':
0001_initial.py:
-Create Model article
# Python3 manage.py Migrate
Operations to perform:
Synchronize unmigrated apps:messages, Staticfiles, Gunicorn
Apply all Migrations:auth, admin, blog, sessions, ContentTypes
Synchronizing Apps without migrations:
Creating tables ...
Running Deferred SQL ...
Installing Custom SQL ...
Running Migrations:
Rendering model states ... Done
Applying blog.0001_initial ... Ok
5. Add Superuser Account
In addition to the method of adding Superuser accounts by default when synchronizing data above, there are other ways to add them. You need to run the Django command.
(1) Create a new user name, using the following command:
#python3 manage.py Createsuperuser
(2) Enter the login name you intend to use:
Username (leave blank to use ' Administrator '): User01
(3) Enter Email:
Email Address:
(4) Enter the password, you need to enter two times, the password is not displayed during the input process:
Password:
Password (again):
(5) When the password is the same two times, you will be prompted to create a successful superuser.
Superuser created successfully
6. Modify admin.py
Go to the Blog folder, modify the admin.py file, edit the content as follows:
From Django.contrib import adminfrom. Models import Articleadmin.site.register (article)
Just three lines of code, you can create a powerful background! At the same time, the URL of admin in urls.py has been turned on by default, so start the server, you can access the background.
#python3 manage.py Runserver
Visit http://localhost:8000/admin/
Enter the Superuser account password previously set, you can log in the background.
7. When using Nginx to deploy Django, there are times when background style loss occurs. For example:
This behavior occurs because Nginx configures static address errors. Enter the/etc/nginx/sites-available/default file, and add:
location/static/{
alias/usr/local/lib/python3.4/dist-packages/django/contrib/admin/static/;
}
This refreshes the page and displays a background page with CSS styles.
Django Backend: A small amount of code for a powerful website backend