我试图找到使用django的模板上下文加载器显示图像的最有效方法.我的应用程序中有一个静态目录,其中包含图像'victoryDance.gif'和项目级别的空静态根目录(带settings.py).假设我urls.py和settings.py文件中的路径是正确的.什么是最好的观点?
from django.shortcuts import HttpResponse
from django.conf import settings
from django.template import RequestContext, Template, Context
def image1(request): # good because only the required context is rendered
html = Template('<img src="{{ STATIC_URL }}victoryDance.gif" alt="Hi!" />')
ctx = { 'STATIC_URL':settings.STATIC_URL}
return HttpResponse(html.render(Context(ctx)))
def image2(request): # good because you don't have to explicitly define STATIC_URL
html = Template('<img src="{{ STATIC_URL }}victoryDance.gif" alt="Hi!" />')
return HttpResponse(html.render(RequestContext(request)))
def image3(request): # This allows you to load STATIC_URL selectively from the …Run Code Online (Sandbox Code Playgroud) 我有一个pandas数据帧
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
df
A B C D
E -0.585995 1.325598 -1.172405 -2.810322
F -2.282079 -1.203231 -0.304155 -0.119221
G -0.739126 1.114628 0.381701 -0.485394
H 1.162010 -1.472594 1.767941 1.450582
I 0.119481 0.097139 -0.091432 -0.415333
J 1.266389 0.875473 1.787459 -1.149971
Run Code Online (Sandbox Code Playgroud)
我如何展平此数组,同时保持列和索引ID如下所示:
E A -0.585995
E B 1.325598
E C -1.172405
E D -2.810322
F A ...
F B ...
...
...
J D -1.149971
Run Code Online (Sandbox Code Playgroud)
无关紧要的价值出现在......
np.flatten()可用于将df.values展平为一维数组,但后来我失去了索引和列的顺序......