Closed obvionaoe closed 1 year ago
Does this look like a problem with the documentation, or the code?
url
was deprecated in favour of path
, so I believe the documentation is outdated in that regard.
The rest I'm not sure what the problem might be
So, I really want to use this for my project, I created a repo where you can reproduce my problem! obvionaoe/django_org_test If you could take a look, I'd really appreciate it!
I still think it might have to do with authentication, as it looks like it tries to redirect to a login, but I don't know how to debug it
Thank you @obvionaoe I'll take a look!
There are several issues in the sample project you shared regarding project architecture and layout.
Regarding the 404, if I access the given URL, e.g http://localhost:8000/accounts/
that URL maps to the OrganizationList
view in the organization.urls
module, which applies login_required
. So when I see the 404 it's actually on this URL, http://localhost:8000/accounts/login/?next=/accounts/
which doesn't exist in your URL configuration.
But why are you being redirected to /accounts/login/
?
Because that's the default value for settings.LOGIN_URL
which is what Django's authentication system redirects to when a non-authenticated user tries to access a view protected by a login requirement. What you're missing is a user-facing authentication page. You can create this yourself or use something like django-allauth
.
It was a little bit of a struggle to get this up and running and I was confused by why the helper script had commands to delete migrations.
The problem is that you have defined both your system user model and a django-organizations
dependency in the polls
app (via the ForeignKey
in TestModel.owner
), which creates a circular dependency. I got everything working after I cleared out the migrations, commented out TestModel.owner
, and rebuilt the migrations.
In the above image, the organizations
app and the polls
app have mutual dependence. This is a recipe for pain and confusion. You want an architecture more like the following:
Remove your custom User model to a separate app which has zero dependencies on django-organizations
and preferably no other apps in your project, first or third party. All it should "know about" is users as individuals. You can create your own app or find a third party app to suit this purpose. I published an app to satisfy this need for myself called django-email-users
which may be of use as an example; the code itself is quite old however!
I don't think this actually caused any problems, but from my own experience is a best practice: always include "pass through" URLs at the end of your URLs configuration.
In your root URLs config the urlpatterns
looks like so:
urlpatterns = [
path('', include('polls.urls')),
path('', include('djoser.urls')),
path('', include('djoser.urls.jwt')),
path('accounts/', include('organizations.urls')),
path('invitations/', include(invitation_backend().get_urls())),
]
The potential problem here is that your explicitly configured URL paths, which here are accounts/
and invitations/
are potentially subject to being intercepted by URLs in the preceding included paths.
Defining explicit paths first guarantees that those URLs will be checked first, making debugging URL issues much simpler.
In the following example I've added django-allauth
for user account management and modified the URL structure. This guarantees now that user management (registration, authentication) routes are checked first, then organizational account routes, and then everything else without a unique path prefix.
urlpatterns = [
path('accounts/', include('allauth.urls')),
path('accounts/', include('organizations.urls')),
path('invitations/', include(invitation_backend().get_urls())),
path('', include('polls.urls')),
path('', include('djoser.urls')),
path('', include('djoser.urls.jwt')),
]
Is there a way to configure Django Organizations to use a different permission class, meaning instead of the default Django LoginRequired class use DRF's IsAuthenticated class? As that one is the one that works with DRF, Djoser and SimpleJWT.
In regards to the circular dependency error, that's a good tip, thank you! I'll check out your email users app :)
It's weird that login_required doesn't recognize DRF's Authentications classes
I did'nt get much what the problem is but related to permission classes we can define the custom permission in django .
Hi, I tried following the Getting Started guide, but now when I try to access the path
accounts/
orinvitations/
I get a 404.When accessing the
accounts/
path it also auto redirects toaccounts/login
which returns a 404.I also had to change the url snippet in the guide to use the
path
function instead of theurl
one because I was gettingon both urls.
I'm using Django 4.0.1, DRF 3.13.1, DRF-simplejwt 5.0.0, django-extensions 3.1.5 and django-organizations 2.0.1.
I also tried disabling DRF-simplejwt in case authentication was the issue but it led to the same errors.
Thanks