如何从Django Modal提交表单

时间:2019-11-10 23:34:44

标签: python django bootstrap-modal

我正在尝试使用模式将视频上传到我的Django网站。目前,当我尝试提交表单时,出现错误CC = g++ CFLAGS = -g -Wall -Wextra -pedantic -std=c++11 SRC = main.cpp TARGET = app RM = rm -rf .PHONY: all clean all: $(TARGET) clean: $(RM) $(TARGET) $(TARGET): $(SRC) $(CC) $(CFLAGS) $^ -o $@

我一直在浏览Django教程以及bootstrap / html /指南,因为找不到关于为何不提交表单的明确答案。

base.html

'NoneType' object has no attribute 'is_ajax'

upload_videos.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <!-- Our style.css -->
        {% load static %}
        <link rel="stylesheet" href="/static/VR360/style.css?{% now "U" %}"/>

        <title>Umbra</title>
    </head>

    <body>
        <ul>
            <li><a href="#login">Login</a></li>
            <li><a href="{% url 'upload-videos' %}">Upload Videos</a></li>
            <li><a href="{% url 'homepage' %}">Home</a></li>
        </ul>
    <!-- Everything that extends base.html, the code will be "inside" these two block content commands-->
        <div id="page-wrap">
            {% block content %}
            {% endblock content %}
        </div>
    </body>
</html>

views.py

{% extends "VR360/base.html" %}
{% block content %}
    <h1>Upload Videos</h1>

    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
            Launch demo modal
    </button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <form method="post" enctype="multipart/form-data">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                    </div>
                    <div class="modal-body">
                        {% csrf_token %}
                        {{ form.as_p }}
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Upload File</button>
                    </div>
                </div>
            </div>
        </form>
    </div>


{% endblock %}

models.py

class VideoCreateView(BSModalCreateView):
    template_name = 'VR360/upload-videos.html'
    form_class = VideoForm

    def get(self, request):
        form = VideoForm()
        videos = Video.objects.all()
        context = {'form': form, 'videos': videos}
        return render(request, 'VR360/upload-videos.html', context)


    def post(self, request):
        form = VideoForm(request.POST, request.FILES)
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            form.save()

        videos = Video.objects.all()
        context = {'form': form, 'videos': videos}

        return render(request, 'VR360/upload-videos.html', context)

form..py

from django.db import models

# Create your models here.
class Video(models.Model):
    video = models.FileField(upload_to='videos/')
    comment = models.CharField(max_length=100)

    def __str__(self):
        return self.comment

当我尝试from django import forms from bootstrap_modal_forms.forms import BSModalForm from .models import Video class VideoForm(BSModalForm): class Meta: model = Video fields = ['video', 'comment'] 时发生错误。我最好的猜测是该表单为空是因为该表单是在模式上提交的,实际上并没有保存在网页上。

最终目标是正确保存表单,以便视频将显示在我的媒体文件夹中。

0 个答案:

没有答案