{"id":4673,"date":"2023-08-17T17:38:37","date_gmt":"2023-08-17T08:38:37","guid":{"rendered":"https:\/\/417.run\/?p=4673"},"modified":"2023-08-17T17:38:37","modified_gmt":"2023-08-17T08:38:37","slug":"admin-upload-image","status":"publish","type":"post","link":"https:\/\/417.run\/pg\/python\/django\/admin-upload-image\/","title":{"rendered":"Django\u306e\u7ba1\u7406\u753b\u9762\u3067\u753b\u50cf\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Django\u306e\u7ba1\u7406\u753b\u9762\u3067\u753b\u50cf\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u624b\u9806\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n\u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9:\u307e\u305a\u3001\u753b\u50cf\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f <code>Image<\/code> \u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-python\">from django.db import models\n\nclass Image(models.Model):\n    title = models.CharField(max_length=255)\n    image = models.ImageField(upload_to=&#039;images\/&#039;)<\/code><\/pre>\n<p><code>ImageField<\/code> \u3092\u4f7f\u7528\u3057\u3066\u3001\u753b\u50cf\u3092\u4fdd\u5b58\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<code>upload_to<\/code> \u306f\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u753b\u50cf\u304c\u4fdd\u5b58\u3055\u308c\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u8a2d\u5b9a\u306e\u5909\u66f4:\u30a4\u30e1\u30fc\u30b8\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u305f\u3081\u306b\u3001\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u8a2d\u5b9a\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-bash\">pip install pillow<\/code><\/pre>\n<p><code>Pillow<\/code> \u306f\u3001Django\u306e <code>ImageField<\/code> \u3067\u753b\u50cf\u51e6\u7406\u3092\u884c\u3046\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002admin.py\u306e\u8a2d\u5b9a:<code>admin.py<\/code> \u30d5\u30a1\u30a4\u30eb\u306b\u753b\u50cf\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u305f\u3081\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-python\">from django.contrib import admin\nfrom .models import Image\n\n@admin.register(Image)\nclass ImageAdmin(admin.ModelAdmin):\n    list_display = [&#039;title&#039;, &#039;image&#039;]<\/code><\/pre>\n<p>\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306e\u8a2d\u5b9a:\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\uff08<code>settings.py<\/code>\uff09\u306b\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-python\">MEDIA_URL = &#039;\/media\/&#039;\nMEDIA_ROOT = os.path.join(BASE_DIR, &#039;media\/&#039;)<\/code><\/pre>\n<p>\u3055\u3089\u306b\u3001<code>urls.py<\/code> \u30d5\u30a1\u30a4\u30eb\u306b\u3082\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-python\">from django.conf import settings\nfrom django.conf.urls.static import static\n\nurlpatterns = [\n    # ... \u305d\u306e\u4ed6\u306eURL\u8a2d\u5b9a ...\n]\n\nif settings.DEBUG:\n    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)<\/code><\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001DEBUG \u30e2\u30fc\u30c9\u304c True \u306e\u3068\u304d\uff08\u3064\u307e\u308a\u3001\u958b\u767a\u74b0\u5883\u3067\u52d5\u4f5c\u3057\u3066\u3044\u308b\u3068\u304d\uff09\u306b\u306e\u307f\u3001\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u3092\u30cf\u30f3\u30c9\u30eb\u3057\u307e\u3059\u3002\u672c\u756a\u74b0\u5883\u3067\u306f\u3001\u901a\u5e38\u3001\u30a6\u30a7\u30d6\u30b5\u30fc\u30d0\u30fc\uff08\u4f8b: Nginx \u3084 Apache\uff09\u304c\u30e1\u30c7\u30a3\u30a2\u30d5\u30a1\u30a4\u30eb\u306e\u914d\u4fe1\u3092\u62c5\u5f53\u3057\u307e\u3059\u3002<\/p>\n<p>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u66f4\u65b0:\u65b0\u3057\u304f\u8ffd\u52a0\u3057\u305f\u30e2\u30c7\u30eb\u306e\u305f\u3081\u306e\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-bash\">python manage.py makemigrations\npython manage.py migrate<\/code><\/pre>\n<p>\u3053\u308c\u3067\u3001Django\u306e\u7ba1\u7406\u753b\u9762\u304b\u3089\u753b\u50cf\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u7ba1\u7406\u753b\u9762\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3001\u65b0\u3057\u304f\u4f5c\u6210\u3057\u305f <code>Image<\/code> \u30e2\u30c7\u30eb\u306e\u30a8\u30f3\u30c8\u30ea\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u753b\u50cf\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>HTML\u3067\u8868\u793a<\/h2>\n<p>html\u3067img\u3068\u3057\u3066\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u30bb\u30c3\u30c8\u3057\u305f\u4f8b\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-html\">&lt;img src=&quot;{{ img.image.url }}&quot; alt=&quot;{{ img.title }}&quot; width=&quot;300&quot;&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Django\u306e\u7ba1\u7406\u753b\u9762\u3067\u753b\u50cf\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u6a5f\u80fd\u3092\u8ffd\u52a0\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u624b\u9806\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9:\u307e\u305a\u3001\u753b\u50cf\u3092\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306e\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u3053\u306e\u4f8b\u3067\u306f Image \u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 from djang &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":{"0":"post-4673","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-django","7":"nothumb"},"_links":{"self":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4673"}],"collection":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/comments?post=4673"}],"version-history":[{"count":3,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4673\/revisions"}],"predecessor-version":[{"id":4676,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4673\/revisions\/4676"}],"wp:attachment":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/media?parent=4673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/categories?post=4673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/tags?post=4673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}