valberg / django-view-decorator

MIT License
45 stars 3 forks source link

View not detected #13

Open ViggieM opened 1 year ago

ViggieM commented 1 year ago

Hi, I am trying out your package, and I think it's an awesome idea. I was playing around with this idea of putting an entire django app into one file, like this:

import sys

from django.conf import settings
from django.core.wsgi import get_wsgi_application
from django.urls import path
from django_view_decorator import include_view_urls

settings.configure(
    DEBUG=True,
    ROOT_URLCONF=__name__,
    SECRET_KEY="don't look me",
    INSTALLED_APPS=["app"],
)

urlpatterns = [
    path("", include_view_urls()),
]

application = get_wsgi_application()

if __name__ == "__main__":
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

And in the folder app, I have a views.py file with following view:

@view(paths="", name="index")
def index(request):
    return HttpResponse("hello")

But when I start the app with python main.py runserver, the view is not detected and I only see the django startproject view with the rocket. Why is that? Is there a setting I am missing?

my directory structure looks like this:

.
├── app
│   ├── __init__.py
│   └── views.py
└── main.py

This are my package versions:

Package               Version
--------------------- -------
asgiref               3.7.2
Django                4.2.3
django-view-decorator 0.0.4
gunicorn              21.2.0
packaging             23.1
pip                   22.2.2
setuptools            63.2.0
sqlparse              0.4.4
typing_extensions     4.7.1
valberg commented 1 year ago

Hi @movileanuv

You are missing django_view_decorator from your INSTALLED_APPS :)

ViggieM commented 1 year ago

Oh ok, my bad. Thank you! It doesn't say that in the README though :)

I tried now to adapt the settings to

    INSTALLED_APPS=[
        "django.contrib.auth",
        "django.contrib.contenttypes",
        "django_view_decorator", 
        "app"
    ],

I had to add the additional two apps "django.contrib.auth" and "django.contrib.contenttypes" because redirect_to_login is imported in decorators.view.

The view is still not found though :/

valberg commented 1 year ago

Hi @movileanuv

Sorry for not getting back to you! Did you figure it out?

Mte90 commented 7 months ago

I have the same issue but only with index with others it is not happening, they are added with no issues.

I was able to get the same error with other views. Basically if I have multiple views doesn't find the others.

urlpatterns = [
    path("", home.index, name='index'),
    path("", include_view_urls()),  # view-decorator

In this way I have the index but:

Reverse for 'comment_show' not found. 'comment_show' is not a valid view function or pattern name.

@require_GET
@view(
    paths="comments/<int:lead_id>/",
    name="comments_show",
)
def show(request: HtmxHttpRequest, comments_id) -> HttpResponse:

Removing the decorator require_GET doesn't change the issue

Mte90 commented 7 months ago

A simple print in urls.py of include_view_urls shows that:

(<module 'django_view_decorator.urls' from '/home/www/project/.venv/lib/python3.11/site-packages/django_view_decorator/urls.py'>, None, None)

So my guess is that we are using the decorator but that data is not available somewhere when is needed. If I add a print on https://github.com/valberg/django-view-decorator/blob/4934a67eceed6414cc2e518438d0312240e6096c/django_view_decorator/apps.py#L42 of the paths variable I am not getting anything but just the crash before. Same thing for https://github.com/valberg/django-view-decorator/blob/4934a67eceed6414cc2e518438d0312240e6096c/django_view_decorator/decorators.py#L36 with the same variable and print, I am not getting anything.

Maybe the decorator are executed after that part so those functions are not executed with runserver?

I have the app enabled in django:

INSTALLED_APPS = [
    "unfold", 
    "unfold.contrib.filters",  # optional, if special filters are needed
    "unfold.contrib.forms",  # optional, if special form elements are needed
    "django.contrib.admin",  # required
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_htmx",
    "tailwind",
    "django_select2",
    "django_view_decorator",
    "portal",
]

Running the CLI command just find only one view:

 (name: index, view: portal.views.home.index)

Maybe django requires to load all the views for any request to get all the info? Because adding some print in the app code I see that the decorator is just executed for index but not also for the other views.

Mte90 commented 7 months ago

So I was able to find the issue. As I have views in different files inside the views.py folder the library doesn't load them.

In urls.py I did:

path("", include_view_urls(extra_modules=["portal.views.comments"])),

So probably need to support a folder and loop the files inside.