我正在尝试找到加密我的 URL 的最佳解决方案,我找到了一些旧版本,用于 python 2.2。我需要设置我的网址,而不是像这样显示:
.com/AddPost/1
/ 和.com/PostDetail/40
. 但是这样的事情:.com/AddPost/DMQRzZWMDdGQtbndzBHNsawN0aXRsZQR0ZXN0AzcwMQR3b2UDMjQwMjEwNQ
您无法猜测 PK 是什么并访问该页面:
网址:
urlpatterns = [
path('PostDetail/<int:pk>', PostDetail.as_view(), name ='post_detail'), ]
视图.py:
class PostDetail(DetailView):
model = Post
template_name = 'post_detail.html'
def get_context_data(self, *args,**kwargs):
post = Post.objects.all()
context = super(PostDetail,self).get_context_data(*args,**kwargs)
stuff = get_object_or_404(Post,id=self.kwargs['pk'])
total_likes=stuff.total_likes()
context['total_likes'] = total_likes
return context