2016-10-11 62 views
1

我正在尋找從django模型的默認ID鍵把它變成十六進制,並顯示它在一個頁面上,當用戶sumbits帖子時,我已經嘗試了幾個方法沒有成功可以任何人都指向正確的方向?python django id到十六進制

views.py

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}) 

如果需要,可以提供更多的信息。

回答

0

Python的十六進制函數是你在這裏需要的全部,但問題是你不能直接從你的模板調用它。所以解決方案是在你的模型中添加一個方法。

class MyModel(models.Model): 

    def to_hex(self): 
     return hex(self.pk) 

然後在你的模板

{{ my_object.to_hex }} 
+0

{{my_object.to_hex()}}刪除()從my_object.to_hex 這工作就像一個魅力!謝謝!! –

+0

很高興有幫助。 – e4c5