Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How do I create a generic url routing to views?

Tags:

People also ask

How do I create a route in Django?

To create a route in Django, we will use the path() function that accepts two parameters: a URL and a View function. All the routes are created inside the urlpatterns list. Simply add a new path as below and a new route will be created.

How does URL routing work in Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).

What is generic views in Django?

Unlike classic views, generic views are classes not functions. Django offers a set of classes for generic views in django. views. generic, and every generic view is one of those classes or a class that inherits from one of them.

What is URL mapping in Django?

It's where you define the mapping between URLs and views. A mapping is a tuple in URL patterns like − from django. conf. urls import patterns, include, url from django.


I have a pretty standard django app, and am wondering how to set the url routing so that I don't have to explicitly map each url to a view.

For example, let's say that I have the following views: Project, Links, Profile, Contact. I'd rather not have my urlpatterns look like this:

(r'^Project/$', 'mysite.app.views.project'),
(r'^Links/$', 'mysite.app.views.links'),
(r'^Profile/$', 'mysite.app.views.profile'),
(r'^Contact/$', 'mysite.app.views.contact'),

And so on. In Pylons, it would be as simple as:

map.connect(':controller/:action/:id')

And it would automatically grab the right controller and function. Is there something similar in Django?