How do I make allauth's authentication available on my homepage URL? - python

So i've installed Django allauth to handle my login authentication. However I don't want a seperate URL for login. I want it to be available on my homepage via Javascript onclick (login box appears when you click a button/no page refresh). But the current allauth URL is located at '/accounts/login'. So as I said I need it located at '/'. How would I do this?
Here is my views.py:
def boxes_view(request):
...
search = request.GET.get('search')
posts = Post.objects.all().filter(category=1).order_by('-date')
if search:
posts = posts.filter(
Q(title__icontains=search) |
Q(content__icontains=search)
)
else:
posts = Post.objects.all().filter(category=1).order_by('-date')
context = {
'posts': posts,
}
return render(request, 'polls.html', context)
Can I pass it in as context or? How would it be done?

It sounds like you want a user to be able to log in using a modal from your homepage, or what you currently see when going to '/'
If so, you don't change the allauth login url. Instead change your javascript code for your login box to present the contents given here.
See also this answer.

Related

Skipping Initial Login Page with Django Allauth

I am using django-allauth for a website. I have integrated Google login through it. Currently, when I access the login, I get the standard sign-in page, on which is a link to sign in through Google. What is the best way to skip the standard sign-in page and go straight to the Google one (as in the one on accounts.google.com...)? All login is done through Google, so I don't need to see the initial page, just the Google one.
Should I override the provided template to just redirect? Or is there a better way to configure it?
I know it is a old topic, but I had the same issue and could not find a Solution on the web because of that I want to share my best solution. First of all I created a new socialaccounts template, so that this one will be taken instead of the original allauth template. For that you need to create a new folder and html file as followed:
"template->socialaccount-> login.html"
Thats the page you want to skip. To do that I simply inserted a javascript which, submits the form as soon the page is loaded:
{%load socialaccount%}
<body onload="document.forms['google_login'].submit()">
<p>You should be redirect to the Google login page in some seconds otherwise use the button below.</p>
<form method="post" name= "google_login">
{% csrf_token %}
<button type="submit">Continue</button>
</form>
</body>
Maybe this helps someone else.
Best regards
I just copied the address to which the existing Django-template hyperlink pointed, and then created a view to redirect all logins to that link with the correct return page. My URL pattern:
url(r'^login/$', views.redirectToGoogle, name="rediect_to_Google")
My view was:
GOOGLE_LOGIN_URL_PREFIX = '/accounts/google/login/?'
def redirectToGoogle(request):
coming_from = request.GET.get("next", "/manage")
url_params = {
"process": "login",
"next": coming_from
}
suffix = urllib.parse.urlencode(url_params)
return redirect(GOOGLE_LOGIN_URL_PREFIX + suffix)

Django 1.10.0 LOGIN_REDIRECT_URL NOT Working

I have been using the default django auth for login and I am also using the decorator #login_required for some of the pages. There is no need to remember the previous page if the user hits logout and stuff. The idea is, the user will be redirected to a static page once logged in. How do I do that in django? I do not want to edit login template.
LOGIN_URL = '/accounts/login/' #redirects the user to the login page if not logged in already
LOGIN_REDIRECT_URL = '/something-else' #does not work.
In my urlconf, it's defined like
url(r'^something-else/$', views.something_else, name='something-else')
Any help would be highly appreciated. Thanks.

Django url pk + slug page not found

I'm using django allauth for user authentication and so far I have been able to display user's page like this: account/1/trial/, where 1 is the pk number and trial is the username(unique), or like this: account/1/. In both cases everything works fine, but if I want to show only the username (account/trial/) in the url than I can't load the profile (accounts/profile/) page for the logged in user(404). Probably my profile function is wrong, how can I correct it so that the page will load normally as if using pk in urls.
urls:
(r"^(?P<pk>\d+)/(?P<slug>[\w.#+-]+)/$", DetailView.as_view(context_object_name='detail',slug_field = "username",model=User,template_name='account/user_detail.html'), name='detail_view'), #if I use this url the page loads correclty
(r"^(?P<pk>\d+)/$", DetailView.as_view(context_object_name='detail',slug_field = "username",model=User,template_name='account/user_detail.html'), name='detail_view'), # also with this url it works
(r"^(?P<slug>[\w.#+-]+)/$", DetailView.as_view(context_object_name='detail',slug_field = "username",model=User,template_name='account/user_detail.html'), name='detail_view'), #if I use only the slug the page does not load.
(r"^profile/$", views.profile, name="profile_view"), #this is the profile page url
and the profile page view:
def profile(request):
return render_to_response("account/profile.html",locals(),context_instance=RequestContext(request))
The string "profile" matches the regex for the slug-only username view, and since Django matches URLs in order, a request for the URL "profile/" will always go to that view instead. The simple solution is to move the profile URL above the other one.

Django dynamic page functionality and url

I have a some problem with routing in my django app.
The problem:
There is some dynamic website and site administrator can create pages with random urls. For example, he can create a news page with url "company/news" or "store/news". Or he can create the page with feedback form with url "feedback" or "user/feedback".
So, Django needs to catch this request and show appropriate news or feedback content for these pages. How can I route the user request to the appropriate view according to the requested page functionality?
You can create view that parses your URL and chooses strategy for different types.
# urls.py
...
url(r'^dynamic-view/(?P<dynamic_view_url>.*)/$', 'dynamic_view')
# views.py
def dynamic_view(request, dynamic_view_url):
url_parts = [p for p in dynamic_view_url.split("/") if p]
if "feedback" in url_parts:
return _view_for_feedback(request, url_parts)
elif "news" in url_parts:
return _view_for_news(request, url_parts)
else:
raise Http404

Sending params to the next_page after the logout

I want to have my users redirected back to the home page after logout. But I'd like to display a "logged out successfully" message in addition to the regular page.
I got the logging out and redirection working:
'django.contrib.auth.views.logout', {'next_page': '/portal/home'}
But I can't seem to find a way to send some information to the homepage through the logout view. Any suggestions?
You might want to check out the messaging framework built into Django. Seems to fit your needs for this problem.
https://docs.djangoproject.com/en/dev/ref/contrib/messages/
In your views
def my_logout(request):
logout(request)
msg="logged out"
return render_to_response('portal/home',{'check':msg})
In your home.html write
{{check}}

Categories