Answers for "python basename"

1

python get base directory

import os
os.path.dirname(os.path.realpath(__file__))
Posted by: Guest on May-11-2020
0

os path splitext

import os
basename, ext = os.path.splitext('inputfile.txt')
Posted by: Guest on November-24-2020
0

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
Posted by: Guest on March-30-2021

Python Answers by Framework

Browse Popular Code Answers by Language