我想创建一个仪表板,我可以使用该库分析一下我的模型数据(文章)plotly.
Plotly条形图没有显示在我的模板上,我想知道我是否做错了,因为下面的代码没有错误:
models.py
from django.db import models
from django.contrib.auth.models import User
import plotly.plotly as py
import plotly.graph_objs as go
class Article(models.Model):
user = models.ForeignKey(User, default='1')
titre = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=40)
likes = models.ManyToManyField(User, related_name="likes")
def __str__(self):
return self.titre
@property
def article_chart(self):
data = [
go.Bar(
x=[self.titre], #title of the article
y=[self.likes.count()] #number of likes on an article
)
]
plot_url = py.plot(data, filename='basic-bar')
return plot_url
Run Code Online (Sandbox Code Playgroud)
dashboard.html
<div>{{ article.article_chart }}</div>
Run Code Online (Sandbox Code Playgroud)
为什么条形图不可见?有什么建议吗?
我想把我的名单中的字符串放进去,
from random import shuffle
words = ['red', 'adventure', 'cat', 'cat']
shuffled = shuffle(words)
print(shuffled) # expect new order for, example ['cat', 'red', 'adventure', 'cat']
Run Code Online (Sandbox Code Playgroud)
预期结果 :
from random import shuffle
words = ['red', 'adventure', 'cat', 'cat']
shuffled = shuffle(words)
print(shuffled) # expect new order for, example ['cat', 'red', 'adventure', 'cat']
Run Code Online (Sandbox Code Playgroud)
我尝试使用随机库中的shuffle,但它给了我一个None错误.
我尝试了什么:
from random import shuffle
words = ['red', 'adventure', 'cat', 'cat']
shuffled = shuffle(words)
print(shuffled) # expect new order for, example ['cat', 'red', 'adventure', 'cat']
Run Code Online (Sandbox Code Playgroud) 我知道关于这个主题的主题并没有遗漏,这就是为什么我查看了关于这个主题的大量帖子,找不到令我满意的东西,所以我试图自己构建它.
我想要做的就是使用Djaxo使用Ajax上传文件以避免页面刷新.
这是我做的:
basic_upload.html:
# html
<form method="post" id="Form" enctype="multipart/form-data">
{% csrf_token %} # django security
<input id="image_file" type="file" name="image_file">
<input type="submit" value="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
Ajax.js:
# javascript
$(document).on('submit', '#Form', function(e){
e.preventDefault();
var form_data = new FormData();
form_data.append('file', $('#image_file').get(0).files);
$.ajax({
type:'POST',
url:'my_url',
processData: false,
contentType: false,
data:{
logo:form_data,
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(), # django security
},
});
});
Run Code Online (Sandbox Code Playgroud)
Views.py:
# views.py (server side)
def myFunction(request):
if request.method == 'POST':
image_file = request.FILES
...
...
Run Code Online (Sandbox Code Playgroud)
(forms.py和urls.py配置正确,没有必要包含它们).
我想我在ajax.js上的方式存在问题,因为在调试时logo返回除了之外的每个数据logo.
我在做什么直言不讳?
我正在 Zeit Now 上从 Express 迁移到无服务器功能。
Stripe webhook 文档要求提供原始正文请求,使用 Express 时我可以通过 bodyParser 获取它,但是它如何在无服务器功能上工作?如何以字符串格式接收正文以验证条带签名?
支持团队将我重定向到这个文档链接,我很困惑,据我所知,我必须传入text/plain请求标头,但我无法控制它,因为 Stripe 是发送 webhook 的那个。
export default async (req, res) => {
let sig = req.headers["stripe-signature"];
let rawBody = req.body;
let event = stripe.webhooks.constructEvent(rawBody, sig, process.env.STRIPE_SIGNING_SECRET);
...
}
Run Code Online (Sandbox Code Playgroud)
在我的函数中,我req.body作为一个对象接收,我该如何解决这个问题?
我的应用程序上的小问题,占位符在Internet Explorer 11上不存在.
我尝试了下面的CSS示例但没有任何成功.
:-ms-input-placeholder { /* IE10–11 */
color: #ccc !important;
font-weight: 400 !important;
}
::-ms-input-placeholder { /* Edge */
color: #ccc;
font-weight: 400;
}
::placeholder { /* CSS Working Draft */
color: #ccc;
font-weight: 400;
}
Run Code Online (Sandbox Code Playgroud)
有关如何解决此问题的任何建议?
这个问题可能已经被问过了,但我找不到一个合适的方法来将多个变量分配给一个值而不将它们链接到它,所以请耐心等待.
例1:
a = b = []
a.append('x')
> a = ['x']
> b = ['x']
Run Code Online (Sandbox Code Playgroud)
因为我追加'x'到a我不想拥有它b.
例2:
a, b = [[], []]
a.append('x')
> a = ['x']
> b = []
Run Code Online (Sandbox Code Playgroud)
按预期工作,但有多个变量,它变得非常丑陋:
a, b, c, d, e, f, g, h, i j = [[], [], [], [], [], [], ...]
Run Code Online (Sandbox Code Playgroud)
例3:
默认方式
a = []
b = []
...
Run Code Online (Sandbox Code Playgroud)
与示例2相同,它与多个变量不同.
我想知道像javascript这样的东西是否存在?
a, b, c = [] #this actually gives a ValueError: not enough …Run Code Online (Sandbox Code Playgroud) 当我点击它时,我想分别关注每个"p"标签,就像输入上的CSS"焦点:"一样.问题是选择器"焦点"不适用于段落,这是一个例子:
HTML
<div id="myDiv">
<p>Some Content 1</p>
<p>Some Content 2</p>
<p>Some Content 3</p>
<p>Some Content 4</p>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
#myDiv p:focus {background-color:red;}
Run Code Online (Sandbox Code Playgroud)
如何找到替代解决方案才能使其正常工作?
我正在从 Jquery AJAX 切换到 react-dropzone & Axios,我想将文件上传到我的 Django 服务器,我在服务器上发布图像的 blob url 没有问题,但我想把它放在下面,request.FILES但我我得到一个空的查询集。
request.FILES : <MultiValueDict: {}> <!--- empty
request.POST : <QueryDict: {}> <!--- able to get a blob url
Run Code Online (Sandbox Code Playgroud)
这是我的 axios 配置的样子:
const temporaryURL = URL.createObjectURL(step3.drop[0]);
var fd = new FormData();
fd.append('image', temporaryURL);
axios({
method: 'post',
url: SITE_DOMAIN_NAME + '/business-card/collect/',
data: fd,
headers: {
"X-CSRFToken": CSRF_TOKEN,
"content-type": "application/x-www-form-urlencoded"
}
}).then(function (response) {
console.log(response)
URL.revokeObjectURL(temporaryURL);
}).catch(function (error) {
console.log(error)
});
Run Code Online (Sandbox Code Playgroud)
我正在根据 POST 请求在 classBasedView 上接收文件。
如何上传文件?我哪里错了?
编辑
我也试过“应用程序/表单数据”,没有解决问题
这是一个示例表格:
var form = document.querySelector('form');
function detectChange() {
var inputs = form.querySelectorAll('input');
for (var input of inputs) {
if (input.value != input.defaultValue) {
return true;
}
}
}
form.querySelector('button').addEventListener('click', function() {
if (detectChange() && confirm('Are you sure you want to reset?')) {
form.reset();
}
});Run Code Online (Sandbox Code Playgroud)
<form>
<input type="number">
<input type="number" value="7">
<button type="button">Reset</button>
</form>Run Code Online (Sandbox Code Playgroud)
我希望即使用户输入非数字值,重置按钮也可以使用。
我想知道是否可以根据另一种类型的值来扩展类型?
\ninterface NavType {\n hideNav?: boolean\n dev?: boolean\n}\nRun Code Online (Sandbox Code Playgroud)\nhideNav我想仅当arg 为 false 时才允许使用dev。
const Navbar: React.FC<NavType> = ({ children, hideNav, dev = false }) => (\n <nav>\n {!dev ? (\n <NavDefault hideNav={!!hideNav}>{children}</NavDefault>\n ) : (\n <NavDev>{children}</NavDev>\n )}\n </nav>\n)\nRun Code Online (Sandbox Code Playgroud)\n在这种情况下我该如何使用条件语句?
\n// Should PASS\nconst navArgs1: NavType = {\n hideNav: undefined,\n dev: true\n}\n\nconst navArgs2: NavType = {\n hideNav: true or\xc2\xa0false,\n dev: false\n}\n\n// Should NOT PASS\nconst navArgs3: NavType = {\n hideNav: true or\xc2\xa0false,\n dev: true\n}\nRun Code Online (Sandbox Code Playgroud)\n javascript ×4
html ×3
python ×3
css ×2
django ×2
jquery ×2
reactjs ×2
ajax ×1
arrays ×1
axios ×1
file-upload ×1
forms ×1
html5 ×1
node.js ×1
placeholder ×1
plotly ×1
serverless ×1
typescript ×1
vercel ×1