我想要实现的目标:
我想创建一个python脚本来从CLI中停用数据库中的Django用户.我想出了这个:
$ sudo python manage.py shell
>>> user = User.objects.get(username=FooBar)
>>> user.is_active = False
>>> user.save()
>>> exit()
Run Code Online (Sandbox Code Playgroud)
当我手动输入manualy命令后,上面的代码工作.但是,我想将命令放在一个.py脚本中
$ sudo python script.py
Run Code Online (Sandbox Code Playgroud)
现在我尝试了不同的aproaches:
os.system("command1 && command2 && command3")
subprocess.Popen("command1 && command2 && command3",
stdout=subprocess.PIPE, shell=True)
问题:
这不起作用!我认为这个问题在这里,因为Python等待打开的Django shell(第一个命令)完成,这是永远不会.它不执行脚本中的其余命令,因为第一个命令将其置于保持状态.
subprocess.popen
可以在shell中执行命令,但只能在Python shell中执行,我想使用Django shell.
任何想法如何使用.py脚本访问Django shell以执行自定义代码?
目标:
我正在使用bash CURL脚本连接到Cloudflare APIv4.目标是更新A记录.我的剧本:
# Get current public IP
current_ip=curl --silent ipecho.net/plain; echo
# Update A record
curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEIDHERE/dns_records/DNSRECORDHERE" \
-H "X-Auth-Email: EMAILHERE" \
-H "X-Auth-Key: AUTHKEYHERE" \
-H "Content-Type: application/json" \
--data '{"id":"ZONEIDHERE","type":"A","name":"example.com","content":"'"${current_ip}"'","zone_name":"example.com"}'
Run Code Online (Sandbox Code Playgroud)
问题:
我在脚本中调用current_ip变量时不会打印它.输出将是"content" : ""
和不"content" : "1.2.3.4"
.
我使用了其他 stackoverflow帖子,我试图按照他们的例子,但我认为我仍然做错了什么,只是无法弄清楚是什么.:(
我目前正在使用瓶子框架在Python中使用简单的webapp .这是我的app结构:
结构体
lib
- bottle.py
- bottledaemon.py
- lockfile.py
- __init__.py
view
- dashboard.tpl
run.py
Run Code Online (Sandbox Code Playgroud)
这是我的run.py代码:
#!/usr/bin/env python
from lib.bottle import route, template, run, debug, request, static_file
from lib.bottledaemon import daemon_run
debug(mode=True)
@route('/')
def show_index():
return template('dashboard')
# If the following line is enabled, the server will start in non-Daemon mode.
#run(host='0.0.0.0', port=80, debug=True)
# If the following lines are enabled, the server will start in Daemon mode.
if __name__ == "__main__":
daemon_run()
Run Code Online (Sandbox Code Playgroud)
我想在不使用旧版SQL的情况下向现有表中添加一列。
基本的SQL语法是:
ALTER TABLE table_name
ADD column_name datatype;
Run Code Online (Sandbox Code Playgroud)
我格式化了Google BigQuery的查询:
ALTER TABLE `projectID.datasetID.fooTable`
ADD (barColumn date);
Run Code Online (Sandbox Code Playgroud)
但是,此错误的语法不正确:
Error: Syntax error: Expected "." or keyword SET but got identifier "ADD" at [1:63]
Run Code Online (Sandbox Code Playgroud)
那么,如何为Google BigQuery正确格式化SQL?
我正在使用Python Bottle中的python应用程序.如果我使用像/ dashboard或/ rules或/ page这样的1个lvl深度URL,该应用程序可以正常工作.但是,如果我更深入地像/ dashboard/overview或/ rules/ruleone或/ page/test那样,CSS,JS,字体和图像将会失败.:(
HTML源代码仍然指向/ assets /但如果我在/ rules/ruleone之类的URL上,正确的路径应该是../assets或./assets吗?path/assets /仅适用于第一级,但不适用于更深层的lvls,换句话说:bottle不会将静态文件路径调整到当前目录.我该如何解决?
我好几天都坚持这个问题,我真的希望有人可以帮助我.:(
我的代码(简化):
#!/usr/bin/env python
import lib.bottle as bottle
from lib.bottle import route, template, debug, static_file, TEMPLATE_PATH, error, auth_basic, get, post, request, response, run, view, redirect, SimpleTemplate, HTTPError, abort
import os, sys, re
@route('/dashboard')
@view('secure_page')
def show__page_dashboard():
return dict(page = 'Dashboard')
@route('/rules/<rule>')
@view('secure_page')
def show_page_rules_more(rule):
return dict(page = rule)
@route('/assets/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='/var/myapp/assets')
TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "view")))
bottle.debug(True)
from lib.bottledaemon import daemon_run
if __name__ == "__main__":
daemon_run() …
Run Code Online (Sandbox Code Playgroud) 基本上,我只想说“给我Y列中的所有值”,而不是“选择所有值并基于主键Z筛选Y”
“列”一词在Django文档页面上仅被提及过一次,在我眼中很清楚地解释了“主键”和“行”选项。
现在我想知道,我该怎么做?:)