简体   繁体   中英

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!

The html code for the buttons are all located in a 'base.html' file; there are other html files that are extensions to that file that represent each button. 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?

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

{% 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. 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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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