I'm trying to make a view restricted unless login is done. Was following tuts+plus django unchained tutorial
However login_required decorator somehow is not doing the job. What am I doing wrong?
that part at views.py (login_required has been imported)
@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. So I am hoping for an error where it can't find /login. But that somehow is not happening.
settings.py has LOGIN_URL = "/login/"
8条答案
按热度按时间7bsow1i61#
我不知道为什么会发生这种情况。但是由于某种原因,在chrome中默认的故事视图打开时没有重定向到/login,而在Firefox中它工作正常。我尝试清除Chrome中缓存的图像和文件,但没有帮助。
现在我只使用Firefox来浏览它。
vddsk6oq2#
我也遇到了同样的问题。我清除了Chrome chache,但它没有工作。现在我在Firefox上运行它,它运行正常
tnkciper3#
通过chrome DevTool -〉应用程序-〉存储-〉Cookie可以修复此问题。
hmae6n7t4#
在
urls.py
中添加一个条目,如下所示:或者,您可以使用remove
LOGIN_URL = "/login/"
从您的settings.py
中移除LOGIN_URL = "/login/"
并用途:在这两种情况下,都必须向www.example.com添加条目urls.py
j8yoct9x5#
确保你的chrome浏览器已经清除了所有的浏览数据,时刻注意时间范围的选择,然后你就可以解决这个问题了。我也遇到过这个问题,在使用Firefox工作良好后,我得到了它。
34gzjxbg6#
或者,您可以通过在Google Chrome的
Incognito Window
中打开您的应用程序来修复此错误。aemubtdh7#
我遇到了同样的问题,我意识到浏览器将我识别为登录用户,因为我以前在管理页面中以管理员身份登录过。我退出了管理页面,login_required装饰器工作正常。
0tdrvxhp8#
我可以通过使用logout()函数来解决这个问题,该函数的导入方式与login()相同,它基本上“清除”了我已经登录的用户的任何存在。
创建一个logout view function/url路由,运行应用程序并调用url,然后你将不能再使用装饰器访问其他视图功能,除非你再次登录。