Monday, 15 August 2011

python - Django with ajax render -


I am quite new to Django and I try to make my first application. However, I had to face a problem now.

I have a form (to add a warning - my MEDEL) and a div with a list of all alerts and with the list of all the alerts if I fill all the fields correctly, the alert is added Goodbye refreshed using AJX The problem is with form recognition

Without Ajax I was sending bound forms with error list for each area. I used the Digengo Form API It was working properly but I can not refresh the error list if no form was successful.

I do not find a render of the form or present without context - all the fields are empty Of course, if I made a mistake in a field and it is working fine without Ajax, then They should not be empty.

Code bunch, the first visual method I've seen:

  def add_alert (request): Report = __get_or_create_empty_Report (request) if request.method == 'POST 'form = AlertForm (request.POST) If form.is_valid (): alert_project = "" ALERT_NAME = "" alert_ticket = "" alert_date = "" ALERT_TYPE = "" alert_comment = "" If form.cleaned_data.has_key (' alert_project '): alert_project = form.cleaned_data [' alert_project '] if form.cleaned_data.has_key (' ALERT_NAME '): ALERT_NAME = form.cleaned_data [' ALERT_NAME '] if form.cleaned_data.has_key (' alert_ticket '): alert_ticket = form .cleaned_data [ 'alert_ticket'] if form.cleaned_data.has_key ( 'alert_date'): alert_date = form.cleaned_dat a [ 'alert_date'] if form.cleaned_data.has_key ( 'ALERT_TYPE'): AL ERT_TYPE = form.cleaned_data [ 'ALERT_TYPE'] if form.cleaned_data.has_key ( 'alert_comment'): alert_comment = form.cleaned_data [ 'alert_comment'] = warning alert () alert.alt_prj_id = get_object_or_404 (project, prj_id = alert_project) alert .alt_name = ALERT_NAME alert.alt_ticket = alert_ticket alert.alt_date = alert_date alert.alt_type = ALERT_TYPE comment comment () comment.com_value = alert_comment comment.save () Alert.alt_com_id = comment alert.alt_rep_id = report alert.save () alerts = Alert.objects.filter (alt_rep_id = report.rep_id) Returns render (request, 'main / theform.html', {'form': form, 'alerts': alert, 'error message': "Alert has been added "}): Form = alertform () alert = alert alert = A Lert.objects.filter (alt_rep_id = Reports .objects.filter (alt_rep_id = report.rep_id) ({form, 'alert' alert 'form'} request, 'main / theform.html', return). Rep_id) Submit submission (request, 'main / theform.html', {'form': form, 'alerts': alerts, 'error_message': "Alert has not been added"})  

Here is a template template:

  {% extended 'main / base.html'%} {% block content%} & lt; Div id = "alert" & gt; & Lt; / Div & gt; For alert in {% Alerts%} & lt; P & gt; {{Alert.alt_name}} & lt; / P & gt; {% Endfor%} & lt; Form class = "col-md-12" action = "{% url 'main: add_alert'%}" method = "post" & gt; {% Csrf_token%} {{form.as_p}} & lt; Input type = "submit" value = "submit" /> & Lt; / Form & gt; {{Error_message}} {% endblock%}  

and simple script:

  $ (document) .ready (function () {// AJAX util $ ('Add-alert-form'). Submit (function (e) {e.preventDefault (); $ .ajax ({type: "POST", url: $ (this) .attr ("verb"), Data: $ (this) .Syrealize (), success: function (data) {$ ("#alert"). Load ("#alerts");}}}}}}}}  

I think I am doing it completely wrong but I do not know what is the correct way.

Then your thoughts will look like this:

  If Form.is_valid (): obj = form.save () data = serializers.serialize ('json', [obj,]) Other: data = json.dumps (dict ([(k, [v] Unicode for E E)), v. Form.errors.items ()])) HttpResponse Return () Data, Mime Type = 'Application / Jason')  

No comments:

Post a Comment