USER
USER

Reputation: 17

django edit page and update

I am having a HTML page which displays the details fetched from my db which is already stored. I have to edit my details and the details have to replace my old contents id db (i.e; overwitten into the same field). I'm using MySQL db. I have added my codes. When I submit the form it is not getting updated into db. Check, whether this code is correct?

models.py

class js_details(models.Model):
    user      = models.ForeignKey(User, unique=True)
    #PERSONAL INFO
    fname     = models.CharField(max_length=30)
    contactno = models.IntegerField(max_length=30)
    emailid   = models.EmailField(max_length=30)
    dob       = models.IntegerField(max_length=30)

forms.py

class EditForm(forms.ModelForm):
class Meta:
    model = js_details
    exclude = ('user')

views.py

def editresume(request):
    user = request.user
    profile_info = js_details.objects.filter(user_id=user)
    profile_data = { "profile_detail" : profile_info }
    if request.method == "POST":
        edit = EditForm(request.POST, request.FILES, instance = request.user.get_profile())

        if edit.is_valid():
            edit.save()        
            return HttpResponseRedirect('/accounts/Profile/')
    else:
        edit = EditForm(instance = request.user.get_profile())
    return render_to_response('registration/personal_information.html', profile_data, context_instance=RequestContext(request))

personal_information.html

<h4> Personal Details</h4>
<table width="90%" border="0">
    <tr>
        <td width="30%">
            <p> Name </p>
            <p> Contact Number </p>
            <p> Email ID</p>
            <p>DateOfBirth</p>
        </td>
        <td width="30%">
            <p>{% for detail in profile_detail %}{{ detail.fname}} {{ detail.lastname}}{% endfor %}</p>
            <p>{% for detail in profile_detail %}{{ detail.pri_contact }}{% endfor %}</p>
            <p>{{ user.email }}</p>
            <p>{% for detail in profile_detail %}{{ detail.dob }}{% endfor %}</p>
        </td>
        <td width="30%">
            <p><a href="">Edit</a></p>
        </td>
    </tr>
</table>

Upvotes: 0

Views: 3475

Answers (2)

Ezix
Ezix

Reputation: 126

OK, you want to display an html page with a form, allowing users to change their settings.

So what we want to do is:

  1. If there is a request (ie the user sent the form), process it.
  2. If there isn't a request, display the form.

Django already has a built-in form library that will help you to (1) display the form and (2) check the data for errors. Here's a simple tutorial to work with it.

Now this is the pseudo-code you can use in order to do this.

#views.py

if there is a request (request.method == "POST")
    get your form
    if the data is correct (form.is_valid())
       retreive the data from your form
       save it it your db
    else
       render your html and pass the form you retrieved (that will display errors)
else
    render and pass a blank form

#forms.py

class myform
    different inputs

#html file

{% if form.errors %}
    <p style="color: red;">{{ form.errors}}</p>
{% endif %}

<form action="." method="POST">{% csrf_token %}
    {{ form.as_p }}
<input type="submit" name="formname" value="Edit !">
</form>

Tell me if you want more details.

Upvotes: 2

emigue
emigue

Reputation: 502

You need to define a Form o ModelForm for js_details. This form allow user to edit js_details data. Then, define a new view that process this form to update db data from previous form. Reference this new view from post action in form requiered by profile_edit.html.

Upvotes: 0

Related Questions