简介:许多朋友询问有关如何将Django文件发布到公共网络的问题。本文的首席CTO笔记将为您提供详细的答案,以供所有人参考。我希望这对每个人都会有所帮助!让我们一起看看!
worlds = requests.post(path,data = {'document_name':document_path,'file':base64.b64encode(file.read())})此方法实际上是将base64的文件信息和文件发送到服务器。侧面,有以下方法沉积在服务器中
如何将文件存储在当地
这个问题不是文件上传的问题,而是一个非常简单的文件编码。
这在python中很困惑。法定错误。您必须了解django默认为unicode。如果您使用str类型。它将自动转换您。这是python的自动转换。GBK,有时会出错。
您的环境正是Windows。英语('utf-8')英语没有问题。
我建议您在文件前面添加一个编码:UTF-8
然后将此句子更改为
断言(type(file_name)== unicode)
fpath = u'd:/ texthub/texthub/site_media/upload/'+file_name.encode('gb18030')
自己尝试。
使用mod_python部署
1.为了基于mod_python配置django,首先
apache。
2.然后在Apache配置文件中应该有一个LoadModule指令。看起来像这样:
loadModule python_module/usr/lib/apache2/modules/mod_python.so
3.配置Apache将请求URL定位到Django应用程序:
VirtualHost *:80
服务器名称
本地“/python1”
Sethandler Python -Program
pythonhandler django.core.handlers.modpython
setEnv django_settings_module python1.settings
Pythonautoreload Off
pythondebug off
pythonpath“ ['/var/www/html/python1'] + sys.path”
Pythonininterpreter Python1
/当地的
本地“/python2”
Sethandler Python -Program
pythonhandler django.core.handlers.modpython
setEnv django_settings_module python2.settings
Pythonautoreload Off
pythondebug off
pythonpath“ ['/var/www/html/python2'] + sys.path”
Pythonininterpreter Python2
/当地的
[......]
/VirtualHost
它告诉Apache, / python之后的任何URL都使用Django的mod_python对其进行处理。
它传递了django_settings_module的值,以便mod_python知道此时应使用哪种配置。
检查mod_python文档以获取详细的说明列表。
4.重新启动apache并查看:“ target =“ _ blank”:
/etc/init.d/apache2 restart
使用mod_wsgi部署
1.下载并安装mod_wsgi模块以生成mod_wsgi.so和wsgi.conf
2.将模块加载到配置中:
loadModule python_module/usr/lib/apache2/modules/mod_wsgi.so
3.修改Apache配置文件httpd.conf
VirtualHost *:80
服务器名称
document root/var/www/html/python
wsgiscriptalias // var/www/html/python/apache/django.wsgi
目录 /
命令拒绝,允许
从所有人那里允许
/目录
目录 /apache
从所有人那里允许
/目录
/VirtualHost
4.创建和配置WSGI配置文件:
#文件名:python.apache.django.wsgi
导入OS,系统
#calculcul callcul the Lath基于WSGI脚本的位置。
apache_configuration = os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(工作区)
os.environ ['django_settings_module'] ='python.settings'
os.environ ['python_egg_cache'] ='/tmp'
导入django.core.handlers.wsgi
应用程序= django.core.handlers.wsgi.wsgihandler()
打印sys.stderr,ss.path
shellchmod a+x django.wsgi
5.修改django项目配置文件设置.py:
数据库= {
'默认': {
'引擎':'django.db.backends.mysql',
“名字”:'Python',
“用户”:“ admin”,
'密码':'admin123',
“主机”:'127.0.0.1',
'端口':'3306',},}
}
template_dirs =((
'/var/www/html/python/pyplates',,
治愈
6.重新启动Apache,访问
/etc/init.d/apache2 restart
步骤1:注册SINA SAE帐户并下载Tortoisesvn。
步骤2:创建一个新应用程序。
注册应用程序名称,注册第二级域名,选择开发语言的Python2.7版本,然后输入应用程序管理接口。第一个是提前初始初始化的。
然后单击代码管理以创建一个版本,版本号为1,创建成功后,您可以看到类似的信息。
步骤3:使用SVN部署代码。
首先在正确的位置创建一个SVN文件夹,例如E:SVN。
然后,在SVN下方创建一个文件夹,该名称是创建应用程序(例如Pylabsite)时的第二个级别域名中填充的名称。
右 - 单击文件夹Pylabsite,选择SVN结帐,填写URL,其他默认值,单击“确定”,然后填写注册的电子邮件和密码,然后在填写时选择“保存身份验证”。没有悬念。
超过
完成后,Pylabsite文件夹中将还有两个文件夹,1和.svn。
在目录中,只有两个文件:config.yaml和index.wsgi,然后将本地project Pylabsite复制到文件夹中。由于SAE最支持
django1.4版本,这是Python安装位置的Django包(例如,C:Python27libsite-packages),和
创建一个新的站点包装文件夹,放置在其中,文件目录结构如下:
本地文件是这样的:
版本:1
然后修改config.yaml和index.wsgi的两个文件。
#- config.yaml-#
名称:Pylabsite
版本:1
库:
- 名称:“ django”
版本:“ 1.6”
处理者:
-url: /static
#-Index.wsgi-#
导入操作系统
导入系统
root = os.path.dirname(__file__)
sys.path.insrt(0,os.path.join(root,'。',',‘site-pageages’))
os.environ.setDefault(“ django_settings_module”,“ pylabsite.settings”)
来自django.core.wsgi import_wsgi_application
应用程序= get_wsgi_application()
这里的格式非常重要。例如,名称之间有一个空间,如果没有空间,将会出现错误。
1/Pylabsite/wsgi.py修改如下:
导入操作系统
导入系统
root = os.path.dirname(__file__)
sys.path.insert(0,os.path.join(root,'..’,‘site-ackages’))
os.environ.setDefault(“ django_settings_module”,“ pylabsite.settings”)
来自django.core.wsgi import_wsgi_application
应用程序= get_wsgi_application()
然后修改1/pylabsite/settings.py:
导入操作系统
导入OS.Path
来自OS导入环境
debug = not Environ.get(“ app_name”,“”)
如果调试:
mysql_db ='pylabsite’
mysql_user ='admin'
mysql_pass ='111'
mysql_host_m =‘127.0.0.1’
mysql_host_s =‘127.0.0.1’
mysql_port ='3306'
别的:
导入sae.const
mysql_db = sae.const.mysql_db
mysql_user = sae.const.mysql_user
mysql_pass = sae.const.mysql_pass
mysql_host_m = sae.const.mysql_host
mysql_host_s = sae.const.mysql_host_s
mysql_port = sae.const.mysql_port
调试= false
template_debug = false
数据库= {
'默认': {
“引擎”:‘django.db.backends.mysql’,
“名称”:mysql_db,
“用户”:mysql_user,
“密码”:mysql_pass,
“主机”:mysql_host_m,
“端口”:mysql_port,
}
}
default_charset ='utf-8'
允许的_host = [[[
‘.sinaapp.com’,‘,
这是给予的
步骤4:上传
修改要修改的所有内容后,您可以通过SVN上传到SAE。可以使用操作方法,正确单击Pylabsite,选择Svn Commit ...
Django袋有十几个m,但是上传速度非常慢。这个过程可以休息。
步骤5:配置MySQL
上述配置已完成。现在数据库仍然为空。一种方法是在本地部署SAE环境,然后python manage.py syncdb同步到服务器,但这是非常折腾的。
第二种方法是在本地执行python manage.py syncdb,然后从本地MySQL导出SQL语句文件,然后在SAE的MySQL中运行SQL语句。我在这里将navicat用于mysql。这很方便。
在此处选择SQL语句,或者您可以直接导入SQL文本。总体而言,配置数据库没有悬念。
使用WebClient代替FTP
首先,让我们定义一个class uploadfile。此类是文件上传类。编码显示如下:
public void uploadfile(字符串fileNamepath,string uristring,bool isautorEname)
{{{
int indexof = 0;
if(filenamepath.contains(@“”))
{{{
indexof = filenamepath.lastindexof(@“”);
}
否则(filenamepath.contains(“/”))
{{{
indexof = filenamepath.lastindexof(“/”);
}
字符串fileName = filenamepath.substring(indexof + 1);
字符串newFileName = filename;
如果(ISAUTORENAME)
{{{
newfileName = dates.now.tostring(“ yymmddhhmms”) + dateTime.now.millisecond.tostring() + filepath.substring(filenamepath.lastIndexexof(lastIndexexof(“。”);
}
字符串filenameext = filename.substring(filename.lastindexof(“。”) + 1);
if(uristring.endswith(“/”)== false)uristring = uristring +“/”;
uristring = uristring + newfileName;
///创建一个webclient示例
webclient mywebclient = new webclient();
mywebclient.credentials = recredentialCache.defaultCredentials;
//要上传的文件
FILESTREAM FS = NEW FILESTREAM(FILENAME PATH,FIFEMODE.OPEN,FIELEACCESS.READ);
// FILESTREAM FS = OPENFILE();
binarleReader r = new binaryReader(fs);
byte [] postArray = r.readBytes((int)fs.length);
流poststream = mywebclient.openwrite(uristring,“ put”);
尝试
{{{
//使用uploadfile方法使用以下格式
//mywebclient.uploadfile(uristring,“ put”,filenamepath);
if(poststream.canwrite)
{{{
poststream.write(postArray,0,postarray.length);
poststream.close();
fs.dispose();
}
别的
{{{
poststream.close();
fs.dispose();
}
}
捕获(异常ERR)
{{{
poststream.close();
fs.dispose();
投掷错误;
}
最后
{{{
poststream.close();
fs.dispose();
}
}
好吧,在定义此类课程之后,这取决于我们如何称呼它。在这里我举例说明:
单击按钮事件:
私有void Center_Click(对象发送者,EventArgs e)
{{{
//上传文件
//获取文件名,文件扩展名,服务器路径
字符串filepath = filename.text;//您需要上传的文件,您可以在这里使用OpenFileDialog获取文件
String Server = @“”“”;//上传路径
//创建一个webclient示例
webclient mywebclient = new webclient();
尝试
{{{
//使用uploadfile方法上传
uploadfile(filepath,server,true);
MessageBox.Show(“成功上传”,“系统提示”,MessageBoxButtons.ok,MessageBoxicon.Information);
}
捕获(异常EX)
{{{
MessageBox.Show(ex.message);
返回;
}
}
结论:以上是首席CTO的一般内容注释,有关如何将Django文件发布到公共网络。感谢您阅读本网站的内容。我希望这对您有帮助。有关如何发布django文件的更多信息,请不要忘记找到公共网络的相关内容。