I want to build a tool like hootsuide for my own stuff. As you know, Instagram only allows instagram partners to publish a content via api.
I use instapy as a single python file but I've never used with django before. I'm having some troubles with integration.
views.py :
from django.shortcuts import render, redirect
from instapy import InstaPy
# Create your views here.
def instapy_login(request):
session = InstaPy(username='test', password='test')
session.login()
return redirect("/")
However, I want to use this login credentials for next requests.
For example :
def fetch_followers(request):
session = InstaPy(username='test', password='test') # I don't want to login again.
session.login() # I don't want to login again.
followers = session.grab_followers(username="test", amount="full")
print(followers)
return redirect("/")
I don't want to login in every request. Any idea about fixing it? Thanks alot!
I am also new to django but i think pass instapy_login to your function in which you want to use
Related
I am working on a Django project. I have a backened(Django) and frontend(HTML, CSS,JS).
Everything is working as expected like login, registration, logout, etc.
However, one requirement, and I know it's a very common one, is that if the tab is closed after user logs in(he didn't logout), and the user reopens the url of login, he should be taken to the dashboard instead of again asking for login credentials on login screen.
What Have I tried?
I saw some other answers and I tried putting this check in the "login/" url of my page:
request.user.is_authenticated
It always gives false if I place this check on the login page. I put it on login page's Get method because I want that if user was logged in and he tries to go to the login url, he gets redirected to dashboard. But it always gives False.
What I want help with?
It would be very kind if anyone can guide me on how to achieve the above objective.
The best way is to use cookies.
For Example, you can create a cookie and set the value to true at every login.
This can be done as(views.py):
from django.shortcuts import render
from django.http import HttpResponse
def setcookie(request):
response = HttpResponse("Cookie Set")
response.set_cookie('isloggedin', True)
return response
def getcookie(request):
loggedin = request.COOKIES['isloggedin]
return HttpResponse("Login: " + loggedin);
In my opinion, the best max_age would be a day.
Also make these changes in urls.py:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path('scookie',views.setcookie), #this
path('gcookie',views.getcookie) #and this
]
Reference: https://www.javatpoint.com/django-cookie
There's a token that you pass to frontend upon login, and it stored in the cookies. In your case if you get logged out after closing browser tab, that means you're not able to store the token in the browser.
As mentioned in a comment, try to use sessions or implement authentication system Simple JWT
I am implementing a web application using Django framework.
In my business I need to let the users have access to the app only by google login.
I also don't need to register the users in my database, it isn't a requirement. I just need that the user will uses his google account to enter the site so I will be able to get his real email to send him back the result of the session. It is a one shot app.
I am using django-allauth but it exposes by default a way to login and register users locally. Is it a way to disable every kind of local registration/login and let the user enter in the app only by google login?
Thank you.
From https://django-allauth.readthedocs.io/en/latest/advanced.html#creating-and-populating-user-instances you'll need to:
disable signup on a custom ACCOUNT_ADAPTER
enable signup only through social account (google) on a custom SOCIALACCOUNT_ADAPTER
In practice:
# settings.py
ACCOUNT_ADAPTER = 'myapp.adapters.AccountAdapter'
SOCIALACCOUNT_ADAPTER = 'myapp.adapters.SocialAccountAdapter'
# myapp/adapters.py
from allauth.account.adapter import DefaultAccountAdapter
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
class AccountAdapter(DefaultAccountAdapter):
def is_open_for_signup(self, request):
return False
class SocialAccountAdapter(DefaultSocialAccountAdapter):
def is_open_for_signup(self, request, sociallogin):
return bool(sociallogin)
Hello am new at using flask and I have been trying to figure out how to restrict some web pages unless logged in. I tried flask decorators I tried flask decorators but I couldn't understand it so I tried using flask principal which am able to get it running but still cannot be able to stop the access to that webpage.
Code Description
from flask_principal import Permission, RoleNeed
# create permission with a single Need, in this case a RoleNeed
user_permission = Permission(RoleNeed('User'))
#app.route('/home')
def home_page():
return render_template('home.html')
#app.route('/user')
#user_permission.require()
def do_user_index():
return Response('Only if you are a user')
#app.route('/employee')
def employee_page():
user = User.query.all()
return render_template('employee.html', user=user)
def do_employee_page():
with user_permission.require():
return redirect(url_for('login_page'))
You can use session:
First thing we gonna do is create a session at the moment of login:
#app.route(#route that you gonna use to login data verify)
def login():
#login data verification
flask.session["user data"] = #Data you want to use to identify the user
#the next step that you wanna do
Now we gonna verify the session data in the pages, if the user are log in they gonna have their data in flask.session and if not, they not going to have the data in session.
#app.route('/user')
def do_user_index():
if "user data" not in flask.session:
#Redirect to the login page
return Response('Only if you are a user')
You can consult the documentation to learn more about the how to use session.
https://flask.palletsprojects.com/en/2.0.x/quickstart/#sessions
I am using Flask,Python for my web application . The user will login and if the session time is more than 5 minutes then the app should come out and it should land on the login page.
I tried some of the methods and I can see the session time out is happening but redirect to login page is not happening.
#app.before_request
def before_request():
"Session time out method"
flask.session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(minutes=2)
flask.session.modified = True
flask.g.user = flask_login.current_user
#return redirect(url_for('login'))
I have used before_request for seesion time out.
I have referrred this link Flask logout if sessions expires if no activity and redirect for login page
but I dont see any changes from what I have tried before and this code. I can see lot of stackoverflow questions over there for this topic and I couldnt find the solution.
I have tried this link als
Expire session in flask in ajax context
But I am not sure what should I pass as a session and what qualifier I should return here?
#mod.before_request
def make_session_permanent():
if session_is_invalid(session):
return redirect(url_for('logout'))
def session_is_invalid(ses):
# return your qualifier
if the previous method is correct can some one tell me what is the session and what qualifier I should return here?
What I need is after session log out the page should automatically land on the login screen
What is happening is session log out is happening but it's not redirecting to login page
could some one help me in this?
When I read the documents of the Flask-Login package, i saw a few things. When creating your Flask application, you also need to create a login manager.
login_manager = LoginManager()
A login_view variable in LoginManager class caught my attention. The details include the following explanation:
The name of the view to redirect to when the user needs to log in. (This can be an absolute URL as well, if your authentication machinery is external to your application.)
Actually, after you create a LoginManager object,
login_manager.login_view = 'your login view'
You should specify your login page. Finally,
Once the actual application object has been created, you can configure it for login with:
login_manager.init_app(app)
After doing these, any unauthorized calls to every method you use #login_required annotation will be sent to the page you have pointed out with login_view.
I also developed a simple application. You can review this application from here. I tested it and working without any problems.
I hope it helps you.
I am using Allauth to login with twitter in my Django app.
I have a problem to logout from existing account to login with different one.
I make logout in function logout in my views.py
and within it i tried to call:
from django.contrib import auth
auth.logout(request)
but it didn't work.
I also tried to expire session using this:
request.session.set_expiry(1)
to expire the session after 1 second, but it also didn't work.
By the way I use my own signup and login (I just save mail and password)
So Any ideas?
All auth takes care of it, jut build a url to:
/accounts/logout/
No need to write your own view. If you want to know what it does you could override their view.