Load views into files from URLs in Django

- Andrés Cruz

En español
Load views into files from URLs in Django

There are some ways in which we can establish the routes that we are going to use in a Django project from the applications that make up this project: the first one we saw would be the most complicated and it looked as follows:

from django.urls import path
from . import views
 
urlpatterns = [
    path('', views.index, name='index')
]
 

Simply, we have to define a file in the application that by convention we simply call as urls.py and then from the urls file of our project we link it or include it with the function called include:

from django.contrib import admin from django.urls import path, include from firstApp import urls urlpatterns = [    path('admin/', admin.site.urls),    path('fisrtApp/', include('firstApp.urls')), ]

Incluyendo vistas directamente en el archivo de las rutas del proyecto

The next slightly easier but less recommended variation would be to directly load our application's view into our project's urls file; for that:

from django.contrib import admin from django.urls import path, include from firstApp import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.employeeView), ]

But as you can guess, if we have many routes or applications this will grow enormously as well as it will be more difficult to maintain; this variation could be recommended if we have few views to load or few applications.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.