简体   繁体   English

如何在所有按钮都位于单个base.html文件中的Django应用程序中停用按钮?

[英]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.

相关问题 在Django中,如何在浏览器上呈现static / base.html文件? - In Django, how to render static/base.html file on browser? 在哪里放置我的 base.html 文件? - Where to place my base.html file? Django-如何在base.html中传递上下文? - Django - how to pass context in base.html? 在Django中,放置包含所有应用程序使用的CDN链接的base.html的正确位置在哪里? - In Django, where is the correct place to put a base.html that includes CDN links used by all apps? 如何在 html 文件中加载 css 文件已经在 Z2B9AFB89A36ACC15ADZ5B159 中扩展了另一个 html 文件(base.html) - how to load css file in html file already extends another html file (base.html) in django Django TemplateDoesNotExist {%extended base.html%}-模板应该在哪里? - Django TemplateDoesNotExist {% extends base.html %} - where should template be? 如何为 base.html: Django 框架添加样式? - How to add style to base.html: Django framework? Django 中调用 base.html 的函数是什么,如何在运行时查看 base.html 中的 python 代码执行 - What is the function in Django that calls base.html and how can I see python code inside base.html execute at runtime 自定义Django报告构建器base.html - Customize Django report builder base.html 如何在HTML和Django中以单一形式使用2个提交按钮? - How can I use 2 submit buttons in a single form in HTML & Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM