简体   繁体   中英

Only list items as url parameters in django

Id like to have url variable that would only accept items from list, but i dont know how to implement it in urls.

list = ['foo', 'bar', 'test', 'test2', 'random']

and urls.py, that i dont know how to implement list items:

url(r'^(?P<list_item>)/$', views.letnik, name="list_item"),

I would really appreciate it if you could help me out!

You can restrict this in views easily. In your view "letnik" can handle this by checking kwargs.
kwargs.get('list_item') will have the pattern matched.
You can check whether the parameter in url matches one in your list and if it does not you can either redirect to another url or throw 404 status.

One way to do this is by restricting in urls.py is as follows:

url(r'^(?P<list_item>foo|bar|test|test2|random)/$', views.letnik, name="list_item"),

But as the list expands it is better to handle it in views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM