Git Product home page Git Product logo

django-form-file-upload's Introduction

Django 表單上傳檔案測試

執行

$ cd src

$ python manage.py migrate

$ python python manage.py runserver

settings

INSTALLED_APPS = [
    ...
    "rest_framework",
    ...
]
REST_FRAMEWORK = {
    "DEFAULT_PARSER_CLASSES": [
        "rest_framework.parsers.MultiPartParser",
    ],
    "DEFAULT_RENDERER_CLASSES": [
        "rest_framework.renderers.TemplateHTMLRenderer",
    ],
}
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            BASE_DIR / "templates",
        ],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]
STATIC_URL = "static/"

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

STATIC_ROOT = BASE_DIR / "assets"

MEDIA_URL = "media/"

MEDIA_ROOT = BASE_DIR / "media"

urls

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.views.generic import RedirectView

from app.books.views import books_view

urlpatterns = [
    path("admin/", admin.site.urls),
    path("books/", books_view, name="books"),
    path("", RedirectView.as_view(url="books")),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views

from django.shortcuts import redirect
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.request import Request
from rest_framework.response import Response

from app.books.models import Book
from app.books.serializers import BookSerializer


@api_view(["GET", "POST"])
def books_view(request: Request) -> Response:
    if request.method == "POST":
        serializer = BookSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return redirect("books")

    return Response(
        data={
            "title": "Hello world ~~~",
            "data": Book.objects.all(),
        },
        status=status.HTTP_200_OK,
        template_name="books.html",
    )

books.html

{% extends "base.html" %}

{% block content %}
    <h2>{{ title | default_if_none:"Books" }}</h2>

    <form action="{% url 'books' %}" enctype="multipart/form-data" method="POST">
        {% csrf_token %}
        <div class="mb-3">
            <label for="title" class="form-label">書名</label>
            <input type="text" class="form-control" name="title" id="title" placeholder="請輸入書名">
        </div>
        <div class="mb-3">
            <label for="description" class="form-label">描述</label>
            <textarea class="form-control" name="description" id="description" rows="3"
                      placeholder="請輸入描述"></textarea>
        </div>
        <div class="mb-3">
            <label for="cover_image" class="form-label">書本封面</label>
            <input class="form-control" type="file" name="cover_image" id="cover_image">
        </div>
        <input type="submit" class="btn btn-primary" value="送出">
    </form>
{% endblock %}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.