I have a problem with Django urls that I cannot get to the bottom of. I have tried the recommendations given in the answers to this question, but they don't help.
<project>/urls.py
urlpatterns = [
...
path('duo/', include('duo.urls')),
path('users/', include('users.urls')),
]
duo/urls.py
urlpatterns = [
...
path('', include('users.urls')),
...
]
users/urls.py
urlpatterns = [
path('', views.SelectPartner.as_view(), name='select-partner'),
...
]
when I use the url http://192.168.1.138:8000/duo/ I get taken to the page http://192.168.1.138:8000/accounts/login/?next=/duo/ which does not exist.
I cannot think what is going on here because the word accounts does not exist anywhere in the project
Is there some tool that I can use to find out what is happening?
You have login required somewhere which sends you to default login page location
If you are going to use default authentication you can add these views up
I think you forget to use the app in URL and then further parameters. Then it will work by default authentication is available in URL if you are going to use authentication
Related
Here is the problem:
I have an app with the following models: project, position, outreach
A position is connected to a project and project only with a Foreign key
An outreach is connected to a position and a position only with a Foreign key
I can create a new project from almost anywhere in my app (same for the other objects). Currently I wrote that a new project is created from the url dashboard/newjobproject but I would to make it so that depending on the page that I am, the url simply becomes something like www.myapp.com/..../newproject
What's a way to write the urls.py to achieve that?
from django.urls import path
from action import views
app_name = 'action'
urlpatterns = [
# ex: /action/
path('', views.login, name='login'),
path('dashboard/', views.dashboard, name='dashboard'),
path('contacts/', views.contacts, name='contacts'),
path('projects/', views.project, name='project'),
path('contacts/newcontact', views.new_contact, name='new_contact'),
path('projects/newjobproject', views.new_outreach, name='new_outreach'),
path('dashboard/newjobproject', views.new_jobproject, name='new_jobproject'),
path('projects/<uuid>/newjobposition', views.new_jobposition, name='new_jobposition'),
]
However,
Try adding this to the bottom of urlpatterns:
path('<path:p>/newjobproject', views.new_jobproject, name='whatever-name-you-want'),
and in your views.py:
def new_jobproject(request, p):
Tbh though, this is sort of a hacky way to do it. It'll break in a few locations. If you have a main urlpatterns array in which you're including the urls for this 'action' app as APIs, this solution won't work outside the API urls.
For eg. if you have:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('action.urls')),
]
And you access your url like this -> www.myapp.com/api/v1/....../newjobproject, then the code will work. If you try to access www.myapp.com/..../newjobproject or www.myapp.com/admin/..../newjobproject then the code will break and it will not match any of the paths. I'm not sure how you'd get it to work in that case.
If the above scenario is not an issue, if you're not going to be using these views as APIs, and your urlpatterns looks something like this:
urlpatterns = [
path('admin', admin.site.urls),
path('/', include('action.urls')),
]
then the code should work for all urls except for the admin/.../newjobproject case.
This is how I defined urls.py file of my app
router = DefaultRouter()
router.register('hello-viewset', views.HelloViewSet, base_name='hello-viewset')
router.register('profiles', views.UserProfileViewSet)
router.register('schema', views.SchemaViewSet)
router.register('creddefination', views.CredDefViewSet)
router.register('overalltable', views.OverallViewSet)
urlpatterns = [
path('', include(router.urls)),
]
urls.py of Project:
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('DIAPI.urls')),
]
I am not getting correct address for creddefination. But when i manually go to http://127.0.0.1:7000/api/creddefination/ it is working. It is just not displaying correctly. What might be reason for this
I guess views.CredDefViewSet and views.OverallViewSet are using the same model.
If that's true, then the default register's basename will be named after that model and used as name in a call to Django's reverse url construction. Since the API Root view will be trying to resolve both views with the same name, it'll lead to the same url.
Workaround is to explicitly add a basename to one of the view:
router.register('creddefination', views.CredDefViewSet, basename='creddeef')
I have been using allauth with my Django project since the start and have had no issues. Recently while trying to clean up some code and improve a user search feature it seems my changes break the accounts/login page.
I have been unable to find any fixes online after 2 days of googling and searching through documentation and I am hoping someone with a little more experience might be able to see where my error is.
I have narrowed the cause of the error to a change in a single URL in my users app, which is completely unrelated to allauth.
When I change
urlpatterns = [
....
path('<int:pk>/<str:slug>/', ProfilePageView.as_view(), name='profile'),
path('search/', SearchResultsView.as_view(), name='search'),
]
to
urlpatterns = [
...
path('<str:slug>/', ProfilePageView.as_view(), name='profile'),
path('search/', SearchResultsView.as_view(), name='search'),
]
is when I receive the error. Inside my terminal I see Not Found: /accounts/login/ however in the browser I see No user found matching the query which confuses me because I am not sure where this query is coming from.
The main change to my code that I am implementing is in my users.models
def get_absolute_url(self):
kwargs = {
'pk' : self.id,
'slug' : self.slug,
}
return reverse('users:profile', kwargs=kwargs['slug'])
which is also giving me a TypeError: _reverse_with_prefix() argument after ** must be a mapping, not str but that's another issue (feel free to provide insight on that too if you know).
I thought maybe it was because my search.html was inside the accounts templates directory, so I tried moving it out of there but that did not yield any different results.
I am confused about what this issue is and why changing an unrelated template/url is breaking my allauth pages? it seems to be all of my accounts/ pages, but also the search page that are no longer working, however my home and about pages are functioning.
Thank you for any and all insight into this issue and please let me know if you need more information.
You are inadvertently hiding one URL behind another. Your defined urlpatterns:
urlpatterns = [
...
path('<str:slug>/', ProfilePageView.as_view(), name='profile'),
path('search/', SearchResultsView.as_view(), name='search'),
]
... are tested in order to find a match. The URL /search/ matches the first pattern and is routed to the ProfilePageView as directed.
The fix is to reverse the order of those definitions, so that /search/ matches the specific case and /<slug>/ matches the "fall-through" case:
urlpatterns = [
...
path('search/', SearchResultsView.as_view(), name='search'),
path('<str:slug>/', ProfilePageView.as_view(), name='profile'),
]
I'm working on a Django web-app which has user accounts, and so has login and logout functions.
What I would like to have is mywebsite.com/accounts/login/ be the login page. This is working as expected right now.
This issue is with logout- what I would like to have is mywebsite.com/accounts/logout/ logout the user and redirect them to the login page. This, however, doesn't appear to work.
Here is my url configuration in accounts/urls.py:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', auth_views.logout, name='logout'),
]
With this configuration, login works fine. But when I go to mywebsite.com/accounts/logout/, I am just immediately sent back to the page I'm currently on.
However, if I change the logout url:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logMeOutPlease/$', auth_views.logout, name='logout'),
]
Then mywebsite.com/accounts/login works as intended and mywebsite.com/accounts/logMeOutPlease works as intended. Is there a reason the first configuration won't work?
If I however move the logout functionality to the top level (i.e just mywebsite.com/logout/), then it again works fine.
For reference, this is what is in the "top level" urls file:
urlpatterns = [
# other urls that I can't show here
url(r'^redirect/', include('mywebsite.apps.redirect.urls')),
url(r'^accounts/', include('mywebsite.apps.accounts.urls')),
url(r'^$', RedirectView.as_view(url='redirect/')),
url(r'^admin/', admin.site.urls),
]
I am using Django 2.0.6 and Python 3.5.2, and my laptop is on Ubuntu 16.04.
Ok, so from what I understand so far, that behavior is actually correct. Since, views logic was not provided in your question, I will assume that the views are currently not using any #login_required decorators and the logout view does what it says i.e. logout. So without seeing the views logic we assume there were no instructions about where to redirect to on successful logout and no decorator so opens the same page again.
So here's the advice.
In your settings.py file:-
Add:
LOGOUT_REDIRECT_URL = 'url_you_want_to_redirect_to_on_logout'
This will enable redirecting to the specified url whenever a user logs out.
And if it helps, similarly we also do have a
LOGIN_REDIRECT_URL = 'url_you_want_to_redirect_to_on_login'
some question about django 1.10:
I am going to make a website and the home page should be a login page created by django,when i tried to make the admin login page as my site's home page,it failed,can anybody help me ?
my urls.py:
urlpatterns = [ url(r'^$', include(admin.site.urls)), ]
or
urlpatterns = [ url(r'^$', admin.site.urls), ]
both works wrong.
thx
For djanog version 2 & later, the syntax url() has been replaced & simplified with path()
So, if you have django version >=2 , use this:
urlpatterns = [
path('', admin.site.urls),
]
Read the docs for details.
Generally speaking, setting admin page as the main page is not a good idea but still, whatever the use case is, try this:
url(r'^', admin.site.urls),
Tested it on Django 1.9 but I think it'll work on Django 1.10 as well.
Also, don't forget to restart the server after making these changes.