Django : how to fix empty slug? - django

I have a urls :
urlpatterns = [
url(r"admin/" , admin.site.urls),
url(r"users/" , include(("users.urls", "users") , namespace='users') ),
path('bristol/' , include(('bristol.urls', "bristol") , namespace='bristol') ),
url(r"" , include('django.contrib.auth.urls')),
]
But I can't connect to my server if I don't put any end slug to my url :
What should I change in my url patterns to redirect localhost:8000 directly to the login view ?
I tried :
urlpatterns += [ url(r"/" , reverse("login"))]
But It didn't help :-/
Edit 1 :
Trying Wilem's solution worked at last, and the issue was partially due to refresh issues on my browser (see comments):

Since django.contrib.auth.urls does not contain any view with path /, it will thus not match with any pattern.
You can define an extra pattern that for example maps on the LoginView:
from django.contrib.auth.views import LoginView
urlpatterns = [
url(r'admin/', admin.site.urls),
url(r'users/', include(('users.urls', 'users'), namespace='users')),
path('bristol/', include(('bristol.urls', 'bristol'), namespace='bristol')),
url(r'', include('django.contrib.auth.urls')),
path('', LoginView.as_view(), name='index')
]
But it is rather odd to map the index path on the LoginView, normally you make a view that acts as some sort of "dashboard". You can make use of the #login_required decorator [Django-doc] to prevent people from looking at the dashboard if they are not logged in.

Related

How to write a urls.py in django so that I can do something like */page

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.

Wrong template rendering

I don't understand why django wont render any other template , but home with this code:
urlpatterns = [
url(r'^login/', login_page),
url(r'^admin/', admin.site.urls),
url(r'^contact/', contact_page),
url(r'^about/', about_page),
url(r'$', home_page),
]
But as soon as I delete the home url , every url works just fine. Yes , I did join the base dir with the templates one. Also , it might be useful to add that Im using django 1.11. Thank you !

Django url/route order not maintained

I have the following in my root URLconf module (there's more, but not important, so left out):
urlpatterns = [
re_path(r'^password-reset-redirect-view/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
password_reset_redirect,
name = 'password_reset_confirm'),
path('', include('search.urls')),
path('', include('customer_portal.urls')),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
Here's the customer_portal.urls:
urlpatterns = [
path('customer/contact/', views.contact),
path('', views.home),
re_path(r"^confirm-email/(?P<key>[-:\w]+)/$", views.email_verification,
name="account_confirm_email"),
]
Here's the rest_auth.registration.urls:
urlpatterns = [
url(r'^$', RegisterView.as_view(), name='rest_register'),
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
name='account_confirm_email'),
]
As you can see both included urls.py urlpatterns have a view named 'account_confirm_email'.
Somewhere in the code this is ran:
url = reverse(
"account_confirm_email",
args=[emailconfirmation.key])
Since customer_portal.urls is included before rest_auth.registration.urls, I expect the route account_confirm_email in customer_portal.urls to be returned by the above reverse method. But instead I get the rest_auth.registration.urls route URL.
Just to be sure I commented out the route in rest_auth.registration.urls, and then I did get the correct URL (customer_portal URL) returned.
It is filled into an email, I check that email and see that I have the wanted url: http://127.0.0.1:8000/confirm-email/......./, instead of: http://127.0.0.1:8000/rest-auth/registration/account-confirm-email/...../
Can anyone tell me why the customer_portal URL isn't the one being reversed in both cases?
Django docs say:
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

Remove user endpoints in Django rest auth

I am using Django rest auth for user account handling. For updating user info i have created custom endpoints, So i don't need the djnago-rest-auth generated endpoints /rest-auth/user/ (GET, PUT, PATCH). How can i remove these endpoints?
urls.py
urlpatterns = [
path('', include("rest_auth.urls"), name="user-auth"),
path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]
Edit
I want to use all other urls of rest-auth like login, register, etc. But i just dont want the /rest-auth/user/ as described here.
#bodoubleu 's answer didn't work, So i added them manually.
from rest_auth.views import (
LoginView, LogoutView, PasswordChangeView,
PasswordResetView, PasswordResetConfirmView
)
urlpatterns = [
path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
path('login', LoginView.as_view(), name="user-login"),
path('logout', LogoutView.as_view(), name='user-logout'),
path('password/change/', PasswordChangeView.as_view(), name='rest_password_change'),
path('password/reset', PasswordResetView.as_view(), name='rest_password_reset'),
path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='rest_password_reset_confirm'),
path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]
Untested but this should work.
urlpatterns = [
path('user/', django.views.defaults.page_not_found),
path('', include("rest_auth.urls"), name="user-auth"),
path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]
If not you can manually define all the rest_auth.urls in your url patterns

Different templates usage caused by changing the order in URLs (auth / registration)

I am using in my project built-in auth tools and django-registration
I have my logout template at:
/accounts/templates/registration/logout.html
If urls.py looks like:
urlpatterns = [
...
url(regex = r'^accounts/', view = include('registration.backends.hmac.urls')),
url(regex = r'^accounts/', view = include('django.contrib.auth.urls')),
...
]
It uses my template. It's OK.
But if I reorganize url like:
urlpatterns = [
...
url(regex = r'^accounts/', view = include('django.contrib.auth.urls')),
url(regex = r'^accounts/', view = include('registration.backends.hmac.urls')),
...
]
It uses built-in admin logout template.
Why does it happen?
Edit
In their tutorial I see that they say about 'registration.backends.hmac.urls':
That URLconf also sets up the views from django.contrib.auth (login,
logout, password reset, etc.), though if you want those views at a
different location, you can include() the URLconf
registration.auth_urls to place only the django.contrib.auth views at
a specific location in your URL hierarchy.
But when I open it, it seems to have no connection with auth urls/views:
EDIT: OK, now I see.
"""
URLconf for registration and activation, using django-registration's
HMAC activation workflow.
"""
from django.conf.urls import include, url
from django.views.generic.base import TemplateView
from .views import ActivationView, RegistrationView
urlpatterns = [
url(r'^activate/complete/$',
TemplateView.as_view(
template_name='registration/activation_complete.html'
),
name='registration_activation_complete'),
# The activation key can make use of any character from the
# URL-safe base64 alphabet, plus the colon as a separator.
url(r'^activate/(?P<activation_key>[-:\w]+)/$',
ActivationView.as_view(),
name='registration_activate'),
url(r'^register/$',
RegistrationView.as_view(),
name='registration_register'),
url(r'^register/complete/$',
TemplateView.as_view(
template_name='registration/registration_complete.html'
),
name='registration_complete'),
url(r'^register/closed/$',
TemplateView.as_view(
template_name='registration/registration_closed.html'
),
name='registration_disallowed'),
url(r'', include('registration.auth_urls')),
]
The last url pattern in registration.backends.hmac.urls includes registration.auth_urls, which provides urls for login, logout and so on.
url(r'', include('registration.auth_urls')),
If you include django.contrib.auth.urls, above the hmac urls, then the logout view from django.contrib.auth will be used. This view uses a different template, registration/logged_out.html. Since you haven't overridden this, the admin template is used.
The django url dipatcher which maps urls to views checks urls in order from first to last:
Django runs through each URL pattern, in order, and stops at the first
one that matches the requested URL.
So the order of the similar urls determinate which url is matched. I.e. the first is matched.