Reflection of HTML elements - django
I am trying to give an administrator of the webservice an ability to name buttons of the main menu using whathever text he or she wants. Moreover, I want to allow him to play with styles that particular button will be described with.
Each of written above action should be available from the administrator's menu ( in this case it is a default django admin framework ).
I've came up with an idea of using completely new model:
class MainSiteButton(models.Model):
button_title = models.CharField(
max_length=100,
null=True,
blank=False,
default="Click to edit",
verbose_name="Text of the button"
)
button_style = models.CharField(
max_length=10000,
null=True,
blank=False,
default="font-size: 10px; font-color: green;",
verbose_name="Style of the button"
)
button_default_name = models.CharField(
choices=button_default_name_choices,
max_length=500,
null=True,
blank=False,
default=next(iter(button_default_name_choices))[0],
verbose_name="Button back-end identifier"
)
Using given model, I am going to define each button, that is defined in the webpage's menu.
These will have such a form:
queried_home_button = MainSiteButton.objects.create(
button_title="Home",
button_default_name='home_button'
)
queried_logout_button = MainSiteButton.objects.create(
button_title="Logout",
button_default_name='logout_button'
)
queried_login_button = MainSiteButton.objects.create(
button_title="Login",
button_default_name='login_button'
)
Operation of defining new buttons will occur only once, at a start of the server
Another step is to define a lazy context processor:
@memoize
def texts_of_buttons(request):
context_data = dict()
try:
queried_home_button = MainSiteButton.objects.filter(
button_default_name='home_button'
)[0]
queried_logout_button = MainSiteButton.objects.filter(
button_default_name='logout_button'
)[0]
queried_login_button = MainSiteButton.objects.filter(
button_default_name='login_button'
)[0]
context_data['home_button'] = queried_home_button
context_data['logout_button'] = queried_logout_button
context_data['login_button'] = queried_login_button
except Exception as e:
logger.error(
'Problem: exception occured in MainSubPageController'
+ 'nError: ' + str(texts_of_buttons)
)
return context_data
To finally use it in the base template:
<a class="item {{ home_button.button_style|safe }}" href="{% url 'home' %}">{{ home_button.button_title }}</a>
<a class="item {{ queried_login_button.button_style|safe }}" href="{% url 'login' %}">{{ queried_login_button.button_title }}</a>
<a class="item {{ queried_logout_button.button_style|safe }}" href="{% url 'logout' %}">{{ queried_logout_button.button_title }}</a>
The question arises whether there exist better solution ( which means faster, that requires fewer memory resources
django django-templates lazy-loading
add a comment |
I am trying to give an administrator of the webservice an ability to name buttons of the main menu using whathever text he or she wants. Moreover, I want to allow him to play with styles that particular button will be described with.
Each of written above action should be available from the administrator's menu ( in this case it is a default django admin framework ).
I've came up with an idea of using completely new model:
class MainSiteButton(models.Model):
button_title = models.CharField(
max_length=100,
null=True,
blank=False,
default="Click to edit",
verbose_name="Text of the button"
)
button_style = models.CharField(
max_length=10000,
null=True,
blank=False,
default="font-size: 10px; font-color: green;",
verbose_name="Style of the button"
)
button_default_name = models.CharField(
choices=button_default_name_choices,
max_length=500,
null=True,
blank=False,
default=next(iter(button_default_name_choices))[0],
verbose_name="Button back-end identifier"
)
Using given model, I am going to define each button, that is defined in the webpage's menu.
These will have such a form:
queried_home_button = MainSiteButton.objects.create(
button_title="Home",
button_default_name='home_button'
)
queried_logout_button = MainSiteButton.objects.create(
button_title="Logout",
button_default_name='logout_button'
)
queried_login_button = MainSiteButton.objects.create(
button_title="Login",
button_default_name='login_button'
)
Operation of defining new buttons will occur only once, at a start of the server
Another step is to define a lazy context processor:
@memoize
def texts_of_buttons(request):
context_data = dict()
try:
queried_home_button = MainSiteButton.objects.filter(
button_default_name='home_button'
)[0]
queried_logout_button = MainSiteButton.objects.filter(
button_default_name='logout_button'
)[0]
queried_login_button = MainSiteButton.objects.filter(
button_default_name='login_button'
)[0]
context_data['home_button'] = queried_home_button
context_data['logout_button'] = queried_logout_button
context_data['login_button'] = queried_login_button
except Exception as e:
logger.error(
'Problem: exception occured in MainSubPageController'
+ 'nError: ' + str(texts_of_buttons)
)
return context_data
To finally use it in the base template:
<a class="item {{ home_button.button_style|safe }}" href="{% url 'home' %}">{{ home_button.button_title }}</a>
<a class="item {{ queried_login_button.button_style|safe }}" href="{% url 'login' %}">{{ queried_login_button.button_title }}</a>
<a class="item {{ queried_logout_button.button_style|safe }}" href="{% url 'logout' %}">{{ queried_logout_button.button_title }}</a>
The question arises whether there exist better solution ( which means faster, that requires fewer memory resources
django django-templates lazy-loading
This is actually not lazy. Since if you "index" a queryset (likeqs[1]) you perform evaluation.
– Willem Van Onsem
Dec 18 '18 at 19:19
that's correct, but I simply thought that using 'memoize' decorator along with function defined here: rock-it.pl/how-to-make-your-django-context-processors-lazy the result of this context processor is stored in the memory. Thanks for making it clear
– D Komo
Dec 18 '18 at 19:23
add a comment |
I am trying to give an administrator of the webservice an ability to name buttons of the main menu using whathever text he or she wants. Moreover, I want to allow him to play with styles that particular button will be described with.
Each of written above action should be available from the administrator's menu ( in this case it is a default django admin framework ).
I've came up with an idea of using completely new model:
class MainSiteButton(models.Model):
button_title = models.CharField(
max_length=100,
null=True,
blank=False,
default="Click to edit",
verbose_name="Text of the button"
)
button_style = models.CharField(
max_length=10000,
null=True,
blank=False,
default="font-size: 10px; font-color: green;",
verbose_name="Style of the button"
)
button_default_name = models.CharField(
choices=button_default_name_choices,
max_length=500,
null=True,
blank=False,
default=next(iter(button_default_name_choices))[0],
verbose_name="Button back-end identifier"
)
Using given model, I am going to define each button, that is defined in the webpage's menu.
These will have such a form:
queried_home_button = MainSiteButton.objects.create(
button_title="Home",
button_default_name='home_button'
)
queried_logout_button = MainSiteButton.objects.create(
button_title="Logout",
button_default_name='logout_button'
)
queried_login_button = MainSiteButton.objects.create(
button_title="Login",
button_default_name='login_button'
)
Operation of defining new buttons will occur only once, at a start of the server
Another step is to define a lazy context processor:
@memoize
def texts_of_buttons(request):
context_data = dict()
try:
queried_home_button = MainSiteButton.objects.filter(
button_default_name='home_button'
)[0]
queried_logout_button = MainSiteButton.objects.filter(
button_default_name='logout_button'
)[0]
queried_login_button = MainSiteButton.objects.filter(
button_default_name='login_button'
)[0]
context_data['home_button'] = queried_home_button
context_data['logout_button'] = queried_logout_button
context_data['login_button'] = queried_login_button
except Exception as e:
logger.error(
'Problem: exception occured in MainSubPageController'
+ 'nError: ' + str(texts_of_buttons)
)
return context_data
To finally use it in the base template:
<a class="item {{ home_button.button_style|safe }}" href="{% url 'home' %}">{{ home_button.button_title }}</a>
<a class="item {{ queried_login_button.button_style|safe }}" href="{% url 'login' %}">{{ queried_login_button.button_title }}</a>
<a class="item {{ queried_logout_button.button_style|safe }}" href="{% url 'logout' %}">{{ queried_logout_button.button_title }}</a>
The question arises whether there exist better solution ( which means faster, that requires fewer memory resources
django django-templates lazy-loading
I am trying to give an administrator of the webservice an ability to name buttons of the main menu using whathever text he or she wants. Moreover, I want to allow him to play with styles that particular button will be described with.
Each of written above action should be available from the administrator's menu ( in this case it is a default django admin framework ).
I've came up with an idea of using completely new model:
class MainSiteButton(models.Model):
button_title = models.CharField(
max_length=100,
null=True,
blank=False,
default="Click to edit",
verbose_name="Text of the button"
)
button_style = models.CharField(
max_length=10000,
null=True,
blank=False,
default="font-size: 10px; font-color: green;",
verbose_name="Style of the button"
)
button_default_name = models.CharField(
choices=button_default_name_choices,
max_length=500,
null=True,
blank=False,
default=next(iter(button_default_name_choices))[0],
verbose_name="Button back-end identifier"
)
Using given model, I am going to define each button, that is defined in the webpage's menu.
These will have such a form:
queried_home_button = MainSiteButton.objects.create(
button_title="Home",
button_default_name='home_button'
)
queried_logout_button = MainSiteButton.objects.create(
button_title="Logout",
button_default_name='logout_button'
)
queried_login_button = MainSiteButton.objects.create(
button_title="Login",
button_default_name='login_button'
)
Operation of defining new buttons will occur only once, at a start of the server
Another step is to define a lazy context processor:
@memoize
def texts_of_buttons(request):
context_data = dict()
try:
queried_home_button = MainSiteButton.objects.filter(
button_default_name='home_button'
)[0]
queried_logout_button = MainSiteButton.objects.filter(
button_default_name='logout_button'
)[0]
queried_login_button = MainSiteButton.objects.filter(
button_default_name='login_button'
)[0]
context_data['home_button'] = queried_home_button
context_data['logout_button'] = queried_logout_button
context_data['login_button'] = queried_login_button
except Exception as e:
logger.error(
'Problem: exception occured in MainSubPageController'
+ 'nError: ' + str(texts_of_buttons)
)
return context_data
To finally use it in the base template:
<a class="item {{ home_button.button_style|safe }}" href="{% url 'home' %}">{{ home_button.button_title }}</a>
<a class="item {{ queried_login_button.button_style|safe }}" href="{% url 'login' %}">{{ queried_login_button.button_title }}</a>
<a class="item {{ queried_logout_button.button_style|safe }}" href="{% url 'logout' %}">{{ queried_logout_button.button_title }}</a>
The question arises whether there exist better solution ( which means faster, that requires fewer memory resources
django django-templates lazy-loading
django django-templates lazy-loading
edited Dec 31 '18 at 11:56
Bhargav Rao♦
30.3k2090111
30.3k2090111
asked Dec 18 '18 at 19:17
D KomoD Komo
467
467
This is actually not lazy. Since if you "index" a queryset (likeqs[1]) you perform evaluation.
– Willem Van Onsem
Dec 18 '18 at 19:19
that's correct, but I simply thought that using 'memoize' decorator along with function defined here: rock-it.pl/how-to-make-your-django-context-processors-lazy the result of this context processor is stored in the memory. Thanks for making it clear
– D Komo
Dec 18 '18 at 19:23
add a comment |
This is actually not lazy. Since if you "index" a queryset (likeqs[1]) you perform evaluation.
– Willem Van Onsem
Dec 18 '18 at 19:19
that's correct, but I simply thought that using 'memoize' decorator along with function defined here: rock-it.pl/how-to-make-your-django-context-processors-lazy the result of this context processor is stored in the memory. Thanks for making it clear
– D Komo
Dec 18 '18 at 19:23
This is actually not lazy. Since if you "index" a queryset (like
qs[1]) you perform evaluation.– Willem Van Onsem
Dec 18 '18 at 19:19
This is actually not lazy. Since if you "index" a queryset (like
qs[1]) you perform evaluation.– Willem Van Onsem
Dec 18 '18 at 19:19
that's correct, but I simply thought that using 'memoize' decorator along with function defined here: rock-it.pl/how-to-make-your-django-context-processors-lazy the result of this context processor is stored in the memory. Thanks for making it clear
– D Komo
Dec 18 '18 at 19:23
that's correct, but I simply thought that using 'memoize' decorator along with function defined here: rock-it.pl/how-to-make-your-django-context-processors-lazy the result of this context processor is stored in the memory. Thanks for making it clear
– D Komo
Dec 18 '18 at 19:23
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53839678%2freflection-of-html-elements-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53839678%2freflection-of-html-elements-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is actually not lazy. Since if you "index" a queryset (like
qs[1]) you perform evaluation.– Willem Van Onsem
Dec 18 '18 at 19:19
that's correct, but I simply thought that using 'memoize' decorator along with function defined here: rock-it.pl/how-to-make-your-django-context-processors-lazy the result of this context processor is stored in the memory. Thanks for making it clear
– D Komo
Dec 18 '18 at 19:23