我的Django没有显示保存在数据库中的图像

时间:2016-04-19 14:44:04

标签: python django

对不起,我的Django没有显示上传的图片。我认为这些代码以前工作过。我应该在哪里更改我的代码?

[19/Apr/2016 21:09:27] "GET /static/myapp/image/resize/yu_popup.jpg HTTP/1.1" 404 1729

我读了一些网页,说我们必须只在开发环境中使用MEDIA_ROOT,所以我不会使用MEDIA_ROOT。导致此问题的原因是我不使用MEDIA_ROOT,即使我发布申请时也应该使用MEDIA_ROOT吗?

$ tree
.
|-- db
|   |-- myapp.sqlite3
|-- db.sqlite3
|-- myapp
|   |-- __init__.py
|   |-- admin.py
|   |-- apps.py
|   |-- forms.py
|   |-- media
|   |   `-- resize
|   |       `-- photo.JPG
|   |-- migrations
|   |   |-- 0001_initial.py
|   |   |-- 0002_resize.py
|   |-- models.py
|   |-- static
|   |   `-- myapp
|   |       |-- image
|   |       |   `-- resize
|   |       |       |-- yu_popup.jpg
|   |       `-- js
|   |-- templates
|   |   |-- base.html
|   |   `-- myapp
|   |       |-- resize
|   |       |   `-- resize.html
|   |-- tests.py
|   |-- urls.py
|   `-- views.py
|-- manage.py
|-- myproject
|   |-- __init__.py
|   |-- __pycache__
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- static
    `-- myapp
        `-- image
            |-- resize
            |   |-- 1454049232616.jpg
            `-- resize_wHx2hQC

settings.py

# -*- coding: utf-8 -*-
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'debug_toolbar',
    'social.apps.django_app.default',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    #'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.cache.FetchFromCacheMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
]

APP_DIR = os.path.join(BASE_DIR, 'myapp')
#STATIC_ROOT = os.path.join(APP_DIR, "staticroot")
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    #os.path.join(BASE_DIR, 'static'),
    os.path.join(APP_DIR, 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    #'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

#MEDIA_ROOT = os.path.join(APP_DIR,"media")
#MEDIA_URL = '/media/'

views.py

def resize(request):

    #import pdb; pdb.set_trace()
    from myapp.forms import Resizef
    formset = Resizef
    photo = ""

    if request.method == 'POST':
        form = formset(request.POST)
        try:
            from myapp.models import Resize
            new_photo = Resize()
            new_photo.photo = request.FILES['photo']
            new_photo.save()
            photo = Resize.objects.get(id=new_photo.id)  # 
        except:
            pass
    else:
        form = formset()

    view = {'form': form, 'photo': photo}

    template = 'myapp/resize/resize.html'

    return render(request, template, view)

models.py

def resize_based_upload_to(instance, filename):
    return "static/myapp/image/resize/{}".format(filename)


@python_2_unicode_compatible  
class Resize(models.Model):
    photo = models.ImageField(upload_to=resize_based_upload_to)

    class Meta:
        db_table = traceback.extract_stack()[-2][2].lower()

resize.html

<html>
<body>
    <form action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
        {{ form.photo }}  <br />
        <button type="submit">
            <span>Upload</span>
        </button>  <br />
    </form>

    <h3>Upload Photo</h3>  <br />
    <img src="/{{ photo.photo }}" />  <br />
</body>
</html>

Python3.5Django1.9

0 个答案:

没有答案