Skip to content

Instantly share code, notes, and snippets.

Last active April 6, 2021 18:44
  • Star 19 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save seanmavley/6048b896dd7660694b18 to your computer and use it in GitHub Desktop.
from django import forms
from django.contrib.auth.models import User
class UserProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from mezzanine.core.fields import FileField
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='user')
photo = FileField(verbose_name=_("Profile Picture"),
upload_to=upload_to("", "profiles"),
format="Image", max_length=255, null=True, blank=True)
website = models.URLField(default='', blank=True)
bio = models.TextField(default='', blank=True)
phone = models.CharField(max_length=20, blank=True, default='')
city = models.CharField(max_length=100, default='', blank=True)
country = models.CharField(max_length=100, default='', blank=True)
organization = models.CharField(max_length=100, default='', blank=True)
def create_profile(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
user_profile = UserProfile(user=user)
post_save.connect(create_profile, sender=User)
from django.shortcuts import render, HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import UserProfile
from .forms import UserProfileForm
from django.forms.models import inlineformset_factory
from django.core.exceptions import PermissionDenied
def edit_user(request, pk):
user = User.objects.get(pk=pk)
user_form = UserProfileForm(instance=user)
ProfileInlineFormset = inlineformset_factory(User, UserProfile, fields=('website', 'bio', 'phone', 'city', 'country', 'organization'))
formset = ProfileInlineFormset(instance=user)
if request.user.is_authenticated() and ==
if request.method == "POST":
user_form = UserProfileForm(request.POST, request.FILES, instance=user)
formset = ProfileInlineFormset(request.POST, request.FILES, instance=user)
if user_form.is_valid():
created_user =
formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user)
if formset.is_valid():
return HttpResponseRedirect('/accounts/profile/')
return render(request, "account/account_update.html", {
"noodle": pk,
"noodle_form": user_form,
"formset": formset,
raise PermissionDenied
# template.html
<div class="col s12 m8 offset-m2">
<div class="card">
<div class="card-content">
<h2 class="flow-text">Update your information</h2>
<form action="." method="POST" class="padding">
{% csrf_token %} {{ noodle_form.as_p }}
<div class="divider"></div>
{{ formset.management_form }}
{{ formset.as_p }}
<button type="submit" class="btn-floating btn-large waves-light waves-effect"><i class="large material-icons">done</i></button>
<a href="#" onclick="window.history.back(); return false;" title="Cancel" class="btn-floating waves-effect waves-light red"><i class="material-icons">history</i></a>
Copy link

sk-space commented May 19, 2017

Best Solution i have found for the Extending User model and Update UserProfile.....But can you provide support to change the admin panel "UserProfile object" into some other attribute like "username"

Copy link

gdegirmenci commented Sep 20, 2017

skpace to do that, in the UserProfile class you need to add following codes

def __str__(self):
return self.user.username

By the way, thanks for solution.

Copy link

Would I access the edit page by entering the user id in the url? If so I get this error:

File "....\", line 32, in edit_user
if request.user.is_authenticated() and ==
TypeError: 'bool' object is not callable

Copy link

bcyoungV commented Jun 9, 2020

thank you

Copy link

Also another error is
if request.user.is_authenticated() and ==
TypeError: 'bool' object is not callable

Copy link

Also another error is
if request.user.is_authenticated() and ==
TypeError: 'bool' object is not callable

@trnguyen1510 I believe you don't need to use "is_authenticated" as a method call but rather a property, i.e. "request.user.is_authenticated"(NOT "request.user.is_authenticated()")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment