路径转换器中需要什么to_url()方法? Django

时间:2018-10-05 15:57:18

标签: python django url

在Django的路径转换器中,我需要什么to_url(self, value)方法?

我只能在官方文档中找到一些示例,并且不理解此方法的使用。

to_url()到底能做什么?

class FourDigitYearConverter:

    regex = '[0-9]{4}'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return '%04d' % value

3 个答案:

答案 0 :(得分:0)

此方法将值(例如,在这种情况下为数字)转换为可在URL中使用的字符串,例如,如果该值是整数(例如4),则将其格式设置为“ 0004”。

答案 1 :(得分:-1)

实际上,当您此时在{% url 'post' post.id %}中将此类网址放入'post.id' first converted into the url format from python to url.

中时,

答案 2 :(得分:-1)

据我所知,to_url()将URL中字符串之间需要的整数转换为字符串。例如www.fb.com/id=20,name=jondoe进入/ id = 20%name = jon + doe

相关问题