Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:37:58+00:00 2026-06-06T22:37:58+00:00

Someone has asked the exact same question in April, without any answer. But since

  • 0

Someone has asked the exact same question in April, without any answer. But since he provided too little information; the question was abandoned.

I have the same problem. Within a main_page.html I have this line:

<a href="/contact/edit/{{ item.id }}" title="Edit">edit</a>

Once you click there, the edit template shall appear inside a twitter bootstrap modal.

url.py

(r'^contact/edit/(?P<contact_id>\d+)/$', contact_view),

view.py

def contact_view(request, contact_id=None):
    profile = request.user.get_profile()
    if contact_id is None:
        contact = Contact(company=profile.company)
        template_title = _(u'Add Contact')
    else:
        contact = get_object_or_404(profile.company.contact_set.all(), pk=contact_id)
        template_title = _(u'Edit Contact')
    if request.POST:
        if request.POST.get('cancel', None):
            return HttpResponseRedirect('/')
        form = ContactsForm(profile.company, request.POST, instance=contact)
        if form.is_valid():
            contact = form.save()
            return HttpResponseRedirect('/')
    else:
        form = ContactsForm(instance=contact, company=profile.company)
    variables = RequestContext(request, {'form':form, 'template_title': template_title})
    return render_to_response("contact.html", variables)

This is usually how the contact.html would look like:

        <form class="well" method="post" action=".">
            {% csrf_token %}
            {{form.as_p}}
            <input class="btn btn-primary" type="submit" value="Save" />
            <input name="cancel" class="btn" type="submit" value="Cancel"/>
        </form>

I could put that inside a <div class="modal-body">.
But then how do I open the modal from view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:37:59+00:00Added an answer on June 6, 2026 at 10:37 pm

    Unless you need to use the contact form outside of the modal, this should work for you. If you do need to use it elsewhere, maintain two versions (one modal, one not). Also, a tip – give django-crispy-forms a lookover – it helps you render forms with twitter-bootstrap classes.

    contact.html:

    <div class="modal hide" id="contactModal">
    <form class="well" method="post" action="/contact/edit/{{ item.id }}">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Editing Contact</h3>
      </div>
      <div class="modal-body">
           {% csrf_token %}
           {{form.as_p}}
      </div>
      <div class="modal-footer">
           <input class="btn btn-primary" type="submit" value="Save" />
           <input name="cancel" class="btn" type="submit" value="Cancel"/>
      </div>
    </form>
    </div>
    

    main_page.html

    <html>
    ...
    
    <a data-toggle="modal" href="#contactModal">Edit Contact</a>
    
    {% include "contact.html" %}
    
    ...
    </html>
    

    Edit:

    Ok, so now that I know that you have potentially multiple forms, it’s probably a bad idea to render each form hidden within the html. You probably want to go to an ajax-y version, and load each form on demand. I’m assuming here that on form submit, the whole page will reload. If you want to asynchronously submit the form, there are answers elsewhere.

    We’ll start by re-defining the contact.html fragment. It should render within a modal, and contain all the markup necessary to play nice with the modal. The contact view that you have originally is fine – except that if the form is invalid, you’ll end up rendering the contact.html and nothing else.

    <form class="well contact-form" method="post" action="/contact/edit/{{ item.id }}">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Editing Contact</h3>
      </div>
      <div class="modal-body">
           {% csrf_token %}
           {{form.as_p}} <!-- {{form|crispy}} if you use django-crispy-forms -->
      </div>
      <div class="modal-footer">
           <input class="btn btn-primary" type="submit" value="Save" />
           <input name="cancel" class="btn" type="submit" value="Cancel"/>
      </div>
    </form>
    

    And now, your main_page.html:

    <html>
    .. snip ..
    
    <a class="contact" href="#" data-form="/contact/edit/{{ item.id }}" title="Edit">edit</a>
    <a class="contact" href="#" data-form="/contact/edit/{{ item.id }}" title="Edit">edit</a>
    <a class="contact" href="#" data-form="/contact/edit/{{ item.id }}" title="Edit">edit</a>
    
    <div class="modal hide" id="contactModal">
    </div>
    
    <script>
        $(".contact").click(function(ev) { // for each edit contact url
            ev.preventDefault(); // prevent navigation
            var url = $(this).data("form"); // get the contact form url
            $("#contactModal").load(url, function() { // load the url into the modal
                $(this).modal('show'); // display the modal on url load
            });
            return false; // prevent the click propagation
        });
    
        $('.contact-form').live('submit', function() {
            $.ajax({ 
                type: $(this).attr('method'), 
                url: this.action, 
                data: $(this).serialize(),
                context: this,
                success: function(data, status) {
                    $('#contactModal').html(data);
                }
            });
            return false;
        });
    </script>
    
    .. snip ..
    </html>
    

    This is all untested, but it should give you a good place to start/iterate from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly straightforward question here, but I can't seem to find someone who has asked
A similar question has been asked before by someone else , but there were
Probably someone has already asked this question but I'm not sure what I'm looking
Someone else has already asked a somewhat similar question: Validate an Xml file against
I'm sure someone has asked this before, but I'm struggling to find where. I'm
I notice this question has been asked a few times but I don't really
I'm not sure if this question has been asked before but what are the
Someone has asked me to look at their code to do a few little
Not sure if someone has already asked this, but I see a strange behavior
I realize that this question has been asked and has been answered here but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.