0

Im trying to submit a form from a dialog window and show the response in the same dialog window. Right now im trying with a Django view that returns a form (with Valdiation Errors) as a html-string.

template = "register.html"
html = render_to_string(template, {'form': form})
return HttpResponse(html, content_type="text/html; charset=utf-8") 

But i have some problems with the function in my dialog-modal. Im missing the part that replaces the content in the "modal/dialog" with the new html from the HttpResponse..

$('#registerform').submit(function(e){
e.preventDefault();
$.post('register', function(data){ 

//somthing is missing here..             
});
return false;    
});

Not sure about the formatting right now but you get the idea. If any expert could guide me in the right direction i would be a happy man! Thanks

user3199840
  • 525
  • 2
  • 13
  • 35

1 Answers1

0

This is an implementation with Django braces: https://github.com/brack3t/django-braces

//Inside your click, event whatever call...
...
// Get the token initialy, this is the way to do it 
// using the jQuery cookie plugin 
var $csrftoken = $.cookie('csrftoken');
$.ajax({
    type: 'POST',
    url: '/your/post/url/',
    crossDomain: false,
    beforeSend: function(xhr) {
        xhr.setRequestHeader("X-CSRFToken", $csrftoken);
    },
    success: function(ctx) {
        """
        ctx contains the data your view returns, 
        If you are using Django braces, you could implement the 
        AjaxResponseMixin mixin which allows you to return a dict 
        to the view.
        """
    },
});
...

views.py

class AjaxDosomethingView(JSONResponseMixin, AjaxResponseMixin, View):

    def post_ajax(self, request, *args, **kwargs):
        """
        Do some stuff and create the response object
        """
        myid = request.POST.get('somevar')
        result = MyModel.objects.get(id=myid)
        response = {'id': result.id, 'title': result.title}

        return self.render_json_response(response, 200)
petkostas
  • 7,250
  • 3
  • 26
  • 29
  • Thank you for your answer! I will look into Django braces I've not heard of it before. Is this the simple way to do it? I seems very complicated to work with dialogs/modals with Django in general. What i've been trying to do is described in the answer here: http://stackoverflow.com/questions/5988611/how-to-reopen-a-django-form-in-a-jquery-dialog-when-the-form-validation-fails-in/5988964#5988964 – user3199840 Jan 21 '14 at 21:59
  • Django braces are mixins to add functionality to your view, such as ajax and permission checking, modal handling is bit different story, if you are using bootstrap you need to move to a few hacks as modal data is usually set once and your cannot override it. – petkostas Jan 21 '14 at 22:01
  • Ok! No im trying to use jquery dialog box as a modal. All i want to do really is to render different views(child templates) inside those dialog boxes because i have a leaflet map with markers on the base.html and I don't want it to reaload if someone clicks the menus. But when it comes to reloading content in the same dialogbox i've been stuck on this problems for days now.. – user3199840 Jan 21 '14 at 22:11
  • Django braces allows you to create ajax response views (unless you want to use a REST framework such as tastypie or django-rest), you can use them in your urls like /ajax/someview/ and return ajax specific reponses to your js. So pretty much you can accomplish what you need with them :) – petkostas Jan 21 '14 at 22:13
  • Yes i guess i have to take a look deeper into those. But I'm a beginner with python and django so I'm trying to look for the not so complicated solutions first! Just to make things work so i can develop it further with more experience. Thanks – user3199840 Jan 21 '14 at 22:39