Views Variables According to Page

will be Grateful for any lead to carry on my road into Django.

Trying to change the stylesheet page according to a variable in the views.

Only Works for home page, and other pages didnt work.

any clues? Thanks in Advance.

Head html Page:

<meta charset="UTF-8" />
{% if page_styl == 'home-page' %}
	<link rel="stylesheet" href="{% static 'css/layout.css' %}" />
{% else %}
	<link rel="stylesheet" href="{% static 'css/{{ page_styl }}.css' %}" />
{% endif %}
<title>A.M.W Store | {{ page }}</title>

Views Example:

from django.shortcuts import render
def index(request):
    context = {
        'page': 'Contact Us',
        'page_styl': 'contact'
    }
    return render(request, 'contact/contact.html', context)

What’s happening when it doesn’t work? What’s the html it’s outputting and are you getting errors?

Maybe your contact.html page could extend a base.html base and you could set the style in there in a {% block styles %}.

https://docs.djangoproject.com/en/3.1/ref/templates/language/#templates

You can’t embed a variable tag within another tag. You need to use the add filter to concatenate the components of that path. You can do something like:
<link rel="stylesheet" href="{% static 'css/'|add:page_styl|add:'.css' %}" />

Also, I would suggest getting rid of the if tag by setting page_styl to “layout” in the context for the home page and remove the condition. (Or changing the name of the file from “layout.css” to “home-page.css”.)

Finally, in the future, it would be more helpful when requesting assistance to provide more details as to what error message(s) you are receiving or what’s not working when you try it.
See: How do I ask a good question? - Help Center - Stack Overflow for some ideas as to what information to include in your posts.

it render the HTML page without Any style.

Thanks a lot for support Ken.
i will be more careful next time to provide more details of course.

  • There is no Error Message just the HTML didn’t read style sheet, and render without style.

  • about If Condition: if home page i need to load the layout.css only i will set the home page style inside and if another page i will set each page style in a separate file, about us will be about.css - contact us will be contact.css, so i don’t kill me files from huge styles.

  • about concatenate variables i will dig after it,
    thanks a lot again for support,

Update:

the Concatenate for Variables Work, So Amazing.