code:
blueprint:
from flask import Blueprint
from flask_restful import Api
################################
### Local Imports ###
################################
profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *
Run Code Online (Sandbox Code Playgroud)
views:
class ShowProfPic(Resource):
def get(self):
return "hey"
api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic")
Run Code Online (Sandbox Code Playgroud)
how do we do a url_for with flask_restful?
because when I do.
url_for('showpic') it's a routing error
and when I do url_for('api.ShowProfPic')
it's also still a routing error
from socket import socket, AF_INET, SOCK_STREAM
sock = socket(AF_INET, SOCK_STREAM)
sock.bind(("localhost", 7777))
sock.listen(1)
while True:
try:
connection, address = sock.accept()
print("connected from " + address)
received_message = sock.recv(300)
if not received_message:
break
connection.sendall(b"hello")
except KeyBoardInterrupt:
connection.close()
Run Code Online (Sandbox Code Playgroud)
所以我试图把我的头包裹在套接字并拥有这个非常简单的脚本,但出于某种原因,我不能用a杀死这个脚本 KeyboardInterrupt
我怎么用KeyboardInterrupt 那个杀死脚本,为什么我不能用它杀死它KeyboardInterrupt?
input在Flask中发送后,如何获得id 的实际值?
形成:
<form action="" method="post">
<input id = "number_one" type="text" name="comment">
<input type="submit" value = "comment">
</form>
Run Code Online (Sandbox Code Playgroud)
比如,我想说的是表单发送时(即当你这样做时):
request.form.get("comment")
Run Code Online (Sandbox Code Playgroud)
传递文本字段的值.我无法弄清楚的是如何获得价值id.
因此,当表单发送时,我们可以告诉信息来自哪个表单,因为每个表单都有一个唯一的id.在这种情况下id是number_one.
那么,我们如何获取id文本输入的实际文字值而不是文本输入?
所以我练习我的刮擦,我遇到了这样的事情:
<div class="profileDetail">
<div class="profileLabel">Mobile : </div>
021 427 399
</div>
Run Code Online (Sandbox Code Playgroud)
我需要<div>标签外的数字:
我的代码是:
num = soup.find("div",{"class":"profileLabel"}).text
Run Code Online (Sandbox Code Playgroud)
但它的输出Mobile :只是<div>标签内的文本而不是标签外的文本。
那么我们如何提取<div>标签外的文本呢?
所以我试着这样做.
a = []
map(lambda x: a.append(x),(i for i in range(1,5)))
Run Code Online (Sandbox Code Playgroud)
我知道map有一个函数但是为什么它不会附加到列表中呢?或者追加不是功能?但是打印a结果a仍然是空的
现在有趣的是这是有效的
a = []
[a.append(i) for i in range(5)]
print(a)
Run Code Online (Sandbox Code Playgroud)
是不是他们基本上"说"同样的事情?
这几乎就好像列表理解成为某种混合列表理解功能的东西
那么为什么lambda和map方法不起作用呢?
我一直在玩默认字典而且我很困惑
为什么这不起作用:
例1
def hi(name):
return "hi " + name
a = defaultdict(hi)
print(a["hello"]("jane"))
Run Code Online (Sandbox Code Playgroud)
输出示例1
TypeError: hi() missing 1 required positional argument: 'name'
Run Code Online (Sandbox Code Playgroud)
但这样做:
例2
def hi(name):
return "hi " + name
a = {"hello":hi}
print(a["hello"]("jane"))
Run Code Online (Sandbox Code Playgroud)
输出示例2
hi jane
Run Code Online (Sandbox Code Playgroud)
也使用lambda会使它工作
例3
def hi(name):
return "hi " + name
a = defaultdict(lambda: hi)
print(a["hello"]("jane"))
Run Code Online (Sandbox Code Playgroud)
输出示例 3
hi jane
Run Code Online (Sandbox Code Playgroud)
为什么示例1返回错误而示例3没有?
这是怎么回事?
我只是查找了数组和arrayList
并且发现一个数组是固定长度的,并且当一个arraylist可以改变并且长度可变时不能改变
我的问题是:
在python中是array == tuple?
并且是在arthon中的arraylist == list?
如果它们不是数组和arraylist的python等价物?
我的意思是。
我在这里实现了一个引导日期时间选择器。https://eonasdan.github.io/bootstrap-datetimepicker/
我的问题是。
如果你有一个输入框,如何禁用它并只让日期时间选择器的输入有效?因为用户可能输入了无效的日期格式。
下面的代码取自https://eonasdan.github.io/bootstrap-datetimepicker/#minimum-setup
代码:
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 形式:
class SignUpForm(Form):
username = TextField("Username: ",validators=[Required(),Length(3,24)])
Run Code Online (Sandbox Code Playgroud)
为什么这样做?
form = SignUpForm()
form.username(placeholder="username")
Run Code Online (Sandbox Code Playgroud)
但当您直接使用占位符作为SignUpForm?
class SignUpForm(Form):
username = TextField("Username: ",placeholder="username",validators=[Required(),Length(3,24)])
Run Code Online (Sandbox Code Playgroud)
它给出了这个错误 TypeError: __init__() got an unexpected keyword argument 'placeholder'
我对此有点困惑,因为直接在类上定义它应该与做一样,form.username(placeholder="username") 但为什么它会给出错误?
我已经检查了文档,并且如何实现is_accessible方法非常模糊.
以下是烧瓶管理员的文档显示的内容
class MicroBlogModelView(sqla.ModelView):
def is_accessible(self):
return login.current_user.is_authenticated()
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('login', next=request.url))
Run Code Online (Sandbox Code Playgroud)
我不知道的是你怎么称呼它是自动调用还是你必须像这样自己调用它:
@expose("/", methods=["GET", "POST"])
def home(self):
if self.is_accesible():
return super().index()
else:
return self.login()
def is_accesible(self):
return current_user.is_authenticated and "admin" in current_user.role
Run Code Online (Sandbox Code Playgroud)
因为放一个是非常重复的
if self.is_accesible():
return super().index()
Run Code Online (Sandbox Code Playgroud)
检查我们是否有很多管理员观点.那么我们究竟是如何实现它的呢?文档展示了如何将它放在您的模型中,而不是如何在您的视图上实现它
python ×9
flask ×4
html ×3
arraylist ×1
arrays ×1
flask-admin ×1
html-parsing ×1
html5 ×1
java ×1
javascript ×1
jquery ×1
lambda ×1
python-3.x ×1
sockets ×1