[英]How to deactivate buttons in Django applications where all buttons are localized in a single base.html file?
On a Django website, where people create accounts and log into accounts to create profiles, I'm attempting to deactivate the Login Button after a user has already logged in. No logging in twice! 在Django网站上,人们在该网站上创建帐户并登录帐户以创建配置文件,我试图在用户已经登录后停用“登录”按钮。两次都不要登录!
The html code for the buttons are all located in a 'base.html' file; 按钮的html代码均位于“ base.html”文件中; there are other html files that are extensions to that file that represent each button.
还有其他html文件,它们是对该文件的扩展,表示每个按钮。 The deactivation should take place in the from the login view once the user has already logged in but how to code that?
一旦用户已经登录,应在登录视图的停用中进行该操作,但是如何进行编码? The html code is in the base.html not the view?
html代码在base.html中而不是视图中?
Also, after initial registration, the Account Registration Button will also need to be deactivated for that user in all future logins. 同样,在初始注册后,还需要在以后的所有登录中为该用户禁用“帐户注册”按钮。 So I need to understand the general idea of how too do this!
因此,我需要了解执行此操作的基本思路!
base.html base.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}OpnePE Base Template{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% static "assets/css/default.css" %}">
</head>
<body>
<div id="page">
<div id="sidebar">
{% block sidebar %}
<p>
<a href = "/accounts/login/"><button>Login</button>
<a href = "/accounts/register/"><button>Account Registrration</button>
<a href = "/accounts/profile/"><button>User Profile</button>
<a href = "/admin/"> <button>Site Administration</button>
</a>
</p>
{% endblock %}
</div>
<div id="content">
{% block content %}This is the content area{% endblock %}
<img src="{% static "assets/images/MedicalSymbol.png" %}" width="120"/>
</div>
</div>
</body>
</html>
The login button that extends the base.html is here. 扩展base.html的登录按钮在这里。 Login.html/
Login.html /
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p class="error">Sorry, that's not a valid user name or password</p>
{% endif %}
<h3>Provider Enrollment Login</h3>
<form action=" /accounts/auth/" method="post">{% csrf_token %}
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
</form>
{% endblock %}
The urls associated with this are : 与此相关的网址是:
url(r'^accounts/login/$', 'openPE.views.login'),
url(r'^accounts/auth/$', 'openPE.views.auth_view'),
url(r'^accounts/logout/$', 'openPE.views.logout'),
url(r'^accounts/loggedin/$', 'openPE.views.loggedin'),
url(r'^accounts/invalid/$', 'openPE.views.invalid_login'),
url(r'^accounts/register/$', 'openPE.views.register_user'),
url(r'^accounts/register_success/$', 'openPE.views.register_success'),
The views involved are : 涉及的意见有:
def login(request):
'''
Pushes a dictionary object into it a csrf object and passes that to login.html.
'''
c = {}
c.update(csrf(request))
return render_to_response('login.html', c)
def register_user(request):
'''Provides form for processes the registration form.'''
if request.method == 'POST':
form = MyRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
args = {}
args.update(csrf(request))
args['form'] = MyRegistrationForm()
return render_to_response('register.html', args)
Just use 只需使用
{% if user.is_authenticated %}
<a><button disabled>Login</button> </a>
{% else %}
<a href = "/accounts/login/"><button>Login</button> </a>
{% endif %}
in your base.html
. 在您的
base.html
。 Even if there are derived templates, the logged in status will be available to them and this would work as expected. 即使存在派生模板,登录状态也将对它们可用,并且将按预期工作。
{% block sidebar %}
<p>
{% if user.is_authenticated %}
<a href = "/accounts/login/"><button>Login</button></a>
<a href = "/accounts/profile/"><button>User Profile</button></a>
<a href = "/admin/"> <button>Site Administration</button></a>
{% else %}
<a href = "/accounts/register/"><button>Account Registrration</button></a>
{% endif %}
</p>
{% endblock %}
To hide it completely, which in my humble opinion is better than having it be visible and disabled :) 完全隐藏它,以我的拙见,比让它可见和禁用更好:)
BTW. 顺便说一句。 your code as pasted is missing the closing
</a>
tags 您粘贴的代码缺少结束
</a>
标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.