django获取参数并传递int类型

django获取参数并传递int类型,django,Django,我在url.py中有以下条目 url(r'^profile/(?p[0-9]+)$”,views.profile\u view,name='profile'), 其中price_id参数由以下模板片段生成: “继续” 这由以下视图呈现: class QuotesResultsView(ListView): template_name = 'pages/quotes_results.html' paginate_by = 20 context_object_name = '

我在url.py中有以下条目

url(r'^profile/(?p[0-9]+)$”,views.profile\u view,name='profile'),

其中price_id参数由以下模板片段生成: “继续”

这由以下视图呈现:

class QuotesResultsView(ListView):
    template_name = 'pages/quotes_results.html'
    paginate_by = 20
    context_object_name = 'quotes_results'

    def get_queryset(self):
        session = self.request.session
        quotes = get_quotes(session)  # return iterable from external api
        quotes_results = quotes['searchResults']
        return quotes_results
def profile_view(request, price_id):
    request.session['price_id'] = int(price_id)
    ....
在呈现的模板中单击按钮时,它会传递(通过url传递到下面的视图):


但是,price_id参数以字符串形式出现在视图中,例如“8234234”,而不是我必须转换的int。在前面的QuotesResultsView中,price_id是quotes结果中的一个int,但从那时起到它出现在下一个视图中是一个字符串?GET参数总是作为字符串传递吗?

是的,来自URL的参数总是字符串,您有责任适当地转换它们。
get_quotes
可能返回一个包含整数
price_id
的结构,但它是通过与URL调用不同的机制获取该值的

当URL配置调用时,
profile\u视图
中的
price\u id
将始终作为字符串输入