title: How can I create a URL-Shortener using Django based on the URL id?
body: <p>I am working with Django and I am just a beginner. Now I am trying to make a URL shortener with Base 62 so I have created one class in <code>models.py</code> and its name is <code>URLGenerator</code>. Here is its code:</p>
  <pre><code>class URLGenerator:
     BASE = 62
     UPPERCASE_OFFSET = 55
     LOWERCASE_OFFSET = 61
     DIGIT_OFFSET = 48
  
     def generate_unique_key(self, integer):
        &quot;&quot;&quot;
        Turn an integer [integer] into a base [BASE] number
         in string representation
        &quot;&quot;&quot;
  
        # we won't step into the while if integer is 0
        # so we just solve for that case here
        if integer == 0:
           return '0'
  
       string = &quot;&quot;
       remainder: int = 0
       while integer &gt; 0:
          remainder = integer % self.BASE
          string = self._true_chr(remainder) + string
          integer = int(integer / self.BASE)
      return string
  
     def get_id(self, key):
        &quot;&quot;&quot;
        Turn the base [BASE] number [key] into an integer
        &quot;&quot;&quot;
        int_sum = 0
        reversed_key = key[::-1]
        for idx, char in enumerate(reversed_key):
          int_sum += self._true_ord(char) * int(math.pow(self.BASE, idx))
      return int_sum
  
     def _true_ord(self, char):
        &quot;&quot;&quot;
        Turns a digit [char] in character representation
        from the number system with base [BASE] into an integer.
        &quot;&quot;&quot;
  
       if char.isdigit():
            return ord(char) - self.DIGIT_OFFSET
       elif 'A' &lt;= char &lt;= 'Z':
           return ord(char) - self.UPPERCASE_OFFSET
       elif 'a' &lt;= char &lt;= 'z':
          return ord(char) - self.LOWERCASE_OFFSET
       else:
           raise ValueError(&quot;%s is not a valid character&quot; % char)
  
     def _true_chr(self, integer):
        &quot;&quot;&quot;
        Turns an integer [integer] into digit in base [BASE]
        as a character representation.
        &quot;&quot;&quot;
       if integer &lt; 10:
           return chr(integer + self.DIGIT_OFFSET)
       elif 10 &lt;= integer &lt;= 35:
           return chr(integer + self.UPPERCASE_OFFSET)
       elif 36 &lt;= integer &lt; 62:
           return chr(integer + self.LOWERCASE_OFFSET)
       else:
           raise ValueError(
              &quot;%d is not a valid integer in the range of base %d&quot; % (integer, BASE))
  </code></pre>
  <p>The above class has two methods that are important. The first one is <code>generate_unique_key</code> that can convert an integer to a unique string and the second one is <code>get_id</code> which can convert a string to an int. For example something like that:</p>
  <pre><code>id = 1024
  generator = URLGenerator()
  key = generator.generate_unique_key(id) # key = GW
  idx = generator.get_id(key)             # idx = 1024
  
  # so the final url should look like this:
  # http://localhost:8000/GW
  </code></pre>
  <p>In the above example in fact the <code>id</code> is the <code>id</code> of a record in the database which I want to use during the saving URL in the database. Then if the user writes <a href="http://127.0.0.1:8000/GW" rel="nofollow noreferrer">http://127.0.0.1:8000/GW</a> he must be redirected to the main URL which is <a href="http://127.0.0.1:8000/1024" rel="nofollow noreferrer">http://127.0.0.1:8000/1024</a>.</p>
  <p>And I have another class which has just one field :</p>
  <pre><code>class Url(models.Model):
     url_id = models.AutoField(primary_key=True)
  
     def save(self , *args , **kwargs):
        pass
  </code></pre>
  <p>But now I do not know how to call the methods of <code>URLGenerator</code> in my <code>URL</code> class and then write a simple function using <code>POST</code> in <code>views.py</code> to see the result.</p>
  <p>I have read some of the posts in StackOverflow and also I have read some tutorials, but unfortunately, I could not solve this problem. I will be grateful for your help and advice.</p>