Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View not detected #13

Open
ViggieM opened this issue Jul 28, 2023 · 6 comments · May be fixed by #24
Open

View not detected #13

ViggieM opened this issue Jul 28, 2023 · 6 comments · May be fixed by #24

Comments

@ViggieM
Copy link

ViggieM commented Jul 28, 2023

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
Copy link
Owner

valberg commented Jul 29, 2023

Hi @movileanuv

You are missing django_view_decorator from your INSTALLED_APPS :)

@ViggieM
Copy link
Author

ViggieM commented Jul 30, 2023

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
Copy link
Owner

valberg commented Sep 1, 2023

Hi @movileanuv

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

@Mte90
Copy link

Mte90 commented Jan 24, 2024

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
Copy link

Mte90 commented Jan 24, 2024

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

cls.views.setdefault(namespace, defaultdict(list))[name].append(
of the paths variable I am not getting anything but just the crash before.
Same thing for 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
Copy link

Mte90 commented Jan 24, 2024

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.

@Mte90 Mte90 linked a pull request Jan 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants