A
A
AprilSnow2015-06-07 20:16:30
JavaScript
AprilSnow, 2015-06-07 20:16:30

How to attach ajax to this form?

You need to glue ajax so that the page is not updated when a post request is sent to the server.

Модель и форма:

from django.db import models
from django import forms

class Cinema(models.Model):
  theater_name = models.TextField(max_length = 35, blank=False)
  theater_address = models.TextField(max_length=35, blank=False)
  cinema_name = models.TextField(max_length=35, blank=False)


class CinemaForm(forms.ModelForm):
    class Meta:
        model = Cinema
        fields = ('theater_name', 'theater_address', 'cinema_name')


Представление:

def cinema(request):
  form = CinemaForm(request.POST or {})
  if request.POST and form.is_valid():
    form.save()
    return redirect("/")
  return render(request, "theatername.html", {"cinema": Cinema.objects.all(), "form": form})


Шаблон:

{% block theater %}
  {% for theater in cinema %}
  <table class="table table-bordered">
  <thead>
    <tr>
      <th>Адрес</th>
      <th>Название кинотеатра</th>
    </tr>
    </thead>
  <tbody>
    <tr>
      	<td>{{ theater.theater_address }}</td>
      	<td>{{ theater.theater_name }}</td>
    </tr>
    </tbody>
  </table>
  <br>
  {% endfor %}
  <form method="post">
  {% csrf_token %}
  {{ form.as_ul }}
  <input class="btn btn-success" type="submit" value="Добавить">
  </form>
{% endblock %}


How to do it? Tell me at least the direction, very little mana for a bunch of django + ajax

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Klimenko, 2015-06-08
@AprilSnow

very little mana for django + ajax

For django, both a normal request and ajax look almost the same (normal libraries will add the https://docs.djangoproject.com/en/1.8/ref/request-... header to the request ), the differences will start in that, depending on from the server response, you need to write the behavior on the page, it is enough to provide for the server to return everything you need to dynamically change the page (and it will happen as you write it, and Django has nothing to do with it)

S
sim3x, 2015-06-07
@sim3x

https://docs.djangoproject.com/en/1.8/ref/csrf/#ajax
https://realpython.com/blog/python/django-and-ajax...
https://www.google.com/ ?q=django+form+ajax

About 472,000 results

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question