admin上传的Django图像不会显示在生产或开发中

时间:2017-10-20 22:24:44

标签: django django-admin

我有Django 1.10和whitenoise 3.3,该网站部署在Heroku上。我的静态文件在开发和生产方面都很好。但是,我的媒体文件不会在生产和开发中加载。

当debug = True时,媒体文件在生产和开发中都能正确呈现。我真正想要的是即使在debug = False时媒体文件也能正确呈现。

一切都很好,花花公子直到我意识到我使用debug = True部署了我的网站。所以,我把它改成了debug = False。现在静态和媒体文件都失败了。我做了一些研究,然后去了whitenoise docs来跟进设置。然后静态文件工作。但是,媒体文件仍然失败。我在Django上阅读了有关媒体和静态文件的文档,但遗憾的是只看到了一些带有nginx的示例,而且doc说有很多方法可以做到这一点并且只能提供结构。

我正在寻找一个解决方案,这将允许我继续通过管理员在生产中添加到我的网站的东西,仍然允许我用heroku运行我的网站。

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
'polls.apps.PollsConfig',
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'whitenoise.runserver_nostatic',

#'storages',
]


MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',

'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

 TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

STATIC_URL = '/static/'
#STATIC_URL = '/site_media/'
#STATICFILES_DIRS = [
#    os.path.join(BASE_DIR, 'mysite/static'),
#]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
#STATIC_ROOT = ''

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

models.py

from django.db import models

class Post(models.Model):
    pub_date = models.DateTimeField('date published')
    title = models.CharField(max_length=200)
    thumbnail = models.ImageField(upload_to='static')
    def __str__(self):
        return self.title

class Image(models.Model):
    image = models.ImageField(max_length=100, upload_to='static')
    pub_date = models.DateTimeField('date_published')
    post = models.ForeignKey('Post', default=0, on_delete=models.CASCADE,)
    title = models.CharField(max_length=200)

    def __str__(self):
        return self.title

class Text(models.Model):
    text = models.TextField()
    pub_date = models.DateTimeField('date_published')
    post = models.ForeignKey('Post', default=0, on_delete=models.CASCADE,)
    title = models.CharField(max_length=200)

    def __str__(self):
        return self.title

这是blog / templates / blog /

中的html文件

about.html

{% load static %}

<html>
    <head>
        <title> and1can's Blog </title>
        <link rel="stylesheet" type="text/css" href="{% static     'blog/about.css' %}"/> 
         <link rel="stylesheet"     href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
         <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Josefin+Slab" />
    </head>
    <body>
        <div class='header'>
            <a class='title' href="/"> <h1 class='title'> and1can </h1></a>
        </div>


            <h3 class='intro'> Machine Learning, Statistics, and all that other fun stuff </h3>

            <div class='about_title'> 
                Andy Chu 
            </div>


        </div>

        </br>



        </br>
        </br>
        </br>

    <div class="wrapper">
        <div class="line"><hr width="90%"></div>
    </div>

    </br>

    <div class='blurb'>
        I am interested in what we are able to learn from data. I am looking to join a data science team that shares this interest and satisfaction gained when making predictions that would otherwise not be possible without analyzing data. Statistics, machine learning, artificial intelligence, computer vision, and computational geometry are my fields of interest.
    </div>

    </br>


    <div class="container">
        <div class='email'>
            Contact: </br>
             </br>
        </div>



        <a href="https://linkedin.com/in/andy-chu-76456a32"> 
            <img src="/media/static/linkedin.png" height=40; width=40;/>
         </a> &nbsp; &nbsp; &nbsp;

        <a href="https://github.com/and1can">
                <img src="/media/static/github.png" height=40; width=40/>
        </a> &nbsp; &nbsp; &nbsp;
        <a href="https://www.kaggle.com/and1can">
            <img src="/media/static/kaggle.png" height=40; width=60/>
        </a>


</body>
</html>

index.html

 {% load static %}



<html>
<head>
    <title> and1can's Blog </title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link rel="stylesheet" type="text/css" href="{% static 'blog/blog.css' %}"/>
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Josefin+Slab" />
        </head>
<body>
    <div>
        <h1 class='title'> and1can </h1>
        <h3 class='intro'> Machine Learning, Statistics, and all that other fun stuff </h3>
        <a class='about' href="/blog/about"> <h1 class='title'> about </h1></a>

    </div>



    <div class="line"><hr width="90%"></div>


    </br>
    </br>
    </br>
    <ul>
        {% for p in latest_posts %}
        </br>
            <div class="header">
                <a href="/blog/{{ p.id }}">
                    {{ p.title }}

                </a>
            </div>
            <div class='intro'>
                {{ p.pub_date}}
            </div>
            </br>
            </br>
                <a href="/blog/{{ p.id }}">
                    <img class="image" src="{{ p.thumbnail.url }}" height=400 width=400/>
                </a>
        </br>
        </br>
        </br>
        </br>
        </br>
        {% endfor %}
    </ul>
</body>
</html>

post.html

{% load static %}



<html>
<head>
    <title> and1can's Blog </title>
    <link rel="stylesheet" type="text/css" href="{% static 'blog/post.css' %}"/> 
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Josefin+Slab" />
</head>
<body>
    <div class='header'>
        <a class='title' href="/"> <h1 class='title'> and1can </h1></a>

    <h3 class='intro'> Machine Learning, Statistics, and all that other fun stuff </h3>


        <a class='about' href="/blog/about"> <h1 class='title'> about </h1></a>
    </div>






    </br>
    </br>
    </br>

    <div class="wrapper">

        <div class="line"><hr width="93%"></div>
    </div>
    </br>
    </br>

    </div>

    <div class='post'>

        <div class='post_intro'>
            {{ post.pub_date }}
        </div>
    </div>


        <div class='post_title'>
            {{ post.title }}
        </div>



        </br>



        </br> 
        <img class="image" src="{{ post.thumbnail.url }}" width=300, height=300/>


    <ul class='content'>
        {% for o in output %}
        </br>
            {% if o.image %}

                    {% block content %}
                        <img class="image" src="{{ o.image.url }}" />
                    {% endblock %}

            {% else %}
                <div class="text">
                {{o.text}}
                </div>
            {% endif %}
        </br>
        {% endfor %}
    </ul>



</body>
</html>

0 个答案:

没有答案