且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django动态css实现:只有一个项目动态变化(背景)。如何?

更新时间:2022-10-21 23:31:09

假设你有背景的黑暗您的代码可能如下所示:



views.py:

  ... 
if i_want_dark_version:
context ['style'] ='dark'
else:
context ['style'] ='light'

template.html:

 < div id =titles-buttonclass ={{style}}> 
...
< / div>

style.css:

 #titles-button.dark h2 a {
background:...;
}
#titles-button.dark h2 a:hover {
background:...;
}

#titles-button.light h2 a {
background:...;
}
#titles-button.light h2 a:hover {
background:...;
}


I would like to set up one single css style which gets the background dynamically from some django variable.

I think the added difficulty is that it is related to a:hover.

The original static style.css code runs like:

.titles-button h2 a {
    margin-left: 4em;
    margin-right: 4em;
    margin-top: 1em;
    margin-bottom: 1em;
    display: block;
    width: 240px;
    height: 80px;
    /* background: transparent url(../logos/djangoVX_logo.png) center left no-repeat; */
    text-indent: -999em;
    border: 1px dashed green;
}
.titles-button h2 a:hover {
    /* background: transparent url(../logos/djangoVX_logo2.png) center left no-repeat; */
}

Note the background is now commented.

I would like to use this style within a DIV and update dynamically the background.

Like:

{% if the_apps_list %}
    {% for apps in the_apps_list %}
    <div class="titles-button"> 
        <h2><a href="/{{ apps.app_name }}/" style="X">{{ apps.app_name }}</a></h2> 
    </div>
    {% endfor %}
{% else %}
    <p>No APPS are available. Internal Error.</p>
{% endif %}

I did my best for "X" but with no results.

With the exception of: - Rewrite the full style in X - Use some javascript fancy stuff (on_mouse_over...) - I have even seen a class: inheritALL

None of this solution fits my idea of using django.

I might get the bull from the wrong side...

..which one is the right side?

cheers F

Let's say you have "dark" and "light" versions of background. Your code may look like:

views.py:

...
if i_want_dark_version:
    context['style']='dark'
else:
    context['style']='light'

template.html:

<div id="titles-button" class="{{ style }}">
    ...
</div>

style.css:

#titles-button.dark h2 a {
    background: ...;
}
#titles-button.dark h2 a:hover {
    background: ...;
}

#titles-button.light h2 a {
    background: ...;
}
#titles-button.light h2 a:hover {
    background: ...;
}