Django - Allow User to Edit Profile and then Show Updated Profile fields -


i have form populating user profile information, when click save, doesn't update.

any clues/hints part need modify appreciated.

thanks in advance!

views.py

def profile_view(request):     user = request.user     form = editprofileform(initial={'first_name':user.first_name, 'last_name':user.last_name})     context = {         "form": form     }     return render(request, 'profile.html', context)  def edit_profile(request):      user = request.user     form = editprofileform(request.post or none, initial={'first_name':user.first_name, 'last_name':user.last_name})     if request.method == 'post':         if form.is_valid():               user.first_name = request.post['first_name']             user.last_name = request.post['last_name']              user.save()             return httpresponseredirect('%s'%(reverse('profile')))      context = {         "form": form     }      return render(request, "edit_profile.html", context) 

forms.py

class editprofileform(forms.modelform):      first_name = forms.charfield(label='first name')     last_name = forms.charfield(label='last name')       class meta:         model = user         fields = ['first_name', 'last_name'] 

edit_profile.html

{% extends "base_site.html" %}  {% block content %}  <h1>edit profile</h1>   <form method="post" action="/accounts/profile/" class="" />     {% csrf_token %}     {{ form.as_p }}     <button type="submit">save</button> </form>   {% endblock %} 

urls.py

from django.conf.urls import url . import views  urlpatterns = [      url(r'^register/$', 'accounts.views.registration_view', name='auth_register'),     url(r'^login/$', 'accounts.views.login_view', name='auth_login'),     url(r'^logout/$', 'accounts.views.logout_view', name='auth_logout'),     url(r'^profile/$', 'accounts.views.profile_view', name='profile'),     url(r'^profile/edit/$', 'accounts.views.edit_profile', name='edit_profile'), ] 

the action in form posting profile_view , not edit_profile , forms self closing aren't being posted correctly.

change this:

<form method="post" action="/accounts/profile/" class="" /> 

to this:

<form method="post" action="/accounts/profile/edit" class="" > 

or better, use django url template tag:

<form method="post" action="{% url 'edit_profile' %}" class="" > 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -