models.py:
from django.db import models class Info(models.Model): name = models.CharField(max_length = 200) surname = models.CharField(max_length = 200) date_of_birth = models.CharField(max_length = 200) bio = models.TextField() contacts = models.TextField() def __unicode__(self): return unicode(self.name + " " + self.surname)
forms.py:
from django.forms import ModelForm from models import Info class EditMyInfoForm(ModelForm): class Meta: model = Info fields = ['name', 'surname', 'date_of_birth', 'bio', 'contacts']
views.py:
def edit_my_info(request, template_name = "edit_page1.html"): info = Info.objects.get(pk=1) if request.POST: form = EditMyInfoForm(request.POST, instance=info) if form.is_valid(): form.save() if request.is_ajax(): return render_to_response("success.html") else: return redirect('editmyinfo_success') else: form = EditMyInfoForm(instance=info) return render(request,template_name,{"form":form, "title":"Edit page"})
edit_page.html:
{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<h3><a href ="/">Main page</a></h3>
<div class="info">
<form id="myForm" action="" method="POST" >
<div id="ajaxwrapper">
{% csrf_token %}
{{ form.non_field_errors }}
{{form.as_p}}
<p id="sendwrapper"><input type="submit" value="save" id="sendbutton"/></p>
</form>
</div>
{% endblock %}
{% block scripts %}
<script src="{% static 'static/js/load_submissions.js' %}"></script>
{% endblock %}