Rizel Tane
Rizel Tane

Reputation: 99

django create form with materialize modal

As far as I tried, I cannot get the form data or send it to the django view function through the materialize css modal. I'm just a novice in js, ajax and related.
The "CreateView" action for my form is running outside, but it is required to be inside a modal and still get the benefits of using django form validation, security, and so on. I'm using Django version 1.11.
The project has two applications: main that loads the main page, and library that performs actions on book objects. A book has only a title, and so there's a form defined like:

library/forms.py:

from django import forms
from .models import Book

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

the project urls definition, 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),
]

Home view, main/views.py:

from django.shortcuts import render

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

main/templates/main/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>

The library's urls, 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'),
]

library/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')

library/templates/library/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>

Any help will be appreciated.

Upvotes: 1

Views: 2591

Answers (1)

Rizel Tane
Rizel Tane

Reputation: 99

Changing a href link and setting the place of html code where the content will be rendered
main/templates/main/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 to render the form in the modal through django's routes
main/templates/main/home.html:

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

Finally, book form specifying the way back.
_library/templates/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>

Upvotes: 1

Related Questions