Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个网站,有一个提交表单,但必须为此登录。如果用户没有登录,它会将其重定向到登录页面。这一切都很好。我的问题是,如果用户未登录并尝试提交表单,我该怎么做才能显示错误消息“您必须登录才能提交。”,但如果没有,则必须显示正常的登录页面。谢谢你。
您可能想使用装饰器@login_required:
@login_required
from django.contrib.auth.decorators import login_required @login_required(login_url='/your/login/url') def your_view(request): # your view code here
希望这可以帮助。