django 中 manage.py通常使用的各種命令大全


django 中 manage.py通常使用的各種命令大全(包含django 安裝指導及測試)

原文:http://blog.csdn.net/qq287156351/article/details/9530567

命令執行、錯誤修復常用命令

1,python manage.py syncdb。本命令會修復SQL的匹配問題,同步數據庫,生成管理界面使用的額外的數據庫表。例如:

 

[python]  view plain copy
 
 
 
  1.   
[plain]  view plain copy
 
 
 
  1. <span style="font-size: 12px; font-weight: normal;">X:\website\mysite\mysite>python manage.py syncdb  
  2. Creating tables ...  
  3. Creating table book_publisher  
  4. Creating table book_author  
  5. Installing custom SQL ...  
  6. Installing indexes ...  
  7. Installed 0 object(s) from 0 fixture(s)</span>  
 
          

 

 
          

 

2,manage.py sqlall book  。查看book這個app下所有的SQL表。例如:

 

[python]  view plain copy
 
 
 
  1. <span style="font-size: 12px; font-weight: normal;">X:\website\mysite\mysite>manage.py sqlall book  
  2. BEGIN;  
  3. CREATE TABLE `book_publisher` (  
  4.     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,  
  5.     `name` varchar(30) NOT NULL,  
  6.     `address` varchar(50) NOT NULL,  
  7.     `city` varchar(60) NOT NULL,  
  8.     `state_province` varchar(30) NOT NULL,  
  9.     `country` varchar(50) NOT NULL,  
  10.     `website` varchar(200) NOT NULL  
  11. )  
  12. ;  
  13. CREATE TABLE `book_author` (  
  14.     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,  
  15.     `first_name` varchar(30) NOT NULL,  
  16.     `last_name` varchar(40) NOT NULL,  
  17.     `email` varchar(75) NOT NULL  
  18. )  
  19. ;  
  20.   
  21. COMMIT;</span>  

 

 

3,python manage.py dbshell .本命令會顯示數據庫版本信息(粗藍色字體)、數據條目(紅色字體)。並在命令下啟動數據庫的命令行工具.

 

[plain]  view plain copy
 
 
 
  1. X:\website\mysite\mysite>python manage.py dbshell  
  2. Warning: Using a password on the command line interface can be insecure.  
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 162  
  5. Server version: 5.6.12 MySQL Community Server (GPL)  
  6.   
  7. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.  
  8.   
  9. Oracle is a registered trademark of Oracle Corporation and/or its  
  10. affiliates. Other names may be trademarks of their respective  
  11. owners.  
  12.   
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  14.   
  15. mysql>  


 

django 管理界面的建立和命令排序

 
          
[python]  view plain copy
 
 
 
  1.   

 

 

 

1.創建Project為testadmin

django-admin.py startproject testadmin
 

文檔結構如下:

復制代碼
D:\DJCODE\TESTADMIN
|   manage.py
|
\---testadmin
        settings.py
        urls.py
        wsgi.py
        __init__.py
復制代碼

2.配置數據庫(在postgreSQL中用pgAdmin新建了一個數據庫django)

復制代碼
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'django',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'postgres',
        'PASSWORD': '911027',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}
復制代碼

3.創建一個應用為book

python manage.py startapp book

在book文件夾中找到models.py文件,將其代碼修改為第六、七章中的代碼即可,文件中代碼如下:

復制代碼
from django.db import models
class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()
class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
復制代碼

然后配置app,將其添加到settings.py文件相應的地方,如:

復制代碼
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'book',#添加這一項,和前面那章不同,前面的是books,注意別搞錯了
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)
復制代碼

最后驗證模型並且執行代碼。

D:\Djcode\testadmin>python manage.py validate
0 errors found
復制代碼
D:\Djcode\testadmin>python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table book_publisher
Creating table book_author

You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): 
復制代碼

然后再按照下圖操作即可。

創建后的auth的用戶名為:django 密碼為:911027

說明:為什么直接跳到Auth的創建過程了呢?因為在settings.py中INSTALLED_APP里面並沒有注釋掉:

    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

只要在其前面加上#號即可。

4.配置admin和url

將settings.py中的INSTALLED_APP中的admin選項前面的#好去掉。

 

    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',

 

更改urls.py文件代碼如下:

復制代碼
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin#將注釋取消
admin.autodiscover()#將注釋取消

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testadmin.views.home', name='home'),
    # url(r'^testadmin/', include('testadmin.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),#將注釋取消

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),#將注釋取消
)
復制代碼

輸入:http://127.0.0.1:8000/admin/ 訪問后看到如下界面。

輸入用戶名django和密碼911027即可登陸。

 當然如果你的母語不是英語,而你不想用它來配置你的瀏覽器,你可以做一個快速更改來觀察Django管理工具是否被翻譯成你想要的語言。 僅需添加django.middleware.locale.LocaleMiddleware 到settings.py文件中MIDDLEWARE_CLASSES設置中,並確保它在django.contrib.sessions.middleware.SessionMiddleware之后。

5.將Models添加到Admin管理界面中

在上一步進入到Admin管理界面后,你只能看到幾項簡單的操作,其實我們最重要的是對book這個app進行操作,比如添加相關的信息等,那么我們就需要將其注冊到Admin管理界面中來。具體如下:

在book目錄下創建admin.py文件,其代碼如下:

from django.contrib import admin
from book.models import Publisher, Author #這里需要特別注意,此處是book而不要因為看到別的書寫成mysite.book
admin.site.register(Publisher)
admin.site.register(Author)

完成后重啟web服務,在登陸:http://127.0.0.1:8000/admin/ 界面,你會注意到多了點什么呢?如下圖:

 

 
 

 

django 的使用注意:


1,數據庫名稱與Key有對應關系,與APP有對應關系。

2,每次“  django-admin.py startproject  ***** ”中創建的KEY不同。

      所以用不同的project不能用來登錄不同的DB(數據庫)。

      提示:OperationalError: (1045, "Access denied for user 帳號名稱'@'localhost' (using password: YES)")

3, 如果不同的project需要登錄以前的project的DB,需要先python manage.py changepassword  ××× 。
 
   或者使用命令行工具python manage.py syncdb  進行同步【(可能的情況下)。但——通常是路徑和設置問題)】。
 
4,網上的文章大多抄襲,請不要相信,最好相信自己摸索,一步一步,步步為營。
 
5,books管理者(admin)文件夾應處於和settings.py同一級別根目錄下。
 

manage.py通常使用的命令




[auth]
python manage.py changepassword  ××× (改變密碼××代表已經有的賬戶)
python manage.py createsuperuser  ×××  (創造賬戶××代表已經想創造的新賬戶)




[django]
python manage.py    base
python manage.py    cleanup
python manage.py    compilemessages
python manage.py     createcachetable
python manage.py     dbshell
python manage.py     diffsettings
python manage.py    dumpdata
python manage.py     flush
python manage.py    inspectdb
python manage.py     loaddata
python manage.py     makemessages
python manage.py     runfcgi
python manage.py     shell
python manage.py    sql
python manage.py    sqlall
python manage.py    sqlclear
python manage.py    sqlcustom
python manage.py     sqlflush
python manage.py     sqlindexes
python manage.py     sqlinitialdata
python manage.py     sqlsequencereset
python manage.py     startapp
python manage.py     startproject
python manage.py     syncdb
python manage.py    test   (測試中將建造默認:Creating test database for alias 'default'.................................................需要1分鍾左右,需要看計算機配置情況)
python manage.py     testserver
python manage.py     validate  (可以驗證安裝錯誤,安裝正確則顯示:0 errors found)

python  makelocalealias.py

python  msgfmt.py

python  pygettext.py



[sessions]
python manage.py     clearsessions




[staticfiles]
 python manage.py    collectstatic
python manage.py     findstatic

python manage.py     runserver   運行

 

django 安裝完成后,在數據庫安裝好后,請進行測試,python manage.py    test   如下顯示錯誤數據,方便調試:

 

FAIL: test_clearsessions_command (django.contrib.sessions.tests.FileSessionTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "X:\python27\django\test\utils.py", line 220, in inner
    return test_func(*args, **kwargs)
  File "X:\python27\django\contrib\sessions\tests.py", line 444, in test_clearsessions_command
    self.assertEqual(1, count_sessions())
AssertionError: 1 != 2


----------------------------------------------------------------------
Ran 478 tests in 78.472s


FAILED (failures=1, skipped=2, expected failures=1)
Destroying test database for alias 'default'...


X:\website\mysite>

 

 

Django開發中常用的命令總結

                                                                       2013-01-29 15:10:21   來源:  下文來自 互聯網絡
1. 創建一個Django Project#使用下面的命令可以創建一個projectdjango-admin.py startproject mysite #創建好之后可以看到如下的pro...

1. 創建一個Django Project

1
2
3
4
5
6
7
8
9
10
11
#使用下面的命令可以創建一個project
django-admin.py startproject mysite
   
#創建好之后可以看到如下的project結構
mysite/
   manage.py
   mysite/
       __init__.py
       settings.py
       urls.py
       wsgi.py

2. 啟動剛剛創建的Project

進入mysite目錄,並運行python manage.py runserver命令。默認情況下runserver的啟動端口是8000,如果需要更改端口號,可以將其以參數的形式傳進去

1
python manage.py runserver 8080

3. 啟動交互式的命令模式

通常需要測試一些簡單的Django代碼,這時就可以使用這種交互式的shell來完成

1
python manage.py shell

4. 創建Django App

1
python manage.py startapp books
1
2
3
4
5
6
# 創建好的App目錄結構如下
books /
   __init__.py
   models.py
   tests.py
   views.py

5. 校驗Model的有效性

通常為了連接數據庫,我們需要創建與數據庫表相對應的Model,當Model創建好之后可以使用下面的命令來校驗Model的有效性

1
python manage.py validate

如果看到了如下的輸出信息,表示你的Model沒有問題

0 errors found

6. 生成SQL schema

確認Model沒有問題時候,Django為我們提供了一個工具幫助生成創建數據庫的schema

1
python manage.py sqlall books

這個命令可以將創建Table的shema輸出到命令行,但是不能將其同步創建到數據庫,為了將其同步到數據庫中,Django也為我們考慮到了

7. 同步Model到數據庫

1
2
3
python manage.py syncdb
# Django 還提供了另一個工具方便我們直接登錄到數據庫中
python manage.py dbshell
 

 

django命令解釋

 

 

 

Django是一個python用於快速開發web應用的框架,它的很多特性使用極其方便快捷。當創建一個django項目和對項目進行管理的時候,會涉及到很多命令行命令。本文對其進行一些總結,以供方便查詢。

django-admin.py startproject mysite

該命令在當前目錄創建一個 mysite 目錄。

django-admin.py這個文件在C:\Python27\Lib\site-packages\django\bin文件夾里,可以把該目錄添加到系統Path里面。

Django內置一個輕量級的Web服務器。

進入 mysite 目錄的話,現在進入其中,並運行 python manage.py runserver 命令

啟動服務器,用http://127.0.0.1:8000/可以進行瀏覽了,8000是默認的端口號。

python manage.py runserver 8080

更改服務器端口號

python manage.py shell

啟動交互界面

python manage.py startapp books

創建一個app,名為books

python manage.py validate

驗證Django數據模型代碼是否有錯誤

python manage.py sqlall books

為模型產生sql代碼

python manage.py syncdb

運行sql語句,創建模型相應的Table

python manage.py dbshell

啟動數據庫的命令行工具

manage.py sqlall books

查看books這個app下所有的表

python manage.py syncdb

同步數據庫,生成管理界面使用的額外的數據庫表

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM