"no user found" 404 on accounts/login using Django allauth - django

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'),
]

Related

Is there a way to debug Django urls?

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

Django rest-auth registration error after creating new user. account_confirm_email not found

I am creating a registration system with Django rest-auth and allauth. Looking at the documentation endpoints, I just used:
urls.py
app_name = "apis"
urlpatterns = [
path('users/', include('users.urls')),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/customlogin', CustomLoginView.as_view(), name='rest_login'),
path('rest-auth/customlogout', CustomLogoutView.as_view(), name='rest_logout'),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
]
when I run, a browseable API appears like below:
After entering the details and post it, the below error occurs:
django.urls.exceptions.NoReverseMatch: Reverse for 'account_confirm_email' not found. 'account_confirm_email' is not a valid view function or pattern name.
is there another URL i need to implement (with the name of account_confirm_email) ?
I have read the documentations and rest-auth demos on this and it seems I need to include the following urls:
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'),
but this still did not fix the error why is that?
The problem was that I was adding this url:
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(), name='account_confirm_email'),
in the user app's urls.py rather than the project's urls.py. why is it that it cases the issue?

django email confirmation error: TemplateResponseMixin requires either 'template_name' or 'get_template_names()'

I have a django project with a customUser model (email / password), and I'm trying to get email verification links working. They were working before, but now for some reason the path is failing. When you sign up for an account on my site you get an email with a registration-confirmation URL like so:
http://127.0.0.1:8000/rest-auth/registration/account-confirm-email/OA:1hxEiC:mbXKk8-is0YJz2FKHd_1d62KNv8/
But when you click this url, it leads to an error page with the message:
ImproperlyConfigured at /rest-auth/registration/account-confirm-email/OA:1hxEiC:mbXKk8-is0YJz2FKHd_1d62KNv8/
TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'
my main urls.py file has a urlpatterns list which looks like this:
urlpatterns = [
#admin page
path('admin/', admin.site.urls),
path('playlist/', include(('playlist.urls', 'playlist'), namespace='playlist')),
#users
path('users/', include('django.contrib.auth.urls')),
#accounts (allauth)
path('accounts/', include('allauth.urls')),
...
#django-rest-auth
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
#bug reports for this issue online point to a solution like so, but this isn't fixing the error
url(r"^rest-auth/registration/account-confirm-email/(?P<key>[\s\d\w().+-_',:&]+)/$", allauthemailconfirmation, name="account_confirm_email"),
#jwt
url(r'^refresh-token/', refresh_jwt_token),
]
Can somebody please help me figure out this error? I have looked at many other instances of this problem being posted online and have found many people solving it by catching the registration-confirmation path using a regular expression, I've tried every regex combination I could find in solutions posted online but haven't had any luck solving the problem. I always get the same error when I click the email confirmation link.
Any help is very much appreciated, thx
It would seem you view allauthemailconfirmation uses a TemplateResponseMixin which requires template_name to be defined. You haven't. So in your view, add it. Something like
class allauthemailconfirmation(TemplateResponseMixin, whateverClassesDeclared):
template_name = 'path/to/filename.html'
...

Django Admin - 404 Error when logging in

When going to login to the Django admin on my page, I geta 404 error and a "/admin/login/ url is not defined".
I only get this error while in the Production of my project - it works just fine locally. I am using A2 hosting and their support team has not been able to help me solve this problem.
The stack trace as well as the error url are seen in the second image.
Let me know if you need to see any code, I am more than happy to share I just dont want to be here all day posing all of my .py files when most of them wont matter anyways.
Code by request:
urls.py
from django.contrib import admin
from django.conf.urls import url, include
from django.views.generic.base import TemplateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^students/', include("students.urls")),
url(r'^$', TemplateView.as_view(template_name="home.html"), name="home"),
url(r'^home/$', TemplateView.as_view(template_name="home.html"), name="home"),
url(r'^about/$', TemplateView.as_view(template_name="about.html"), name="about"),
url(r'^how_to_sponsor/$', TemplateView.as_view(template_name="how_to_sponsor.html"), name="how_to_sponsor"),
url(r'^malawi/$', TemplateView.as_view(template_name="malawi.html"), name="malawi"),
url(r'^stories/$', TemplateView.as_view(template_name="stories.html"), name="stories"),
url(r'^donations/$', TemplateView.as_view(template_name="donations.html"), name="donations"),
url(r'^staff/$', TemplateView.as_view(template_name="staff.html"), name="staff"),
url(r'^malawi_education/$', TemplateView.as_view(template_name="malawi_education.html"), name="malawi_education"),
]
admin.site.site_header = 'Maphunziro Project'
UPDATE:
I ran a migration and now the login screen displays like it normally does - I am still getting the login issue however.
Could this be a dependency problem? I have all of the same dependencies installed on the server as my localhost version but maybe I'm missing one that is required for production.
Try the address without the trailing slash 'www.educate-malawi.com/admin/login'. You can take a look at the documentation regarding the APPEND_SLASH here.

Django redirect to url in other app with kwargs

I'm trying to just log a user in by going to a login-redirect page, which then redirects to the users profile page. I understand that this is not the first time this question has been asked, but I've tried all the other answers and I have no idea why this isn't working for me.
urls.py
urlpatterns = [
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name="login"),
url(r'^login/redirect/$', account_redirect, name="account-redirect"),
url(r'^logout/$', auth_views.logout, {'next_page': 'home'}, name="logout"),
url(r'^stores/', include('stores.urls', namespace='store_app'))
]
views.py
def account_redirect(request):
# tried both to see if it would help...
# return HttpResponseRedirect(reverse('store_app:account-landing', kwargs= {"pk":request.user.pk,"name":request.user.vendor.name}))
return redirect('store_app:account-landing', pk=request.user.pk, name=slugify(request.user.vendor.name))
stores/urls.py
urlpatterns = [
url(r'^account/(?P<pk>\d+)/(?P<name>\w+)/$', AccountLanding.as_view(), name="account-landing" ),
]
Based on serveral other answers to related questions, this should work fine, but it doesn't. After I login and get to login/redirect/ I get the following error:
Reverse for 'account-landing' with arguments '()' and keyword arguments '{'pk': 1, 'name': u'Fake Company'}' not found. 1 pattern(s) tried: [u'stores/account/(?P<pk>\\d+)/(?P<name>\\w+)/$']
I don't understand, it's trying the right pattern and has the right arguments? So why does it not work?
slugify has a tendency to turn spaces into hyphens so chances are you need to include that into your url
^account/(?P<pk>\d+)/(?P<name>[\w-]+)/$
Note: This would only be the issue if 'Fake Company' is example data or the error you're showing is what appeared from when you were using the commented out line
The reason slugify does this is because url's can't contain spaces and instead they are turned into %20's, which look ugly.