login-required

Django - how to tell if user was redirected to the page by @login_required?

僤鯓⒐⒋嵵緔 提交于 2021-02-11 14:20:16
问题 I want to use the @login_required decorator for a view called allUsers_page , which is used when you go to the url r'^users/allUsers$' In my settings.py, I have LOGIN_URL='/login' so when a user goes to the allUser_page view (when he goes to the url users/allUsers , if the user is not signed into his account, it will redirect him to the login page. Now, in the login page, is there a way for me to find out if the user just went to the login page directly or if he was redirected to the login

Django - Protecting Media files served with Apache with custom login_required decorator

橙三吉。 提交于 2020-01-30 08:03:23
问题 I deployed a Django app using Apache, and I check for authentication in most views using a decorator. @custom_decorator def myView(request): bla bla bla... It's not the @login_required decorator that comes with Django, but it's almost the same thing, except that allows access only to users from certain groups. This works as intended. Also, I'm serving media (user uploaded) files with Apache, like this: Alias /media /path/to/media <Directory /path/to/media> Require all granted </Directory I

Why do I need to decorate login_required decorator with @method_decorator

半城伤御伤魂 提交于 2019-12-20 10:42:05
问题 I am trying to understand the code for the mixins posted at this blog post. These mixins call the login_required decorator from django.contrib.auth.decorators within the mixins , but they do so decorated by the method_decorator from django.utils.decorators . In the sample code below I dont understand why I need to decorate the login_required decorator . from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required class LoginRequiredMixin

@login_required trouble in flask app

ε祈祈猫儿з 提交于 2019-12-19 16:35:20
问题 I have created a blueprint that handles authenticating. This blue print uses Flask-Login. And has the following, as well as more code not shown. In the blueprint I have the following: from flask.ext.login import LoginManager from flask.ext.login import UserMixin from flask.ext.login import current_user from flask.ext.login import login_required from flask.ext.login import login_user from flask.ext.login import logout_user auth_print = Blueprint('auth_print', __name__) login_manager =

@login_required trouble in flask app

匆匆过客 提交于 2019-12-19 16:33:14
问题 I have created a blueprint that handles authenticating. This blue print uses Flask-Login. And has the following, as well as more code not shown. In the blueprint I have the following: from flask.ext.login import LoginManager from flask.ext.login import UserMixin from flask.ext.login import current_user from flask.ext.login import login_required from flask.ext.login import login_user from flask.ext.login import logout_user auth_print = Blueprint('auth_print', __name__) login_manager =

Django: How can I apply the login_required decorator to my entire site (excluding static media)?

六月ゝ 毕业季﹏ 提交于 2019-12-18 10:42:51
问题 The example provides a snippet for an application level view, but what if I have lots of different (and some non-application) entries in my "urls.py" file, including templates? How can I apply this login_required decorator to each of them? (r'^foo/(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'), (r'^$', 'django.views.generic.simple.direct_to_template', {'template':'homepage.html'}), 回答1: Dropped this into a middleware.py file in my project root (taken from http://onecreativeblog.com/post

Django: re-use login_required decorator inside other decorators

心不动则不痛 提交于 2019-12-12 08:53:20
问题 According to one of the comments in https://stackoverflow.com/a/8715790/210481, which I agree with, we should avoid multiple decorators if one depends on the other. So, in the example, if we have a decorator "active_required" for active users, we should not have to use both active_required and login_required on the same view. We should have "login_required" decorator "called" somehow inside the "active_required" one. Is it possible to do it with the standard "login_required" decorator that

Django: UpdateView restrict per user

喜欢而已 提交于 2019-12-10 13:27:20
问题 I have a site where users can create and edit their own lists. I'm using the generic view CreateView to allow users to create lists. I would like to use the generic view UpdateView to allow them to edit the lists, but the login_required=True is not enough in this case, since only the list creator can edit his/her list. 2 questions: 1) is there any parameter that I can specify in the URLconf to add this restrictions? 2) can I impose the those generic views should only work with POST and not

How to test if a view is decorated with “login_required” (Django)

淺唱寂寞╮ 提交于 2019-12-07 09:19:56
问题 I'm doing some (isolated) unit test for a view which is decorated with "login_required". Example: @login_required def my_view(request): return HttpResponse('test') Is it possible to test that the "my_view" function is decorated with "login_required"? I know I can test the behaviour (anonymous user is redirected to login page) with an integration test (using the test client) but I'd like to do it with an isolated test. Any idea? Thanks! 回答1: Sure, it must be possible to test it in some way. It

Django: login_required on ajax call

梦想与她 提交于 2019-12-05 20:11:35
问题 I am trying to authenticate user on ajax post but doesn't work. Here what I have done settings.py LOGIN_URL = '/accounts/login/' LOGIN_REDIRECT_URL = '/' Template <script> $('.btn-request').click(function(){ var button = this; $.ajax({ type: "POST", url: "{% url 'like' %}", data: {'tutorial_id': $(this).attr('name'), 'csrfmiddlewaretoken': '{{csrf_token}}'}, dataType: "json", success: function(json) { toastr.success(json.message); }, error: function(rs, e) { alert(rs.responseText); } }); }) <