开发者

Alternative to hardcode urls in views

开发者 https://www.devze.com 2022-12-13 17:46 出处:网络
In my views I sometimes use this: return HttpResponseRedirect(\"/account/\") But that is hardcoding a url in my view, which I think is not very nice.

In my views I sometimes use this:

return HttpResponseRedirect("/account/")

But that is hardcoding a url in my view, which I think is not very nice.

So what is the alternative? I know I can generate urls directly from the urls.py file, in my templates, via the {% url %} tag, so th开发者_如何转开发ere has to be something I can do in my views, right?


You use the reverse function. Documentation here: reverse


Yes, use the reverse function, but it is even better if used together with a URL pattern name

See http://docs.djangoproject.com/en/dev/topics/http/urls/#id2

0

精彩评论

暂无评论...
验证码 换一张
取 消