django使用materialize modal创建表单

时间:2018-05-05 02:18:56

标签: django forms modal-dialog materialize

据我所知,我无法获取表单数据或通过materialize css模式将其发送到django视图函数。我只是js,ajax和相关的新手 我的表单的“CreateView”操作在外部运行,但它必须位于模态内,并且仍然可以获得使用django表单验证,安全性等的好处。我正在使用Django版本1.11 该项目有两个应用程序:main加载主页面,以及对book对象执行操作的库。一本书只有一个标题,因此有一个表格定义如下:

库/ forms.py

from django import forms
from .models import Book

class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = [
            'title', 
        ]

项目网址定义, mysite / urls.py

from django.conf.urls import url, include
from django.contrib import admin
from main import views as main_views

urlpatterns = [
    url(r'^$', main_views.home, name='home'),
    url(r'^books/', include('library.urls')),
    url(r'^admin/', admin.site.urls),
]

主页视图, main / views.py

from django.shortcuts import render

def home(request):
    return render(request, 'main/home.html')

主/模板/主/ home.html做为

<html>
  <head>
    <title>A library</title>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
  <body>
    {% block body %}
    <div class="container">
      <!--a class="waves-effect waves-light btn modal-trigger" href="#modal1">Create Book</a-->
      <a class="waves-effect waves-light btn modal-trigger" data-target="add_book_modal" href="{% url 'books:create' %}">
        Create Book
      </a>
      <div class="divider"></div>
        <div class="section">
          <h5>Section 1</h5>
          <p>Stuff</p>
        </div>
      </div>
    {% endblock %}

    {% include 'library/book_form.html' %}

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
    <script>
    $( document ).ready(function() {
      $('.modal').modal();
    });
    </script>
  </body>
</html>

图书馆的网址,library / urls.py:

from django.conf.urls import url
from .views import BookCreateView
app_name='books'

urlpatterns = [
    url(r'^create/$', BookCreateView.as_view(),  name='create'),
]

库/ views.py

from django.shortcuts import render
from django.views.generic import CreateView
from django.urls import reverse_lazy

from .models import Book
from .forms import BookForm

class BookCreateView(CreateView):
    form_class = BookForm
    template_name = 'library/book_form.html' 
    success_url = reverse_lazy('home')

库/模板/库/ book_form.html

<div id="add_book_modal" class="modal">
  <div class="modal-content">
  {% block body %}
    <main>
    <h5>Add book information</h5>
    {% if form.errors.non_field_errors %}
      {{ form.errors.non_field_errors }}
    {% endif %}
    <form method="POST">
      {% csrf_token %}
      {{ form.non_field_errors }}
      {{ form.source.errors }}
      {{ form.source }}
      <!--Form content-->
      {{form.as_p}}
      <!--Form button-->
      <button class="btn waves-effect waves-light" type="submit" name="action">Submit
      </button>    
    </form>
    </main>
  {% endblock %}
  </div>
</div>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

更改 a href链接并设置将在其中呈现内容的html代码的位置
主/模板/主/ home.html的:

...
<a class="waves-effect waves-light btn modal-trigger" href="#modal1">Create Book</a>
...
<!-- Modal Structure -->
<div id="modal1" class="modal">
  <div class="modal-content">
    <!--Form content goes in here.-->
  </div>
</div>
{% endblock %}
<!-- django include tag removed -->

Jquery通过django的路径在模态中渲染表单
主/模板/主/ home.html的:

<script>
$( document ).ready(function() {
  $('.modal').modal();
  $('.modal-content').load("{% url 'books:create' %}");
});
</script>

最后,书的形式指明了回来的路 _LIBRARY /模板/库/ book_form.html _:

<h5>Add book information</h5>
{% if form.errors.non_field_errors %}
  {{ form.errors.non_field_errors }}
{% endif %}
<form method="POST" action="{% url 'books:create' %}">
  {% csrf_token %}
  {{ form.non_field_errors }}
  {{ form.source.errors }}
  {{ form.source }}
  <!--Form content-->
  {{form.as_p}}
  <!--Form button-->
  <button class="btn waves-effect waves-light" type="submit" name="action">Submit form
  </button>    
</form>