title: Creating user accounts for pre-registered users in Django
body: <p>I have a website that has a registration system and a blog with some registered users. Yesterday, I added a new app that creates dedicated profile pages for each one of those users. </p>
  
  <p>The issue being, the profile pages aren't getting created for the users that have already registered. This is, I guess because the user profile creation logic allows for profile creations only after the user has registered.</p>
  
  <p>Below is the code in my <strong><em>models.py</em></strong></p>
  
  <pre><code>from django.db.models.signals import post_save
  from django.dispatch import receiver
  
  @receiver(post_save, sender=User)
  def create_account(sender, instance, created, *args, **kwargs):
      if created:
          profile, new = UserAccount.objects.get_or_create(user=instance)
  
  post_save.connect(create_account, sender=settings.AUTH_USER_MODEL)
  </code></pre>
  
  <p>So, what can I do to create those profile pages?</p>
  
  <p>I tried the following:<br>
  1. Manually creating a profile page against each registered users' username. (But this isn't the way I want to lean on. This is just a temporary arrangement)</p>
  
  <p>Issue with this, when the superuser who creates these profile pages calls for his private profile page [at /u/], the code looks towards all the users created by the superuser instead of his own. </p>
  
  <p>This is the error that's shown:</p>
  
  <pre><code>MultipleObjectsReturned at /u/
  
  get() returned more than one UserAccount -- it returned 2!
  
  .
  .
  .
  instance = get_object_or_404(UserAccount)
  </code></pre>
  
  <p>So, what's the issue in code here? [my views.py]</p>
  
  <pre><code># public user profile
  def user_account(request, username):
      instance = get_object_or_404(UserAccount, user__username=username)
      context = {
      'instance'  : instance,
      'title'     : "User Account",       
      }
      template = "user_accounts/public_account.html"
      return render(request, template, context)
  
  # private user profile.
  @login_required
  def self_user_account(request):
      if not request.user.is_authenticated:
          raise Http404
  
      instance = get_object_or_404(UserAccount)
  
      if not request.user == instance.user:
          raise Http404
  
      context = {
          'instance'  : instance,
          'title'     : 'Your Account',
      }   
  
      template = "user_accounts/self_account.html"
      return render(request, template, context)
  
  # ability to update the user profile
  @login_required
  def update_user_account(request):
      if not request.user.is_authenticated:
          raise Http404
  
      instance = get_object_or_404(UserAccount)
  
      if not request.user == instance.user:
          raise Http404
  
      if request.method == 'POST':
          form = UserAccountForm(request.POST or None, request.FILES or None, instance=instance)
          if form.is_valid():
              instance = form.save(commit=False)
              instance.save()
              messages.success(request, "Account Updated.")
              return HttpResponseRedirect("/u/")
          else:
              messages.error(request, "Something went wrong. Profile not created.")
      else:
          form = UserAccountForm(instance=instance)
  
      context = {
          'title': 'Update Your Account',
          'form' : form,
      }
      template = "user_accounts/update.html"
      return render(request, template, context)
  </code></pre>
  
  <p>Below is my <strong><em>urls.py</em></strong> These are populated after /u/ from the main <strong><em>urls.py</em></strong> </p>
  
  <pre><code>urlpatterns = [
      # for updating
      url(r'^update/$', views.update_user_account, name="update_user_account"),
      # for outside world
      url(r'^(?P&lt;username&gt;[\w.@+-]+)/$', views.user_account, name="public_user_account"),
      # for the user himself 
      url(r'^$', views.self_user_account, name="self_user_account"),
  ]
  </code></pre>
  
  <p>Below is my <strong><em>models.py</em></strong> script</p>
  
  <pre><code># uploading profile photos
  def upload_location(instance, filename):
      return "account_photos/%s/%s" %(instance.user, filename)
  
  # Create your models here.
  class UserAccount(models.Model):
      user            = models.OneToOneField(User)
      photo           = models.ImageField(
                          upload_to=upload_location, # there needs to be a upload location tho
                          # most probably a cdn server
                          blank=True,
                          null=True, 
                          width_field="width_field",
                          height_field="height_field")
      width_field     = models.IntegerField(default=0, null=True)
      height_field    = models.IntegerField(default=0, null=True)
      bio             = models.TextField(max_length=60, null=True, blank=True, verbose_name="You in 60 words.")
      phone           = PhoneNumberField(blank=True, verbose_name="Contact Number")
      status          = models.CharField(max_length=128, default="Student")
      totos           = models.IntegerField(default=0, verbose_name="Contribution")
      user_since      = models.DateTimeField(auto_now=True)
  
      # social links
      email           = models.EmailField(verbose_name="email address", 
                                          max_length=255,
                                          unique=True,
                                          null=True, blank=True)
      custom_link     = models.URLField(null=True, blank=True)
      facebook_link   = models.URLField(null=True, blank=True)
      twitter_link    = models.URLField(null=True, blank=True)
      linkedin_link   = models.URLField(null=True, blank=True)
      github_link     = models.URLField(null=True, blank=True)
      reddit_link     = models.URLField(null=True, blank=True)
  
  
  
      def __str__(self):
          return self.user.username
  
      def get_absolute_url(self):
          return self.reverse("accounts:public_user_account", kwargs={"username":self.user__username})
  
  from django.db.models.signals import post_save
  from django.dispatch import receiver
  
  @receiver(post_save, sender=User) # User is coming from the user=models.OneToOneField(&lt;User&gt;)
  def create_account(sender, instance, created, *args, **kwargs):
      if created:
          profile, new = UserAccount.objects.get_or_create(user=instance)
  
  post_save.connect(create_account, sender=settings.AUTH_USER_MODEL)
  </code></pre>