我想获取一个特定的页面 url 并自动缩短 url,并将其显示给用户。编写以下代码后,它没有缩短,也没有显示错误。
模型.py
_char_map = string.ascii_letters+string.digits
def index_to_char(sequence):
return "".join([_char_map[x] for x in sequence])
class Arts(models.Model):
user=models.ForeignKey(User)
title=models.CharField(max_length=100)
picture_1=models.ImageField(upload_to="photos")
picture_2=models.ImageField(upload_to="photos", null=True,blank=True)
picture_3=models.ImageField(upload_to="photos", null=True,blank=True)
description=HTMLField(null=True,blank=True)
slug=models.SlugField(max_length=100, unique=True)
pub_date=models.DateTimeField()
def __unicode__(self):
return u'%s' %(self.title)
def get_short_id(self):
_id=self.id
digits=[]
while _id > 0:
rem= _id % 62
digits.append(rem)
_id /=62
digits.reverse()
return index_to_char(digits)
@staticmethod
def decode_id(string):
i=0
for c in string:
i=i*64+_char_map.index(c)
return i
def save(self, *args, **kwargs):
if not self.id:
self.slug=slugify(self.title)
super(Arts,self).save(*args,**kwargs)
视图.py
我要缩短链接的页面
def artdetail(request,arts_id,slug):
post=Arts.objects.get(id=arts_id,slug=slug)
link_db=models.Arts()
currentUrl = request.build_absolute_uri(link_db.get_short_id())
return render_to_response('postdetail.html',{'post':post, ' Arts':Arts,'currentUrl':currentUrl},context_instance=RequestContext(request))
模板
{{currentUrl}}
这是最好的方法吗?我错过了什么?