Search code examples
pythondjangoformspopup

I want to create django popup form in my project


I have created fee management system in django.

The problem is I am using simple form and for each form user have to navigate to separate page.

I want to create popup form in django. I have search many websites but can't get solution

enter image description here

In above window when user click on payment button pop-up form will be open. and when user click on submit button changes will be shown in same page.

Can anyone tell me how to solve this? or share code if you have work in same area.


Solution

  • Why not use bootstrap modals instead?

    For example https://pypi.org/project/django-bootstrap-modal-forms/

    Examples To see django-bootstrap-modal-forms in action clone the repository and run the examples locally:

    $ git clone https://github.com/trco/django-bootstrap-modal-forms.git
    $ cd django-bootstrap-modal-forms
    $ python -m pip install -r requirements.txt
    $ python manage.py migrate
    $ python manage.py runserver
    

    Signup form in Bootstrap modal For explanation how all the parts of the code work together see paragraph Usage. To test the working solution presented here clone and run Examples. forms.py

    from django.contrib.auth.forms import UserCreationForm
    from django.contrib.auth.models import User
    from bootstrap_modal_forms.mixins import PopRequestMixin, CreateUpdateAjaxMixin
    
    
    class CustomUserCreationForm(PopRequestMixin, CreateUpdateAjaxMixin,
                                 UserCreationForm):
        class Meta:
            model = User
            fields = ['username', 'password1', 'password2']
    

    signup.html

    {% load widget_tweaks %}
    
    <form method="post" action="">
      {% csrf_token %}
    
      <div class="modal-header">
        <h3 class="modal-title">Sign up</h3>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
    
      <div class="modal-body">
    
        <div class="{% if form.non_field_errors %}invalid{% endif %} mb-2">
          {% for error in form.non_field_errors %}
            {{ error }}
          {% endfor %}
        </div>
    
        {% for field in form %}
          <div class="form-group">
            <label for="{{ field.id_for_label }}">{{ field.label }}</label>
            {% render_field field class="form-control" placeholder=field.label %}
            <div class="{% if field.errors %} invalid{% endif %}">
              {% for error in field.errors %}
                <p class="help-block">{{ error }}</p>
              {% endfor %}
            </div>
          </div>
        {% endfor %}
      </div>
    
      <div class="modal-footer">
        <button type="button" class="submit-btn btn btn-primary">Sign up</button>
      </div>
    
    </form>
    

    views.py

    from django.contrib.messages.views import SuccessMessageMixin
    from django.urls import reverse_lazy
    from django.views import generic
    from bootstrap_modal_forms.mixins import PassRequestMixin
    from .forms import CustomUserCreationForm
    
    class SignUpView(PassRequestMixin, SuccessMessageMixin, generic.CreateView):
        form_class = CustomUserCreationForm
        template_name = 'accounts/signup.html'
        success_message = 'Success: Sign up succeeded. You can now Log in.'
        success_url = reverse_lazy('index')
    

    urls.py

    from django.urls import path
    from . import views
    
    app_name = 'accounts'
    urlpatterns = [
        path('signup/', views.SignUpView.as_view(), name='signup')
    ]
    

    .html file containing modal, trigger element and script instantiating modalForm

    <div class="modal fade" tabindex="-1" role="dialog" id="modal">
      <div class="modal-dialog" role="document">
        <div class="modal-content"></div>
      </div>
    </div>
    
    <button class="signup-btn btn btn-primary" type="button" name="button">Sign up</button>
    
    <script type="text/javascript">
      $(function () {
        // Sign up button
        $(".signup-btn").modalForm({formURL: "{% url 'accounts:signup' %}"});
    
      });
    </script>