简体   繁体   中英

How to upload image file from django admin panel ?

I am making a personal website using django 1.10 I want to upload my website logo from admin for why I can edit this in future. So that I have wrote website app and the models of website is :

from future import unicode_literals

from django.db import models

# Create your models here.
class Website(models.Model):
    title = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    logo = models.ImageField(max_length=255, upload_to='images/')

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

Admin panel: 在此输入图像描述

But image is not uploaded in media/images folder. Please help me about this issue.

Finally, I solved this problem, I had to change urls.py as like:

from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin

    urlpatterns = [
        url(r'^', include("website.urls", namespace='home')),
        url(r'^admin/', admin.site.urls),

    ]

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

It saves inside project folder if I change this in settings.py:

Before:

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media_cdn")

After:

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(PROJECT_ROOT), "media_cdn")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM