add media backend
This commit is contained in:
parent
696717d138
commit
f257da3567
13 changed files with 133 additions and 2 deletions
0
app/media/__init__.py
Normal file
0
app/media/__init__.py
Normal file
10
app/media/admin.py
Normal file
10
app/media/admin.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
@admin.decorators.register(models.Image)
|
||||
class ImageAdmin(admin.ModelAdmin):
|
||||
search_fields = ['file']
|
||||
list_display = ('image_tag', 'file')
|
||||
readonly_fields = ('image_tag', 'get_absolute_url')
|
||||
6
app/media/apps.py
Normal file
6
app/media/apps.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MediaConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'app.media'
|
||||
23
app/media/migrations/0001_initial.py
Normal file
23
app/media/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 4.0.4 on 2022-04-22 17:11
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Image',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('modified', models.DateTimeField(auto_now=True)),
|
||||
('file', models.ImageField(blank=True, null=True, upload_to='image')),
|
||||
],
|
||||
),
|
||||
]
|
||||
0
app/media/migrations/__init__.py
Normal file
0
app/media/migrations/__init__.py
Normal file
25
app/media/models.py
Normal file
25
app/media/models.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from django.db import models
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
class Image(models.Model):
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
modified = models.DateTimeField(auto_now=True)
|
||||
|
||||
file = models.ImageField(upload_to='image', null=True, blank=True)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return self.file.url
|
||||
get_absolute_url.short_description = 'URL'
|
||||
|
||||
def image_tag(self):
|
||||
from django.utils.html import mark_safe
|
||||
return mark_safe('<img src="%s" style="max-width:256px" />' % self.get_absolute_url())
|
||||
image_tag.short_description = 'Preview'
|
||||
|
||||
def __str__(self):
|
||||
if self.file:
|
||||
name = self.file.name.split('/')[-1]
|
||||
else:
|
||||
name = 'None'
|
||||
return name
|
||||
3
app/media/tests.py
Normal file
3
app/media/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
app/media/views.py
Normal file
3
app/media/views.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Loading…
Add table
Add a link
Reference in a new issue