{"id":755,"date":"2013-04-01T00:00:00","date_gmt":"2013-04-01T00:00:00","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2013\/04\/01\/django-how-can-i-pass-a-string-from-a-url-to-a-view\/"},"modified":"2021-07-24T18:05:25","modified_gmt":"2021-07-24T18:05:25","slug":"django-how-can-i-pass-a-string-from-a-url-to-a-view","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django-how-can-i-pass-a-string-from-a-url-to-a-view.html","title":{"rendered":"Django – How can I pass a string from a URL to a view ?"},"content":{"rendered":"
Within Django the are 2 ways of using a URL parameter within a view. They are via URLConf or by using request.GET.<\/p>\n
With this method the URLConf file is configured to define your URL parameter via the use of regex.<\/p>\n
Say that we have a URL that is ‘www.domain.co.uk\/user=value’ and we want to grab ‘value’.
First we would configure our urls.py with,<\/p>\n
url(r'^user=(?P<user>\\d{5})$', 'domain.views.output'),<\/pre>\nThis “value” can then be passed to a function within your view. Such as,<\/p>\n
def output(request, user):<\/pre>\nrequest.GET<\/strong><\/h3>\n
The other way of achieving this is via the use of the request.GET method which is typically used when your url contains a question mark, such as ‘www.domain.co.uk\/?user=value’.<\/p>\n
First of all your urls.py is configured with a standard entry.<\/p>\n
url(r'^$', 'domain.views.output'),<\/pre>\nWe then use the request.GET to assign the “value” to the variable x.<\/p>\n
def output(request):
x = request.GET.get('user', '')<\/pre>\n","protected":false},"excerpt":{"rendered":"Within Django the are 2 ways of using a URL parameter within a view. They are via URLConf or by using request.GET. URLConf With this method the URLConf file is configured to define your URL parameter via the use of regex. Say that we have a URL that is ‘www.domain.co.uk\/user=value’ and we want to grab … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"yoast_head":"\n
Django - How can I pass a string from a URL to a view ? - Fir3net<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n