python django id to hex
<p>I'm looking to take the default ID key from the django model turn it into hexadecimal and display it on a page when the user sumbits the post, I've tried several of methods with no success can anyone point me in the right direction?</p>

<p>views.py</p>

<pre><code>def post_new(request):
if request.method == "POST":
    form = PostForm(request.POST)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.published_date = timezone.now()
        post.save()
        return redirect('post_detail', pk=post.pk)
else:
    form = PostForm()
return render(request, 'books_log/post_edit.html', {'form': form})
</code></pre>

<p>Can supply more information if needed.</p>

<p>Python's hex function is all you need here, but the problem is you can't call it directly from your template. So the solution is to add a method to your model.</p>

<pre><code>class MyModel(models.Model):

   def to_hex(self):
       return hex(self.pk)
</code></pre>

<p>Then in your template</p>

<pre><code> {{ my_object.to_hex }}
</code></pre>

python django