How to Solve Error Message /accounts/profile in Django

Posted on

Intoroduction

Using the default authentication feature from Django-based web application, there is an error on the implementation of the authentication function itself. As soon as the login process is a success, it soon redirect the page to the /accounts/profile url. The following is the image of the error message :

How to Solve Error Message /accounts/profile in Django
How to Solve Error Message /accounts/profile in Django

Actually, there is a definition of the login process in the script in the following steps :

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', auth_views.LoginView.as_view(template_name='user/login.html'), name='login'),

So, the logic of the login process is actually following the LoginView class. The LoginView class is the one exist for the import declaration which is part of the django library. The import declaration exist through importing the views in the following line on the top of the file :

from django.contrib.auth import views as auth_views

After importing the necessary view, the next process is just to use the LoginView using the file template for defining the layout of the login page. The login page exist in the directory template of ‘user’ in this example. This is the actual directory structure :

(myenv) c:\python-win\system>tree .
Folder PATH listing for volume Windows
Volume serial number is XXXX-XXXX
C:\PYTHON-WIN\SYSTEM
├───app
│   |   model.py
│   |   urls.py
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───project
│   |   model.py
│   |   settings.py
│   |   urls.py
│   └───__pycache__
├───templates
│   ├───app
...
(myenv) c:\python-win\system>

The next thing is that there must be a definition or declaration of the url redirection. If there is no definition or declaration of the login url redirection, the login process will redirect to the default url of ‘/accounts/profile’. So, in the end it will display an error page if the ‘url’ of ‘/accounts/profile’ does not exist in the Django urls.py file. Because the application will not be able to find the suitable page for displaying the output.

Solutions

So, there are two options or alternatives to solve this problem. Either to define the ‘/accounts/profile’ in the urls.py or define another url. This article will show the second option to solve the problem. It is by declaring another url by defining in the ‘settings.py’ file. The following is the entry of the definition in the ‘settings.py’ file :

LOGIN_REDIRECT_URL = 'home'

LOGIN_URL = 'login'

On, the above configuration, there must be a definition of ‘LOGIN_REDIRECT_URL’ which in this case, the ‘home’ value. The definition must be exist in the urls.py file. The following is an example of the definition of it :

urlpatterns = [
    ...
    path('', AppListView.as_view(), name='home'),
    ...
]

The above definition will call the AppListView class and execute it as view. So, the redirect URL if the login process is a success is not the ‘/accounts/profile’. Instead it will return to whatever the AppListView class define. The following is an example of it :

class AppListView(ListView):
    model = App
    template_name = 'app/home.html'   
    ...

It will render the home.html page in the app folder. Normally, it will exist in the template folder.

Leave a Reply