Models: class Delegate(models.Model): username = models.ForeignKey(User, verbose_name=_("User Name"),unique=True) resume = models.TextField(_("Resume"),blank=True,null=True) location = models.TextField(_("City/Organisation"),blank=True,null=True) occupation = models.ForeignKey(Occupation,blank=True,null=True, verbose_name = _("Occupation")) photo = ImageField(_("Photo"),upload_to="delphotos/", blank=True,null=True) class Admin: pass def __unicode__(self): return _(u"Username: %s") %(self.username) View: class Edituserform(forms.Form): def __init__(self, *args, **kwargs): super(Edituserform, self).__init__(*args, **kwargs) # Generate choices self.fields['occupation'].choices = [(chld.id, chld.name) for chld in Occupation.objects.all()] first_name = forms.CharField(max_length=30, label=_("First Name or Initials"), required=False) last_name = forms.CharField(max_length=30, label=_("Last Name"), required=False) occupation = forms.ChoiceField(label=_("Occupation"), choices=(), required=False) photo = forms.ImageField(label=_("Photo"), required=False) resume = forms.CharField(max_length=1000, label=_("Resume"), widget=forms.Textarea, required=False) location = forms.CharField(max_length=1000, label=_("City/Organisation"), widget=forms.Textarea, required=False) pass1 = forms.CharField(max_length=50,widget=forms.PasswordInput, label=_("Enter New Password"), required=False) pass2 = forms.CharField(max_length=50,widget=forms.PasswordInput, label=_("Enter New Password Again"), required=False) # Hidden fields to store the url of photo for display flname = forms.CharField(max_length=250,widget=forms.HiddenInput, label='', required=False) flurl = forms.CharField(max_length=250,widget=forms.HiddenInput, label='', required=False) def clean_pass2(self): if self.cleaned_data['pass1'] != self.cleaned_data['pass2']: raise forms.ValidationError('Passwords do not match') else: if len(self.cleaned_data['pass1']) < 6 and len(self.cleaned_data['pass1']) >0: raise forms.ValidationError('Password must be at\ least 6 letters long') @user_passes_test(lambda u: u.is_anonymous()==False ,login_url="/web/login/") def edituser(request): """view to edit a user's profile - this is saved in User and Delegate """ if request.POST: form = Edituserform(request.POST,request.FILES) if form.is_valid(): fm = form.cleaned_data newuser = User.objects.get(pk=request.user.id) newuser.first_name = fm['first_name'] newuser.last_name = fm['last_name'] if fm.get('pass1') and fm.get('pass2'): newuser.set_password(fm['pass1']) newuser.save() changed = False newdel,created = Delegate.objects.get_or_create(username=request.user) newdel.resume = fm['resume'] changed = True newdel.occupation_id= int(fm['occupation']) changed = True newdel.location = fm['location'] changed = True newdel.save() if request.FILES: # Delete the old photo pat = settings.MEDIA_ROOT+request.POST['flname'] try: os.unlink(pat) except: pass newdel.save_photo_file(request.FILES['photo']['filename'], request.FILES['photo']['content']) return HttpResponseRedirect("/web/" ) else: try: id = request.user.id usr = User.objects.get(pk=id) data = { 'username': usr.username, 'first_name':usr.first_name, 'last_name': usr.last_name } a = Delegate.objects.get(username=request.user) data['resume'] = a.resume data['occupation'] = a.occupation.id data['location'] = a.location pat = settings.MEDIA_ROOT+a.photo filedt = open(pat,'r') filedata = { 'photo': {'filename': a.get_photo_url(),'content': filedt.read()} } try: data['flname'] = a.photo data['flurl'] = a.get_photo_url() except: pass form = Edituserform(data,filedata) except: form = Edituserform(data) return render_to_response("web/edituser.html", {'form':form, 'request':request, }) Template: {% extends "base.html" %} {% load i18n %} {% block centercontent %}

{% trans "Edit profile for user:" %} {{ form.data.username }}

{% trans "Edit your profile here. You can also change your password if you want" %}

{% if form.errors %} Please correct the errors below {% endif %}

{% for field in form %}
{{ field.label_tag }}
{% ifequal field.name 'photo' %} {{ form.data.flname }} {% endifequal %}
{{ field }}
{% if field.help_text %}
{{ field.help_text }}
{% endif %} {% if field.errors %}
{{ field.errors }}
{% endif %} {% endfor %}

{% endblock %}