如何将变量从url传递到django中的视图? [英] How to pass a variable from the url to a view in django?

查看:131
本文介绍了如何将变量从url传递到django中的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题。



如何将URL中的变量传递给视图?我遵循日期示例



我的观点需要参数:

  def hours_ahead(request,offset):

我的url.py有这个

 (r'^ plus / \d {1,2} / $',hours_ahead),


$ b $我知道我需要传递另一个参数,但是我不知道如何从URL字符串中获取数字(即'time / plus / 4 /')。

 (r'^ plus / \d {1,2} / $',hours_ahead,offset = ??) 


解决方案

您可以使用正则表达式中的命名组

 (r'^ plus /(?P< offset> \d {1,2})/ $',hours_ahead),


Simple question.

How can I pass a variable from the URL to the view? I'm following the Date Example.

My view needs to arguments:

def hours_ahead(request, offset):

My url.py has this"

(r'^plus/\d{1,2}/$', hours_ahead),

I know I need to pass another argument through but I don't know how to get the number from the URL string (i.e. 'time/plus/4/'. Something like this?

(r'^plus/\d{1,2}/$', hours_ahead, offset=??),

解决方案

You could use Named groups in the regex

(r'^plus/(?P<offset>\d{1,2})/$', hours_ahead),

这篇关于如何将变量从url传递到django中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆