Application showing login page even after user is logged in Django - django

When I log in to the Django project and then press the back button or enter the login page url, I am getting the Login page again even when the user is authenticated. I have tried adding the redirect_authenticated_user as in the code below:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.decorators import login_required
from django.contrib.auth import views as auth_views
from django.conf.urls import url
admin.autodiscover()
admin.site.login = login_required(admin.site.login)
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
]
Not sure if I have implemented something incorrectly.

I think you need to swap the order of your url patterns like this:
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
path('accounts/', include('django.contrib.auth.urls')),
]
When you try to access an endpoint, Django matches it with the first suitable url in your urlpatterns (In your case it was default django login with no parameters passed)

Related

DetailView Django dont work error page not found

When I click on the article I get
How can I fix it?
I tried url, path and other but I have this error
urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('index', include('mainpage.urls')),
path('blog', include('news.urls')),
path('games', include('games.urls')),
path('fortnite', include('fortnite.urls')),
path('contacts', include('contact.urls')),
path('admin', admin.site.urls),
]
news/urls.py
from django.urls import path,include,re_path
from django.conf.urls import url
from . import views
from django.views.generic import ListView, DetailView
from news.models import Articles
urlpatterns = [
path('', ListView.as_view(queryset=Articles.objects.all().order_by('-date')[:20],template_name='news.html')),
path('<int:pk>/', DetailView.as_view(model=Articles, template_name='news/post.html'))
]
First of all please add '/' at end of all your path strings , change it like this
path('index/', include('mainpage.urls')),
path('blog/', include('news.urls')),
path('games/', include('games.urls')),
path('fortnite/', include('fortnite.urls')),
path('contacts/', include('contact.urls')),
path('admin/', admin.site.urls),
]
check if it works or not

Routing error: trying to route a defined route in urls.py

I encountered a strange behavior of Django urls. Although forecast/upload url is defined in the urls.py, Django is saying
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/forecast/upload/
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
polls/
admin/
forecast/ [name='index']
forecast/ **forecast/upload/** [name='upload']
forecast/ help.html [name='help']
The current path, **forecast/upload/**, didn't match any of these.
Project url.py:
from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('forecast/', include('forecast.urls')),
]
Application urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
#url(r'^$', views.home, name="home"),
path('forecast/upload/', views.upload, name="upload"),
path('help.html', views.help, name="help"),
]
You have specified "forecast" twice; once at project level and once in the app. So your URL would be "forecast/forecast/upload".
Presumably, you don't want that, in which case you should remove the "forecast" from the pattern in the app urls.

urls.py django 2.0.2 need parameters

i'm learning django and i'm making a blog tutorial but it's from older version that i have, i have version 2.0.2 and i'm not understand documentation my problem is that i dont know how configure my urls.py
this is my three proyect:
i need to put archive.html in 127.0.0.1:8000/ and this is my urls code
codigofacilito/blog.urls.py :
"""codigofacilito URL Configuration
The urlpatterns list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin<br/>
from django.urls import path<br/>
from . import views<br/>
urlpatterns = [
path('admin/', admin.site.urls),
]
and codigofacilito/codigofacilito.urls.py:
from django.contrib import admin
from django.urls import path
from . import views
enter code here`urlpatterns = [
path('admin/', admin.site.urls),
]
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name = 'archive.html')),
path('admin/', admin.site.urls),
]
put this in your project's urls.py, this will directly render the archive.html when you visit 127.0.0.1:8000/ in your browser.
but if you want to render data from backend in this html page then i suggest you to use views (function based , class based, etc..)
then you just have to import the views in url file and specify path for that.
from your_app.views import your_view
urlpatterns = [
path('/', your_view),
]

Adding a new url gives error

In my urls.py I have this.
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from events import views as eviews
urlpatterns = [
url(r'^user/', include('accounts.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', eviews.events_list, name='events_list'),
url(r'^(?P<o_type>[-\w]+)/$',
eviews.events_list,
name='events_list_by_org'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$',
eviews.event_detail, name='event_detail'),
]
when I add the "user" url which is at first line in urlpatterns, it shows error on line 26 which is "events_list_by_org" url. When I comment out or remove 'user' url it works fine but shows error when it's included.
Try separating your event urls like your accounts url inside account app, and adding namespece to accounts and events.
urls.py in core
urlpatterns = [
url(r'^user/', include('accounts.urls', namespace='accounts')),
url(r'^event/', include('event.urls', namespace='event')),
]
urls.py in event app
urlpatterns = [
url(r'^$', eviews.events_list, name='events_list'),
url(r'^(?P<o_type>[-\w]+)/$',
eviews.events_list,
name='events_list_by_org'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$',
eviews.event_detail, name='event_detail'),
]
Maybe it's because of follwoing namespaces, so trying your namespace to user urls.

Django redirect user to app based on permission and group

Say i have a django project which have 4 apps 1 app is for logging in
project urls:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('Login.urls')),
url(r'^admin/', admin.site.urls),
]
settings:
LOGIN_REDIRECT_URL = '/'
login app urls:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login/$', auth_views.login, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
]
now say i have 3 apps based on user type i.e. Admin, Team Lead, Worker
so i want to redirect user based on their type of employment.
how can i achieve this ?
any help is appreciated
I guess that you will manually add/edit the user groups.
That can be done from django admin panel.
Here is the group/permission django docs: Permissions and Authorization
This is what I did:
I've created an index view in order to get the user group and redirect to the respective view that you want.
So, index view in views.py:
from django.contrib.auth.decorators import login_required
#login_required
def index(request):
group = request.user.groups.filter(user=request.user)[0]
if group.name=="employees":
return HttpResponseRedirect(reverse('worker'))
elif group.name=="teamLeader":
return HttpResponseRedirect(reverse('teamLeader'))
elif group.name=="admin":
return HttpResponseRedirect(reverse('adm'))
context = {}
template = "index.html"
return render(request, template, context)
And urls in urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from app import views as app_views
urlpatterns = [
url(r'^login/$', auth_views.login, name='login'),
url(r'^$', app_views.index, name='index'),
url(r'^employees/$', app_views.employees, name='employees'),
url(r'^teamLeader/$', app_views.teamLeader, name='teamLeader'),
url(r'^adm/$', app_views.adm, name='adm'),
]
In settings.py
LOGIN_URL = '/login'
LOGIN_REDIRECT_URL = '/'
Is this what you want to do?