Django 1.9.1 NoReverseMatch when using django.auth - python

I'm trying to learn django by following the book "Django by Example" and probably due to conflicting versions i'm running into this problem when trying to use django.auth together with some URL settings in
settings.py.
Im getting totally frustrated by now since i have no idea how to even begin debugging this error. Any help or advice would be much appreciated
Here's the relevant part of the settings.py file
from django.core.urlresolvers import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('dashboard')
LOGIN_URL = reverse_lazy('login')
LOGOUT_URL = reverse_lazy('logout')
app views.py:
from django.shortcuts import render, redirect
from django.shortcuts import HttpResponse
from django.contrib.auth import authenticate, login, logout
from .forms import LoginForm
from django.contrib.auth.decorators import login_required
# Create your views here.
#login_required
def dashboard(request):
return render(request, 'account/dashboard.html', {'section': 'dashboard'})
urls.py
from django.conf.urls import url
from . import views
app_name = 'account'
urlpatterns = {
url(r'^$', views.dashboard, name='dashboard'),
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout'),
url(r'^logout-then-login/$', 'django.contrib.auth.views.logout_then_login', name='logout_then_login'),
}
Main urls.py :
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^account/', include('account.urls')),
]
Error Message
updated settings.py :
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')

When you use app_name that sets up a namespace that will be used when you include() that urls.py somewhere else.
So there's no url with the name "login", instead it's called "account:login", and that's the name you have to pass to reverse().
LOGIN_REDIRECT_URL = reverse_lazy('account:dashboard')
LOGIN_URL = reverse_lazy('account:login')
LOGOUT_URL = reverse_lazy('account:logout')
Relevant docs: URL namespaces and included URLconfs
If you are using django-extensions (you should), you can use the management command show_urls to get a nicely formatted list of all the url routes that are registered in your project.

Related

My code doesn't run it says there is a circular import error I'm try to load the home view so that it redirects me

I am working on a django project, but it returns
the included urlconf "myapp.urls"does not appear to have any patterns in it.
I tried checking my views to ensure I imported everything correctly
from django.contrib import admin
from django.urls import path
from .views import home
from accounts.views import login_view
urlpatterns = [
path('admin/', admin.site.urls),
path('',home),
path('accounts/login/', login_view),
]
I expect the site to run and redirect me to the login page
This is my views in the same directory with the urls.py
#login_required
def home(request):
return render(request,"home.html")
Your code doesn't have a problem, send the repository link
Include this In your settings.py
settings.py
LOGIN_URL = '/accounts/login/'
LOGOUT_URL = "/accounts/logout/"
LOGIN_REDIRECT_URL = " "

Heroku cannot import name 'login' [duplicate]

I try to build a login function for my page. To edit the urls.py as followed, it keeps printing this:
cannot import name 'login' from 'django.contrib.auth.views'
How could I deal with the problem?
from django.contrib.auth.views import login
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path("login/",
login,
{"template_name": "users/login.html"},
name="login"),
]
Since django-1.11, the login, logout, etc. function-based views have been rewritten to class-based views: the LoginView [Django-doc] and LogoutView [Django-doc] classes, as is specified in the release notes. The "old" function-based views could still be used, but were marked as deprecated.
In django-2.1, the old function-based views have been removed, as specified in the release notes.
You can write it like:
from django.contrib.auth.views import LoginView
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path('login/',
LoginView.as_view(
template_name='users/login.html'
),
name="login"
),
]
try this
app_name = 'users'
urlpatterns = [
url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
You can try with this code:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns = [
# Login page.
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]
#Willem Van Onsem's answer worked for me. On an implementation note, if you rather keep your view code separate from urls (also if you have some processing to do), you would write your urls.py like this (based on a per-app urls.py file in your app folder which means you have to include it in the overall urlpatterns of the project's urls.py file which is in the same folder as your settings.py file, with the syntax path('', include('users.urls')),):
from django.urls import path
from .views import (
login_view
)
app_name = "userNamespace"
urlpatterns = [
path('login/', loginView.as_view(), name="login-view"),
]
and over in your views.py file you would have something like this:
from django.shortcuts import render
from django.contrib.auth.views import (
LoginView,
)
from users.models import User
class login_view(LoginView):
# The line below overrides the default template path of <appname>/<modelname>_login.html
template_name = 'accounts/login.html' # Where accounts/login.html is the path under the templates folder as defined in your settings.py file
Can try this to create a login form
# views page
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.contrib import messages
def loginPage(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Username or Password is incorrect')
context = {}
return render(request, 'accounts/login.html', context)
#urls
urlpatterns = [
path('login/', views.loginPage, name='login'),,
]

Django cannot import login from django.contrib.auth.views

I try to build a login function for my page. To edit the urls.py as followed, it keeps printing this:
cannot import name 'login' from 'django.contrib.auth.views'
How could I deal with the problem?
from django.contrib.auth.views import login
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path("login/",
login,
{"template_name": "users/login.html"},
name="login"),
]
Since django-1.11, the login, logout, etc. function-based views have been rewritten to class-based views: the LoginView [Django-doc] and LogoutView [Django-doc] classes, as is specified in the release notes. The "old" function-based views could still be used, but were marked as deprecated.
In django-2.1, the old function-based views have been removed, as specified in the release notes.
You can write it like:
from django.contrib.auth.views import LoginView
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path('login/',
LoginView.as_view(
template_name='users/login.html'
),
name="login"
),
]
try this
app_name = 'users'
urlpatterns = [
url(r'^login/$', LoginView.as_view(template_name='users/login.html'), name='login'),
]
You can try with this code:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns = [
# Login page.
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]
#Willem Van Onsem's answer worked for me. On an implementation note, if you rather keep your view code separate from urls (also if you have some processing to do), you would write your urls.py like this (based on a per-app urls.py file in your app folder which means you have to include it in the overall urlpatterns of the project's urls.py file which is in the same folder as your settings.py file, with the syntax path('', include('users.urls')),):
from django.urls import path
from .views import (
login_view
)
app_name = "userNamespace"
urlpatterns = [
path('login/', loginView.as_view(), name="login-view"),
]
and over in your views.py file you would have something like this:
from django.shortcuts import render
from django.contrib.auth.views import (
LoginView,
)
from users.models import User
class login_view(LoginView):
# The line below overrides the default template path of <appname>/<modelname>_login.html
template_name = 'accounts/login.html' # Where accounts/login.html is the path under the templates folder as defined in your settings.py file
Can try this to create a login form
# views page
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.contrib import messages
def loginPage(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
messages.info(request, 'Username or Password is incorrect')
context = {}
return render(request, 'accounts/login.html', context)
#urls
urlpatterns = [
path('login/', views.loginPage, name='login'),,
]

Django After login succeed, redirect again to login page

My Django project has 5 apps: app1, app2 ......
each app has one template index.html and 1 view named index.
project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('main.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
app1/urls.py
from django.conf.urls import url, include
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^accounts/login/$', auth_views.login, name='login'),
app1/views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, user_passes_test
def is_member(user):
return user.groups.filter(name='group1').exists()
#login_required
#user_passes_test(is_member)
def index(request):
getting our template
template = loader.get_template('app1/index.html')
rendering the template in HttpResponse
return HttpResponse(template.render())
setting.py
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
The 5 apps requirer login based on group
app one I have no problem to login but the reset of the apps after login succeed, I will be redirected to login page again.
The reset of the apps has exactly the same code except the group name and template location.
Also the logger result show me that there is no problem with credential.
I tried to change the group nae for other app as app1 and still I am getting same result.
Please advice me how to solve this problem.

if condition in urls.py - django

I want to check if user not loged(don't have a session) he go to login.html file and if he loged(have a session) he go to profile.html , but i want to check it at urls.py
My projects Tree :
manage.py
url
----setting.py
mr_url
-----templates
---------url
------------profile.html
------------login
-----------------logon.html
-----views.py
-----models.py
-----urls.py
My urls.py :
from django.conf.urls import url
from . import views
from django.views.generic import TemplateView
urlpatterns =[
url(r'^$', views.profile, name='profile'),
]
How i can check set session for user or not in urls.py ????
Note : i can do it in views.py but i don't want!
You can use login_required decorator:
from django.contrib.auth.decorators import login_required
urlpatterns =[
url(r'^$', login_required(views.profile), name='profile'),
]
If the user is not logged in, he will be redirected to the login page whose default is accounts/login. You can customize it by setting LOGIN_URL in your settings.
Hope it helps!

Categories

Resources