[英]In Django, how to render static/base.html file on browser?
[英]How to deactivate buttons in Django applications where all buttons are localized in a single base.html file?
在Django网站上,人们在该网站上创建帐户并登录帐户以创建配置文件,我试图在用户已经登录后停用“登录”按钮。两次都不要登录!
按钮的html代码均位于“ base.html”文件中; 还有其他html文件,它们是对该文件的扩展,表示每个按钮。 一旦用户已经登录,应在登录视图的停用中进行该操作,但是如何进行编码? html代码在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>
扩展base.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 %}
与此相关的网址是:
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'),
涉及的意见有:
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)
只需使用
{% if user.is_authenticated %}
<a><button disabled>Login</button> </a>
{% else %}
<a href = "/accounts/login/"><button>Login</button> </a>
{% endif %}
在您的base.html
。 即使存在派生模板,登录状态也将对它们可用,并且将按预期工作。
{% 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 %}
完全隐藏它,以我的拙见,比让它可见和禁用更好:)
顺便说一句。 您粘贴的代码缺少结束</a>
标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.