0

I want to save details in database and retrieve back to same page using Ajax. I added the code for your reference. Kindly share your ideas.

models.py

class Personal(models.Model):
    user=models.ForeignKey(User)
    name=models.CharField()
    dob = models.CharField()
    email = models.EmailField()
    address1 = models.CharField()
    address2 = models.CharField()
    country = models.CharField()
    state = models.CharField()
    city = models.CharField()

Views.py

def profile(request):
    userid=request.user.id
    personal=JSPersonal.objects.filter(user_id=userid)
    return render(request,'registration/profile.html', {'personal':personal})

templates(profile.html)

{% if personal %}
{% for p in personal %}
<p>Name : {{p.name}}</p>
<p>DOB : {{p.dob}}</p>
<p>Email : {{p.email}}</p>
<p>Address1 : {{p.address1}}</p>
<p>Address2 : {{p.address2}}</p>
<p>Country : {{p.country}}</p>
<p>State : {{p.state}}</p>
<p>City:{{p.city}}</p>
{% endfor %}
{%else%}
<p>Click <a href="#">Here</a> to add details</p>
{% endif %}

By clicking the "Here" model form get loaded here there is a space to enter the personal details.Here I need to store details in database and return back to same page once I click submit button in the model form. Only particular content get loaded not whole page.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user
  • 675
  • 2
  • 10
  • 19

2 Answers2

0

The basic idea is that you put an element with an ID around the data that will change, and target that id with a jQuery.load call.

see: Render a django table using ajax

Community
  • 1
  • 1
Thomas
  • 11,757
  • 4
  • 41
  • 57
0

I recommend that you use the forms for such tasks: https://docs.djangoproject.com/en/dev/topics/forms/

In this case, you can easily send your form with jQuery $.post():

$.post('/form/url/', $('form_selector').serialize())
  .done(function(data) { ... })
  .fail(function(jqXHR) { ... });

A more detailed response: How to POST a django form with AJAX & jQuery

Community
  • 1
  • 1
GreyZmeem
  • 630
  • 1
  • 6
  • 11