python get base directory
import os
os.path.dirname(os.path.realpath(__file__))
python get base directory
import os
os.path.dirname(os.path.realpath(__file__))
os path splitext
import os
basename, ext = os.path.splitext('inputfile.txt')
def url(self): return os.path.join('',settings.MEDIA_URL+'uploads/', os.path.basename(str(self.image))) what is used of it
import os
from django.db import models
from django.conf import settings
from django.utils.safestring import mark_safe
# the settings for MEDIA_ROOT and MEDIA_URL come from the project settings
# but could be overridden in the model
# MEDIA_ROOT = '/home/<user>/project/imgproject/media_cdn'
# MEDIA_URL = '/media'
# Create your models here.
class Image(models.Model):
# allows for an image to be either stored in the MEDIA_ROOT path or
# be a reference to an external URL to an image.
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length = 255)
description = models.TextField(blank=True)
image = models.ImageField(upload_to=settings.MEDIA_ROOT, blank=True)
externalURL = models.URLField(blank=True)
def url(self):
# returns a URL for either internal stored or external image url
if self.externalURL:
return self.externalURL
else:
# is this the best way to do this??
return os.path.join('/',settings.MEDIA_URL, os.path.basename(str(self.image)))
def image_tag(self):
# used in the admin site model as a "thumbnail"
return mark_safe('<img src="{}" width="150" height="150" />'.format(self.url()) )
image_tag.short_description = 'Image'
def __unicode__(self):
# add __str__() if using Python 3.x
return self.title
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us