Nginx,gunicorn,ubuntu 16.06 - 上传的图片未显示

时间:2017-07-27 11:18:31

标签: python django nginx digital-ocean

人类同胞

我已经在这个问题上待了几天,非常感谢一些帮助。所以我成功部署了我的应用程序,一切正常,包括CSS和javascript除了这个小东西,我确定只是一个小修复我错过了,导致我上传的图像不显示甚至我看到“上传成功”页面。

我的图片模型

class Image(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                         related_name='images_created')
    title = models.CharField(max_length=2000)
    slug = models.SlugField(max_length=200,
                        blank=True)
    url = models.URLField()
    image = models.ImageField(upload_to='/home/kingiyk/stylplus/media/')
    description = models.TextField(blank=True, null=True)
    created = models.DateField(auto_now_add=True,
                                            db_index=True)
    users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
                                                related_name='images_liked', blank=True)

    tags = TaggableManager()

    class Meta:
        ordering = ('-created',)




    def __str__(self):
        return self.title


    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
            super(Image, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('images:detail', args=[self.id, self.slug])

我的nginx conf

server {
    listen 80;
    server_name 67.207.87.22;

    location = /favicon.ico { access_log off; log_not_found off;

    }

    location /static/ {
        alias /home/kingiyk/stylplus/static;
    }

    location /media/ {
        alias /home/kingiyk/stylplus/media;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/kingiyk/stylplus/stylplus.sock;
    }
}

我上传的图片位于/ home / kingiyk / stylplus / media /所以至少我知道他们已经保存了。

这是我的设置文件:

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





DEBUG = False

ALLOWED_HOSTS = ['67.207.87.22', 'styllplus.com', 'www.styllplus.com']



ROOT_URLCONF = 'stylplus.urls'



WSGI_APPLICATION = 'stylplus.wsgi.application'









STATIC_URL = '/static/'
STATIC_ROOT = '/home/kingiyk/stylplus/static/'




from django.core.urlresolvers import reverse_lazy

LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout')

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'



ABSOLUTE_URL_OVERRIDES = {
    'auth.user': lambda u: reverse_lazy('user_detail', args=[u.username])
    }

SITE_ID = 1


DJANGO_MESSAGES_NOTIFY = False

这是我的html上传页面,它应该显示图像

{% extends "base.html" %}

{% load staticfiles %}


{% block title %}{{ user.username }}{% endblock %}
{% block content %}
{% load thumbnail %}
<div id="lonely">
 {% thumbnail image.user.profile.photo "40" as in %}
    <a href="{{ image.user.get_absolute_url }}">
        <img src="{{ in.url }}" class="gametime"></a>
        {% endthumbnail %}
<a href="{{ image.user.get_absolute_url }}" class="time">{{ image.user.username }}</a>

<div id="details">
{% thumbnail image.image "551" as im %}

    <a href="{{ image.image.url }}">
        <img src="{{ im.url }}" alt="{{ image.title }}"class="image-detail">
    </a>

{% endthumbnail %}

{% with total_likes=image.users_like.count users_like=image.users_like.all %}
    <div class="image-info">
     <div>
        <span class="count">
            <span class="total">{{ total_likes }}</span>
            like{{ total_likes|pluralize }}
        </span>
        {% with comments.count as total_comments %}

{{ total_comments }} comment{{ total_comments|pluralize }}

{% endwith %}

                              {%if request.user不在users_like%}                  喜欢                 {%else%}                  不像                 {% 万一 %}              

    </div>
    {{ image.title|linebreaks }}
</div>

<div id="my-chart"></div>
<div class="image-likes">
    Liked by
    {% for user in image.users_like.all %}
        <div>
            {% thumbnail user.profile.photo "30" as in %}
            <img src="{{ in.url }}" class="gametime">
            {% endthumbnail %}
            <a href="{{ user.get_absolute_url }}">{{ user.username }}

        </div>
    {% empty %}

    {% endfor %}
</div>
{% endwith %}




{% for comment in comments %}
<div class="image-info">
<p class="info">


    <a href="{{ comment.user.get_absolute_url }}" class="mine">{{      

     comment.user }}</a> {{ comment.comment }}

</p>

除了图像不可见之外,其他任何东西都可以正常工作 我看不出这个bug是什么。我很感激你的时间

1 个答案:

答案 0 :(得分:1)

如果要显示媒体文件,您需要添加媒体网址和root来告诉django在哪里查找媒体

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

settings.py

中添加这2行

如果您在主STATIC_URL中添加了STATIC_ROOTurls.py,那么也会将MEDIA_URLMEDIA_ROOT添加为静态。

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL , document_root=settings.MEDIA_ROOT)

将这些行添加到您的urls.py