简体   繁体   English

Django - login_required 不重定向

[英]Django - login_required not redirecting

I'm trying to make a view restricted unless login is done.除非完成登录,否则我试图限制视图。 Was following tuts+plus django unchained tutorial正在关注 tuts+plus django unchained 教程

However login_required decorator somehow is not doing the job.但是 login_required 装饰器不知何故没有完成这项工作。 What am I doing wrong?我究竟做错了什么?

that part at views.py (login_required has been imported) views.py 中的那部分(已导入 login_required)

@login_required
def story(request):
    if request.method == "POST":
        form = StoryForm(request.POST)
        if form.is_valid():
                form.save()
                return HttpResponseRedirect('/')
    else:
        form = StoryForm()
    return render(request, 'stories/story.html', {'form': form})

no changes has been brought in urls.py. urls.py 中没有进行任何更改。 So I am hoping for an error where it can't find /login.所以我希望在找不到 /login 的地方出现错误。 But that somehow is not happening.但不知何故,这并没有发生。

settings.py has LOGIN_URL = "/login/" settings.py 有 LOGIN_URL = "/login/"

I'm not sure why this is happening.我不确定为什么会这样。 But for some reason in chrome the default story view is opening without redirecting to /login whereas it is working fine in Firefox.但是由于某种原因,在 chrome 中,默认的故事视图打开时没有重定向到 /login,而在 Firefox 中它运行良好。 I tried clearing the cached images & files in Chrome, but it didn't help.我尝试清除 Chrome 中缓存的图像和文件,但没有帮助。

For now I'll just use Firefox to get through it.现在我将只使用 Firefox 来完成它。

Add an entry in urls.py like this:urls.py添加一个条目,如下所示:

(r'^login/$', 'login_view'),

Or you can use remove LOGIN_URL = "/login/" from your settings.py and use:或者你可以使用删除LOGIN_URL = "/login/"从你的settings.py及用途:

@login_required(login_url='/login/')

in both case you must add entry to urls.py在这两种情况下,您都必须向 urls.py 添加条目

I had the same issue.我遇到过同样的问题。 I cleared Chrome chache but it didn't work .我清除了 Chrome chache 但它没有用。 Now i am running it on Firefox and its running correctly现在我在 Firefox 上运行它并且它运行正常

Make sure your chrome browser has cleared all of the browsing data, mind that time range choice all time, and then you will solve this problem.确保您的 chrome 浏览器已清除所有浏览数据,始终注意时间范围选择,然后您将解决此问题。 I alse encountered this issue, after using Firefox to work well, I got it.我也遇到过这个问题,用火狐运行正常后,我明白了。

通过 chrome DevTool -> Application -> Storage -> Cookies 清除页面 cookie 可以解决这个问题。

或者,您可以通过在 Google Chrome 的Incognito Window打开您的应用程序来修复此错误。

I had the same problem and I realized the browser identifies me as a logged-in user because I had logged in before as administrator in the admin page.我遇到了同样的问题,我意识到浏览器将我识别为登录用户,因为我之前在管理页面中以管理员身份登录。 I logged out of the admin page and the login_required decorator worked correctly.我退出了管理页面,并且 login_required 装饰器工作正常。

I was able to resolve this by using the logout() function imported the same way as login() and that essentially "cleared" any existence of my already logged in user.我能够通过使用 logout() function 以与 login() 相同的方式导入来解决这个问题,这基本上“清除”了我已经登录的用户的任何存在。

Make a logout view function/url route, run the app and call the url, then you will no longer be able to access the other view functions with the decorator unless logged in once again.创建注销视图函数/url 路由,运行应用程序并调用 url,然后您将无法再使用装饰器访问其他视图函数,除非再次登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM