在开发Django中使用生产媒体文件

2024-05-23 14:23:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用Django(和Django CMS)制作的博客。有时候,当我在开发中工作时,我会做一些更改,如果能看到我所做的这些更改与我在生产中使用的实际媒体文件是什么样的,那就太好了

因此,今天我尝试将MEDIA_URLMEDIA_ROOT设置设置为我的生产站点的实际媒体URL。i、 e:

MEDIA_URL = 'https://example.com/media/'
MEDIA_ROOT = 'https://example.com/media/'

我天真地希望在完成这项工作后,能看到正在开发中的制作媒体文件,但没有,它没有起作用

我还尝试了urlpatterns列表的典型扩展。i、 e:

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

但是我在docs中读到,如果给定的前缀(MEDIA_URL)是一个URL(如我的例子),那么这样做是没有用的

传统信息:我的网站由PythonAnywhere托管,它们(PythonAnywhere)也提供媒体文件。这些文件都是公开的。我使用^{}作为我的文件管理器。我的媒体文件的一个示例:media file

不管怎样,这有可能实现吗?如果是,我错过了什么


Tags: 文件djangohttpscomurlsettingscmsexample
1条回答
网友
1楼 · 发布于 2024-05-23 14:23:39

你得到了一个非常丑陋的解决方案:你应该了解更多关于adaptersrecycled views

这是你应该做的

// adapter class
private boolean setBlueBackground = false;

public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView;

    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        textView = new TextView(mContext);
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(new GridView.LayoutParams(100, 100));

    } else {
        textView = (TextView) convertView;
    }

    if(setBlueBackground)
        textView.setBackgroundColor(Color.BLUE);
    else 
        textView.setBackgroundColor(Color.RED);

    textView.setText("" + position);
    return textView;
}

public void setBlueBg(boolean blue) {
    setBlueBackground = blue;
    notifyDataSetChanged();
}

现在你要做的就是改变颜色

mAdapter.setBlueBg(true); // or false to make it red

它将取代这个

for(int i = 0; i < 180;i++){ //NPE from 180 or higher
    Log.d("Getting view number",""+i);
    mGridView.getChildAt(i).setBackgroundColor(Color.BLUE);
}

好了

相关问题 更多 >