HM Tanbir
HM Tanbir

Reputation: 1020

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: enter image description here

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

Upvotes: 4

Views: 4567

Answers (2)

HM Tanbir
HM Tanbir

Reputation: 1020

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)

Upvotes: 2

HM Tanbir
HM Tanbir

Reputation: 1020

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")

Upvotes: 0

Related Questions