Skip to content

Commit

Permalink
Added imagefield chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 19, 2018
1 parent 555c278 commit 274cc36
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
29 changes: 29 additions & 0 deletions docs/imagefield.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
How to show image from Imagefield in Django admin.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

In your :code:`Hero` model, you have an image field.::

headshot = models.ImageField(null=True, blank=True, upload_to="hero_headshots/")

By default it shows up like this:

.. image:: imagefield.png


You have been asked to change it to that the actual image also shows up on the change page. You can do it likethis::


@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):

readonly_fields = [..., "headshot_image"]

def headshot_image(self, obj):
return mark_safe('<img src="{url}" width="{width}" height={height} />'.format(
url = obj.headshot.url,
width=obj.headshot.width,
height=obj.headshot.height,
)
)

With this change, your imagefield looks like this:

.. image:: imagefield_fixed.png
Binary file added docs/imagefield_fixed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import csv
import sys
from django.http import HttpResponse
from django.utils.safestring import mark_safe


class IsVeryBenevolentFilter(admin.SimpleListFilter):
Expand Down Expand Up @@ -55,7 +56,17 @@ class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):

exclude = ['added_by',]

readonly_fields = ["father", "mother", "spouse"]
# fields = ["headshot_image"]

readonly_fields = ["father", "mother", "spouse", "headshot_image"]

def headshot_image(self, obj):
return mark_safe('<img src="{url}" width="{width}" height={height} />'.format(
url = obj.headshot.url,
width=obj.headshot.width,
height=obj.headshot.height,
)
)

# def get_readonly_fields(self, request, obj=None):
# if obj:
Expand Down
3 changes: 3 additions & 0 deletions heroes_and_monsters/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Meta:
arbitrariness_factor = models.PositiveSmallIntegerField(
help_text="How arbitrary this hero is?"
)

headshot = models.ImageField(null=True, blank=True, upload_to="hero_headshots/")

# relationships
father = models.ForeignKey(
"self", related_name="+", null=True, blank=True, on_delete=models.SET_NULL
Expand Down
3 changes: 3 additions & 0 deletions heroes_and_monsters/heroes_and_monsters/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
20 changes: 5 additions & 15 deletions heroes_and_monsters/heroes_and_monsters/urls.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
"""heroes_and_monsters URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

admin.site.site_header = "UMSRA Admin"
admin.site.site_title = "UMSRA Admin Portal"
Expand All @@ -27,3 +14,6 @@
path('entity-admin/', admin.site.urls),
path('event-admin/', event_admin_site.urls),
]

if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

0 comments on commit 274cc36

Please sign in to comment.