0

I've been reading the docs and several questions already answered, but nothing seems to work, so here it the question. I'm starting with django and I can't manage to get rid of the following error:

Using the URLconf defined in alpha_trader.urls, Django tried these URL patterns, in this order:

__debug__/
[name='home']
accounts/update [name='user_change']
accounts/ login/ [name='login']
accounts/ logout/ [name='logout']
accounts/ password_change/ [name='password_change']
accounts/ password_change/done/ [name='password_change_done']
accounts/ password_reset/ [name='password_reset']
accounts/ password_reset/done/ [name='password_reset_done']
accounts/ reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/ reset/done/ [name='password_reset_complete']
accounts/signup [name='user_signup']
The current path, accounts/update.html, didn't match any of these.

Hereby my settings.py:

# Custom Django auth settings

AUTH_USER_MODEL = 'accounts.User'

LOGIN_URL = 'login'

LOGOUT_URL = 'logout'

LOGIN_REDIRECT_URL = 'home'

LOGOUT_REDIRECT_URL = 'home'

Hereby urls.py:

from django.urls import include, path
from django.conf import settings

from accounts import views

urlpatterns = [
    path('', include('accounts.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/signup', views.SignUpView.as_view(), name='user_signup'),
]

hereby accounts/urls.py:

from django.urls import include, path
from accounts import views

urlpatterns = [
    path('', views.home, name='home'),
    path('accounts/update', views.UserUpdateView.as_view(), name='user_change'),
]

hereby accounts/views.py:

def home(request):
    if request.user.is_authenticated:
            return redirect('accounts/home.html')
    return render(request, 'accounts/home.html')

and finally accounts/models.py:

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.html import escape, mark_safe


class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    num_of_saved_backtests=models.IntegerField(default=0)
    num_of_online_indicators=models.IntegerField(default=0)

Does anyone sees the error in my code? IMPORTANT INFO I'm capable to use home when I'm not logged in. It doesn't work one I have managed to log in as a user...

Alvaro
  • 1,430
  • 2
  • 23
  • 41
  • `return redirect('accounts/home.html')` don't use the file path, but instead you can use the function name, or the url name, depends on the view where you want to redirect to – Lemayzeur Jan 13 '19 at 19:52
  • Thanks for the comment. I just used the url name and it doesn't work... It looks like it enters a loop: [13/Jan/2019 20:58:37] "GET / HTTP/1.1" 302 0 [13/Jan/2019 20:58:37] "GET / HTTP/1.1" 302 0 [13/Jan/2019 20:58:37] "GET / HTTP/1.1" 302 0 – Alvaro Jan 13 '19 at 20:01
  • @Lemayzeur, regarding the function name, there is none, given that this is the home page – Alvaro Jan 13 '19 at 20:02
  • *It looks like it enters a loop* coorect!. – Lemayzeur Jan 13 '19 at 20:10
  • But in this case the error is given by chrome: Page 127.0.0.1 has redirected you too many times. – Alvaro Jan 13 '19 at 20:20
  • You're right, my approach was about the path. a redirection can't be to the same view in the case the condition will be the same, this will boil down to a loop – Lemayzeur Jan 13 '19 at 20:23

1 Answers1

0

return redirect('accounts/home.html') In this line, don't redirect to the file path, but instead you can use the function name, or the url name, depends on the view where you want to redirect to.

return redirect('url_name') # or redirect('function_name')

Also a redirection can't be to the same view in the case the condition will be the same, this will boil down to a infinite loop.

Lemayzeur
  • 8,297
  • 3
  • 23
  • 50