After log out user in my django web app, the redirected homepage still displays the "Log Out" button instead of the "Sign in with Facebook". In the following code, I follow the django documentation to logout the user and redirect the page to homepage which is the base.html. It seems that my web app still has user.is_authenticated as True after log out? What am I missing?
I cannot find any useful hint online. Any comment is much appreciated.
Here is part of my template html
<div class="navbar-form navbar-right">
{% if user.is_authenticated %}
<a id="logout" href="/accounts/logout" class="btn btn-success">Logout</a>
{% else %}
<a id="facebook_login" href="/accounts/facebook/login" class="btn btn-success">Sign in with Facebook</a>
{% endif %}
</div>
Here is my urls.py
url(r'^$', 'homepage.views.home', name='home'),
url(r'^accounts/', include('allauth.urls')),
url(r'^accounts/logout/$', 'homepage.views.logout', name='logout'),
Here is my homepage/views.py
# Create your views here.
def home(request):
return render(request, "base.html", {})
# ensure only logged in users can access the view.
#login_required
def logout(request):
logout(request)
# Take the user back to the homepage.
return redirect('home')
There are 2 things here:
You need to reorder the URLs
from:
url(r'^accounts/', include('allauth.urls')),
url(r'^accounts/logout/$', 'homepage.views.logout', name='logout'),
to
url(r'^accounts/logout/$', 'homepage.views.logout', name='logout'),
url(r'^accounts/', include('allauth.urls')),
This way, your logout takes precedence over the allauth's logout URL pattern
You should be aliasing the imported logout, or rename your logout to something different.
Example:
from django.contrib.auth import logout as auth_logout
and then
def logout(request):
auth_logout(request)
....
Related
I know similar kind of question is asked before, but I was not able to get it. This is my first project as a Django beginner.
In my Django blog app, I made a delete button but it is not working and I am finding for answers, trying different methods on the web but it did not help.
I am trying to do is when admin open the post, then on clicking the delete button, it take the post-id and delete that post and redirect to home page, but it is not working as expected. So, lastly I came here. Any help will be appreciated. Thanks!
This is my urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('post/<int:pk>', views.post, name='post'),
path('about', views.about, name='about'),
path('contact_us', views.contact_us, name='contact_us'),
path('register', views.register, name='register'),
path('login', views.login, name='login'),
path('logout', views.logout, name='logout'),
path('create_post', views.create_post, name='create_post'),
path('delete_post', views.delete_post, name='delete_post')
]
This is my views.py file:
def delete_post(request, *args, **kwargs):
pk = kwargs.get('pk')
post = get_object_or_404(Post, pk=pk)
if request.method == 'POST':
post.delete()
return redirect('/')
return render(request, 'delete-post.html')
This is delete post html form:
<form action="{% url 'delete_post' post.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete post">
</form>
Delete button:
<button type="button" class="btn btn-danger" style="position:relative; right: -1145px;">Delete</button>
for deleting a post
def delete_post(request, id):
post = Post.objects.filter(id=id)
address.delete()
return redirect('/')
and in your html
<a class="btn btn-outline-danger" href="{% url 'appname:delete_post' id=post.id %}">Delete It</a>
and in your urls.py
path('<int:id>/delete-post',views.delete_post,name='delete_post')
I'm currently facing an issue that I really don't understand at all, I searched through the whole code and was not able to find any references there, I deleted all caches, databases, venv etc. With no effects at all.
It's all about the 'login' url pattern
urls.py
from Project_Accounts import views as Project_Accounts
....
url(r'^admin/', admin.site.urls),
# Reg and Auth
url(r'^login/$', Project_Accounts.login, name='login'),
url(r'^signup/$', Project_Accounts.signup, name='signup'),
....
urlpatterns += [
path('Project_Accounts/', include('django.contrib.auth.urls')),
]
base.html
{% if user.is_anonymous %}
<a href="{% url 'signup' %}" class="top-menu">
<button type="button" class="btn btn-success">Sign-Up</button>
</a>
<a href="{% url 'login' %}" class="top-menu">
<button type="button" class="btn btn-primary">Login</button>
</a>
{% endif %}
views.py (Project_Accounts)
def login(request):
if request.method == 'POST':
form = LoginForm(request.POST, request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
request.session.flush()
if user.pubpgp:
if user.pubpgp_enabled:
request.session['userID'] = user.pk
return redirect(reverse('login_2fa'))
else:
hybridlogin(request, user)
return redirect(reverse('home'))
else:
hybridlogin(request, user)
# Redirect to a success page.
return redirect(reverse('home'))
else:
return render(request, 'Project_Accounts/login.html', {'form': form})
else:
return render(request, 'Project_Accounts/login.html', {'form': LoginForm()})
If my base.html gets displayed, signup view works fine, and every other also except the login. The URL I get back for login is
127.0.0.1:8000/Project_Accounts/login
but it used to be
127.0.0.1:8000/login
Any idea why that can appear?!
There is really no reference here and the error that gets generated by the WSGI server is:
TemplateDoesNotExist at /Project_Accounts/login/
registration/login.html
Request Method: GET
Request URL: http://127.0.0.1:8000/Project_Accounts/login/
Django Version: 2.1.4
Exception Type: TemplateDoesNotExist
Exception Value:
registration/login.html
Where does registration/login.html comes from!?!?
Makes no sense to me and I also don't find any reference here in my code.
I'm thankful for every hint.
Kind regards.
I have created a login view in my project urls.py,
urlpatterns = [
path('login/', views.LoginView.as_view(template_name='login.html'), name='login'),
]
login.html
<div class="container">
<h2>Login</h2>
<form action="\" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</div>
Till here it works fine I can see login page and login into it. But now I can login with any anonymous credentials and they can see the home page.
So how do I restrict it to registered user and then logout them.
When you add path('login/'... stuff to urls.py it does nothign to access to home page. You will need to either forbid access to homepage explicitly by adding login_required to your view:
from django.contrib.auth.decorators import login_required
#login_required
def my_view(request):
...
Or you can use LoginRequiredMixin for class based view. Or as another option you can use django-stronghold package.
I'm trying to create a logout page for django.
This is the views.py file:
def index(request):
if not request.user.is_authenticated():
return redirect('webapp/login.html')
else:
result = Hello_World.delay()
somethingDownByCelery = result.get(timeout=2)
context = {'somethingDownByCelery': somethingDownByCelery, 'userName': request.user.username}
return render(request, 'webapp/index.html', context)
def loginUser(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('webapp/index.html')
else:
return redirect('webapp/disabled.html')
else:
condition = "Invalid Login"
context = {'condition', condition}
return render(request, 'webapp/index.html', context)
def logoutUser(request):
logout(request)
return redirect('webapp/index.html')
This is the index page after the logout is initiated.
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'WebApp/style.css' %}"/>
Hello World, this will call celery!
<html>
<br>
</html>
{{ somethingDownByCelery }}
<html>
<br>
<br>
</html>
Hello! {{ userName }}
<html>
<br>
<br>
</html>
<form action="{% url 'WebApp:logout'%}" method="post">
{% csrf_token %}
<p> Logout </p>
<input type="submit" value="Submit">
</form>
What should happen is that the user would logout, and get redirected to the index page, whereas since the user is not logged in, it will redirect the user to a login page.
However, it only shows me: The view django.contrib.auth.logout didn't return an HttpResponse object.
This is the project root urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from WebApp import views
from StripCal import views
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Dashboard_Web.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^webapp/', include('WebApp.urls', namespace="WebApp")),
)
This is the app's urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from WebApp import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^login', views.login, name='login'),
url(r'^logout', views.logout, name='logout'),
)
try this:
from django.shortcuts import HttpResponseRedirect
def logoutUser(request):
logout(request)
return HttpResponseRedirect('/loginpage/')
instead of webapp/index.html, you should give the URL of your login page like /loginpage/ inside HttpResponseRedirect
Instead of writing your own, use Djangos builtin logout view for this.
#urls.py
from django.contrib.auth.views import logout
url(r'^sign-out/$', logout, {'template_name': 'index.html', 'next_page': '/'}, name='sign-out'),
Docs are found here and now you can link to this without the use of a form and Django will take care of doing the redirection for you.
I know that this is a old post, but no answer addressed what went wrong in the code so here it is.
In the webapp urls.py file view must be pointing to logoutUser view and not logout view.
and similarly with the login view
from django.contrib import admin
from WebApp import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^login', views.loginUser, name='login'),
url(r'^logout', views.logoutUser, name='logout'),
)
Now the url function is taking the intended view method as an argument
First things first.
Your function should look similar to this one:
def logout_view(request):
logout(request)
return redirect('/login')
To use logout, you have to import logout like this:
from django.contrib.auth import logout
If you are redirecting user on the basis of User.is_authenticated, it would never redirect because if the user is not logged in, an anonymous user it created.
You can do something like ths:
def home(request):
if request.user.is_anonymous == True:
return redirect('/login')
else:
return render(request,"index.html",{'nav':request.user})
When I try to go to URL of view with #login_required, it redirects me to login form. But the rendered form has no action=attribute and when I click submit button nothing happens. But when I go to /admin/ I got the working login form. After I log in that way all views are working. I'm using admin_bootstrap so it's different login form. But why the default one is acting that way?
Relevant code:
mySubApp/views.py:
#login_required
def index(request):
user = request.user
return HttpResponse("Hello, world. You're at index.")
mainApp/urls.py:
#login
url(r'^login/$', 'django.contrib.auth.views.login', name='LogMeIn'),
#mySubApp
url(r'^subapp/', include('mySubApp.urls')),
mySubApp/urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
settings.py:
LOGIN_URL = 'LogMeIn'
Do I need to create a custom login form to make it work or what?
You have to define your own templates when using the authentication views (they aren't provided by defualt):
Django provides several views that you can use for handling login, logout, and password management. These make use of the stock auth forms but you can pass in your own forms as well.
Django provides no default template for the authentication views - however the template context is documented for each view below.
So create a registration/login.html:
<form action="{% url auth:login %}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>