So I understand, after your comment, you just want to make the code look better in the urls.py file.
Instead of:
...
from contas.views import index, cadastro, contato, sobre, novasenha, base, entenda
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('cadastro/', cadastro, name='cadastrar'),
path('contato/', contato, name='contato'),
path('sobre/', sobre, name='sobre'),
...
]
You can only import the module file views
and use it with namespace in the declaration:
...
from django.urls import path
from contas import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('cadastro/', views.cadastro, name='cadastrar'),
path('contato/', views.contato, name='contato'),
path('sobre/', views.sobre, name='sobre'),
...
]
And that would be the one recommended.
If you prefer, there is no magic, url_patterns are a normalized Python object - a list of objects of type "path", where the first parameter is the desired url pattern, the second object itself is the view which can be a function, class, or other callable object), and the third, a name for the view. That is, you can generate this list programmatically from introspection of the views module - instead of stating them all in a static way. This is a case where "explicit" is usually better than implied - but can save you having to edit one more file each time you create a new view - in which case the code can be something like:
from contas import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
]
for name, obj in views.__dict__.items():
if callable(obj):
urlpatterns.append(path(f"{name}/", obj, name)
With this code, all functions in your views file will have an automatically generated URL.
Note that while this is a somewhat dangerous practice, since it does not distinguish functions that you may not want to use as views, and so on, the technique lets you realize how simple it is to do something in Django to be used as a "decorator for views", as is done in other frameworks, such as Flask.
In this case, it can be a very simple decorator, it does not "envelop" the decorated function, it only adds to it the desired data:
def autoview(path=None, name=None):
def decorate(func):
# acrescenta o atributo "autoview" na função,
# que é um objeto Python normal:
func.autoview = {
"path": path or f"{func.__name__}/",
"name": name or func.__name__
}
return func
return decorate
From here you can write your views file in this style:
from utils import autoview
...
@autoview(name="cadastrar")
def cadastro(request):
return render(request, 'contas/cadastro-clientes.html')
@autoview()
def contato(request):
return render(request, 'contas/contato.html')
And in urls.py, a variant of the above suggestion, to use ".autoview":
for name, obj in views.__dict__.items():
if hasattr(object, "autoview"):
urlpatterns.append(path(obj.autoview["path"], obj, obj.autoview["name"])