Django後台:少量代碼,實現強大的網站後台

來源:互聯網
上載者:User

標籤:

Django的後台只需要少量代碼,就可以實現強大功能.
本文章以Django 1.8.4位版本測試,基於python3.4,Ubuntu 14.10.以root賬戶運行.以後台添加部落格位例子.

1.建立一個名稱為blog_project的賬戶,和名為blog的app

#django-admin startproject blog_project#cd blog_project#django-admin startapp blog

2.將blog添加到setting.py檔案中的INSTALLED_APPS中

#vim blog_project/setting.py
INSTALLED_APPS = (    ‘django.contrib.admin‘,    ‘django.contrib.auth‘,    ‘django.contrib.contenttypes‘,    ‘django.contrib.sessions‘,    ‘django.contrib.messages‘,    ‘django.contrib.staticfiles‘,    ‘blog‘,)

3.修改blog檔案夾中的models.py,建立資料庫

# coding:utf-8from django.db import models class Article(models.Model):        title = models.CharField(u‘標題‘, max_length=256)        content = models.TextField(u‘內容‘)         pub_date = models.DateTimeField(u‘發表時間‘, auto_now_add=True, editable = True)        update_time = models.DateTimeField(u‘更新時間‘,auto_now=True, null=True)    def __str__ (self):#在Python2中用__unicode__替換__str__                return self.title

4.同步所有的資料表

#python3 manage.py syncdb#python3 manage.py makemigrations#python3 manage.py migrate

顯示以下內容,下面黃色標記的部分是添加背景superuser,添加你自己的帳號就好.當然添加superuser還有別的方法,下面我們會詳細介紹.

#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 have any superusers defined.
Would you like 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版本以上需要運行這兩個命令

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.添加superuser賬戶

除了上面同步資料時預設添加superuser賬戶的方法外,還有其他的添加方法.需要運行Django命令.

(1)建立一個使用者名稱,使用如下命令:

#python3 manage.py createsuperuser

(2)輸入打算使用的登入名稱:

Username(leave blank to use ‘administrator‘):user01

(3)輸入email:

Email address:

(4)輸入密碼,需要輸入兩次,輸入過程中密碼不顯示:

Password:

Password(again):

(5)當兩次密碼都相同的時候,就會提示superuser建立成功。

Superuser created successfully

6.修改admin.py

進入blog檔案夾,修改admin.py檔案,編輯內容如下:

from django.contrib import adminfrom .models import Articleadmin.site.register(Article)

只需要這三行代碼,就能建立強大的後台!同時,urls.py中關於admin的url已經預設開啟,所以啟動伺服器,就可以訪問後台了.

#python3 manage.py runserver

訪問http://localhost:8000/admin/

輸入之前設定的superuser的帳號密碼,就能登入後台了.

7.使用nginx部署Django時,有時候會出現後台樣式丟失的情況.比如:

出現這種情況的原因是,Nginx配置靜態地址錯誤.進入/etc/nginx/sites-available/default檔案,添加:

location /static/ {
            alias /usr/local/lib/python3.4/dist-packages/django/contrib/admin/static/; 

}

這樣重新整理頁面,就會顯示帶有CSS樣式的後台頁面.







Django後台:少量代碼,實現強大的網站後台

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.