0

I want to load a form of a django template in a Jquery popup. The form loads fine in the popup's div but when i click submit nothing happens. The form itself seems to work on it's own url but not in the popup of the other page where i try to load it. Someone know whats the Problem im pretty new to django and jquery.

The form i want to load into the popup:

<form action="" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Save</button>
</form> 

My Jquery:

$(document).on('ready', function(){
    var button = $('#button');
    button.click(function(){
        $('#newsletter').load('/newsletter #newsletterform').css('display', 'block');
    });
});

My Views:

def newsletter(request):
    recipients = Recipient.objects.order_by('adress')
    return render(request, 'newsletter/adress_list.html', {'recipients':recipients})

def add(request):
    if request.method == "POST":
        form = NewsletterForm(request.POST)
        if form.is_valid():
            recipient = form.save(commit=False)
            recipient.save()
            return redirect('/join')
    else:
        form = NewsletterForm()
    return render(request, 'newsletter/add.html', {'form': form})

My Newsletter App URLs:

urlpatterns = patterns('',
    url(r'^list/$', views.newsletter),
    url(r'^$', views.add, name='add'),
)

Django-CMS Url:

url(r'^newsletter/', include('newsletter.urls', 
4
  • 1
    What error do you get? Also, what URL is the form supposed to submit to? You're loading it from a different URL...but then processing it in the current URL? Commented Mar 22, 2015 at 12:36
  • Set the action parameter of the form to the url you want the form to post to. Commented Mar 22, 2015 at 12:39
  • No error its just the content of the form is not saved to the list like it usual does. I think it cant work because of the URL thing Commented Mar 22, 2015 at 12:44
  • when i set action to: action="{% url newsletter %}". Form no longer appears in the popup Commented Mar 22, 2015 at 12:50

1 Answer 1

1

You need to change the action of the form to point to your other URL/view. Assuming your add view exists at the /newsletter URL, you would change your form to:

<form action="/newsletter" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Save</button>
</form> 
Sign up to request clarification or add additional context in comments.

1 Comment

ok this works perfect, made some mistakes by integrating my app to django-cms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.