Home > OS >  Where are variables passed into the view in Django?
Where are variables passed into the view in Django?

Time:11-17

I'm refactoring code in a Django view. I'm working with a view with the below arguments:

def title_views(request, key, group, sort):
    group = get_object_or_404(Group, group=group, main_id = key)
    default_sort = sort

I know by default each view should have the request argument. But in terms of key, group, sort, where can I expect these items to be passed? Is it through the template that the view is called in? I come here for help because the documentation isn't that clear on this, at least in my experience.

Thanks!

CodePudding user response:

These are URL parameters. Likely the URL that links to this view looks like:

    path('<str:key>/<str:group>/<str:sort>/', title_views)

or with a regex, something like:

    re_path(r'(?P<key>\w )/(?P<title>\w )/(?P<sort>\w )/')

If you thus visit a page like foo/bar/qux/ it will pattern match on the path, and thus call the function with 'foo' for key, 'bar' for title, and 'qux' for sort.

Usually the URL parameters thus contain data that determine how to filter, order and render the content.

  • Related