从django admin

时间:2017-08-16 20:14:57

标签: python django

对于我建立的网站(从头开始的第一个网站,对于django而言是新的)我想向管理员提供一种从管理页面部署图像的方法,这些图像将用于驱动业务逻辑顾客的观点。但是,我希望管理员也能够从管理页面查看这些图像。 所以我希望可以从应用视图和管理视图访问一个公共图像文件夹。我目前只有一个名为retailFilters的应用程序。

现在实际上将一些文件部署到我的媒体/图像文件夹并不是问题,我在管理页面上添加了一条记录(指定要上传的图像),果然,文件正好等待我预期的位置。我也意识到我必须告诉django在哪里为他们提供服务,并且从互联网上寻找我的MEDIA_ROOT,MEDIA_URL和urlpatterns定义为:

settings.py

...
parent_dir = os.path.abspath(os.path.dirname(__file__) + '/..')
MEDIA_ROOT = os.path.join(parent_dir, 'media/')
MEDIA_URL = 'media/'

(和INSTALLED_APPS包括django.contrib.staticfiles))

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

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

然而,点击它们的响应是管理员挽救了横幅错误:

"使用ID过滤" 1 /更改/ media / images / 2012-03-29" .27.05.jpg"不存在。也许它被删除了?"

我的python版本是2.7.10

我的django版本是1.11

目前我的目录结构(root是项目目录)如下:

.
├── db.sqlite3
├── manage.py
├── media
│   └── images
│       └── 2012-03-29_22.27.05.jpg
├── NOTES
├── retailFilters
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── fixtures
│   │   └── boltons.json
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_auto_20170816_1934.py
│   │   ├── 0002_auto_20170816_1934.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tables
│   │   ├── Bolt_On_Group.py
│   │   ├── Bolt_On_Group.pyc
│   │   ├── Bolt_On.py
│   │   ├── Bolt_On.pyc
│   │   ├── Filter.py
│   │   ├── Filter.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── Order_Payment.py
│   │   ├── Order_Payment.pyc
│   │   ├── Order.py
│   │   ├── Order.pyc
│   │   ├── Payment_Vendor.py
│   │   ├── Payment_Vendor.pyc
│   │   ├── User.py
│   │   └── User.pyc
│   ├── tests.py
│   └── views.py
├── snapify
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── static
    └── retailFilters
        └── media
            └── images

我的admin.py有:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from django.contrib.auth.models import User
from django.contrib.auth.models import Group

from tables.Filter import Filter

admin.site.unregister(User)
admin.site.unregister(Group)

<...other un-related admin entries ...>    

@admin.register(Filter)
class FILTER_Admin(admin.ModelAdmin) :
    fields = ('DESCRIPTION', 'FILENAME', 'CATEGORY', 'PRICE')

    #list_display = ('show_image',)

我的Filter模型是:

Filter.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage

import os
import pdb

categories = {'T': 'Personalised Template', 
              'C': 'Custom Filter'}
images = 'images'

class Filter(models.Model) :
    FILTER_REFERENCE   = models.IntegerField(primary_key = True)
    FILENAME           = models.ImageField(upload_to = images)
    DESCRIPTION        = models.TextField()
    CATEGORY           = models.CharField(max_length = 1, null = True, choices = [(x, categories[x]) for x in categories])
    PRICE              = models.DecimalField(default = 0.00, max_digits = 5, decimal_places = 2)

    def __unicode__(self) :
        return self.DESCRIPTION

    #def show_image(self) :
    #    #pdb.set_trace()
    #    return '<a href="{0}"><img src="{0}"></a>'.format(self.FILENAME)

    #show_image.allow_tags = True

和runserver命令行的最后一个输出:

System check identified no issues (0 silenced).
August 16, 2017 - 20:03:42
Django version 1.11.1, using settings 'snapify.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[16/Aug/2017 20:03:44] "GET /admin/ HTTP/1.1" 200 4683
[16/Aug/2017 20:03:47] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 4203
[16/Aug/2017 20:03:47] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:03:55] "POST /admin/retailFilters/filter/ HTTP/1.1" 200 3175
[16/Aug/2017 20:03:57] "POST /admin/retailFilters/filter/ HTTP/1.1" 302 0
[16/Aug/2017 20:03:57] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 3189
[16/Aug/2017 20:03:58] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:04] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 3067
[16/Aug/2017 20:04:06] "GET /admin/retailFilters/ HTTP/1.1" 200 3167
[16/Aug/2017 20:04:08] "GET /admin/retailFilters/filter/add/ HTTP/1.1" 200 5420
[16/Aug/2017 20:04:08] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:20] "POST /admin/retailFilters/filter/add/ HTTP/1.1" 302 0
[16/Aug/2017 20:04:20] "GET /admin/retailFilters/filter/ HTTP/1.1" 200 4391
[16/Aug/2017 20:04:20] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:23] "GET /admin/retailFilters/filter/1/change/ HTTP/1.1" 200 5789
[16/Aug/2017 20:04:23] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[16/Aug/2017 20:04:25] "GET /admin/retailFilters/filter/1/change/media/images/2012-03-29_22.27.05.jpg/ HTTP/1.1" 302 0
[16/Aug/2017 20:04:25] "GET /admin/retailFilters/filter/1/change/media/images/2012-03-29_22.27.05.jpg/change/ HTTP/1.1" 302 0
[16/Aug/2017 20:04:25] "GET /admin/ HTTP/1.1" 200 5427
[16/Aug/2017 20:04:29] "GET /admin/retailFilters/filter/1/change/ HTTP/1.1" 200 5789

很抱歉,如果有人问过无数次,但我已经花了5个小时在互联网上搜索这个问题并且仍然没有成功。

有人能指出我失踪的东西吗?

1 个答案:

答案 0 :(得分:7)

您只需要更改settings.py:

MEDIA_ROOT = os.path.join(parent_dir, 'media/')
MEDIA_URL = '/media/'
#------------^