您的位置:首页 > 编程语言 > Go语言

django 中 manage.py通常使用的各种命令大全(包含django 安装指导及测试)

2015-06-08 16:24 941 查看

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

命令执行、错误修复常用命令

1,python
manage.py syncdb。本命令会修复SQL的匹配问题,同步数据库,生成管理界面使用的额外的数据库表。例如:

[python] view
plaincopy

[plain] view
plaincopy

<span style="font-size: 12px; font-weight: normal;">X:\website\mysite\mysite>python manage.py syncdb

Creating tables ...

Creating table book_publisher

Creating table book_author

Installing custom SQL ...

Installing indexes ...

Installed 0 object(s) from 0 fixture(s)</span>





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

[python] view
plaincopy

<span style="font-size: 12px; font-weight: normal;">X:\website\mysite\mysite>manage.py sqlall book

BEGIN;

CREATE TABLE `book_publisher` (

`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,

`name` varchar(30) NOT NULL,

`address` varchar(50) NOT NULL,

`city` varchar(60) NOT NULL,

`state_province` varchar(30) NOT NULL,

`country` varchar(50) NOT NULL,

`website` varchar(200) NOT NULL

)

;

CREATE TABLE `book_author` (

`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,

`first_name` varchar(30) NOT NULL,

`last_name` varchar(40) NOT NULL,

`email` varchar(75) NOT NULL

)

;

COMMIT;</span>

3,python manage.py dbshell .本命令会显示数据库版本信息(粗蓝色字体)、数据条目(红色字体)。并在命令下启动数据库的命令行工具.

[plain] view
plaincopy

X:\website\mysite\mysite>python manage.py dbshell

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 162

Server version: 5.6.12 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

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


[python] view
plaincopy

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


2. 启动刚刚创建的Project

进入mysite目录,并运行python manage.py runserver命令。默认情况下runserver的启动端口是8000,如果需要更改端口号,可以将其以参数的形式传进去


3. 启动交互式的命令模式

通常需要测试一些简单的Django代码,这时就可以使用这种交互式的shell来完成


4. 创建Django App


5. 校验Model的有效性

通常为了连接数据库,我们需要创建与数据库表相对应的Model,当Model创建好之后可以使用下面的命令来校验Model的有效性

如果看到了如下的输出信息,表示你的Model没有问题

0 errors found


6. 生成SQL schema

确认Model没有问题时候,Django为我们提供了一个工具帮助生成创建数据库的schema

这个命令可以将创建Table的shema输出到命令行,但是不能将其同步创建到数据库,为了将其同步到数据库中,Django也为我们考虑到了


7. 同步Model到数据库

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

同步数据库,生成管理界面使用的额外的数据库表

Thank to,

1,http://www.crazyant.net/2012/07/04/python%E4%BD%BF%E7%94%A8cookielib%E5%92%8Curllib2%E6%A8%A1%E6%8B%9F%E7%99%BB%E9%99%86%E6%96%B0%E6%B5%AA%E5%BE%AE%E5%8D%9A%E5%B9%B6%E6%8A%93%E5%8F%96%E6%95%B0%E6%8D%AE/

2,http://www.phodal.com/about/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: