0

So i am building a personal website and am using Materialize for web designing. At first it was good and my pages look good, but then I added some new pages and i found that the css is not applied in these new pages, can anybody help me in solving this.

Actually both are same pages(categories.html) but the path are different.

header.html (main html file, have not changed the name yet)

<<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <!--Import Google Icon Font-->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    
    <!--Import materialize.css-->
    <link type="text/css" rel="stylesheet" href="static/main/materialize.css"  media="screen,projection"/>
    
   
  </head>  

  <body>

    {% load static %}

    {% include "main/includes/navbar.html" %}

    {% include "main/includes/messages.html" %}
    
    <main>
      <div style="background-image: url('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwallpapersafari.com%2Fw%2FcU6JWo&psig=AOvVaw1eBAulQvnXOrIK1yQueVX5&ust=1623841457736000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPje9_--mfECFQAAAAAdAAAAABAD')">
        <div class = "container">
          <br>
          {% block content %}
          {% endblock %}
        </div>
      </div>  
    </main>

    {% include "main/includes/footer.html" %}

    <script type="text/javascript" src="static/main/materialize.js"></script>
  </body>
</html>

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Item, ItemSeries, ItemCategory
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, logout, authenticate
from django.contrib import messages
from .forms import NewUserForm


def single_slug(request, single_slug):
    categories = [c.category_slug for c in ItemCategory.objects.all()]
    if single_slug in categories:
        matching_series = ItemSeries.objects.filter(item_category__category_slug=single_slug)
        
        series_urls = {}
        for m in matching_series.all():
            part_one = Item.objects.filter(item_series__item_series=m.item_series).earliest("item_update")
            series_urls[m] = part_one.item_slug

        return render(request,
                      "main/category.html",
                      {"part_ones": series_urls})   


    items = [c.item_slug for c in Item.objects.all()]
    if single_slug in items:
        this_item = Item.objects.get(item_slug=single_slug)

        return render(request,
                      "main/item.html",
                      {"item":this_item})   

    return HttpResponse(f"{single_slug} does not correspond to anything.")

def product(request):
    return render(request = request,
                  template_name = "main/categories.html",
                  context ={"categories": ItemCategory.objects.all})

def homepage(request):
    return render(request = request,
                  template_name = "main/categories.html",
                  context ={"categories": ItemCategory.objects.all})    

def about(request):
    return render(request = request,
                  template_name = "main/about.html",
                  context ={"categories": ItemCategory.objects.all})    

def contact(request):
    return render(request = request,
                  template_name = "main/contact.html",
                  context ={"categories": ItemCategory.objects.all})    

def register(request):
    if request.method == "POST":
        form = NewUserForm(request.POST)
        if form.is_valid():
            user = form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f"New Account Created: {username}")
            login(request, user)
            messages.info(request, f"You are now logged in as: {username}")
            return redirect("main:homepage")
        else:
            for msg in form.error_messages:
                messages.error(request, f"{msg}: {form.error_messages[msg]}")   

    form = NewUserForm
    return render(request,
                  "main/register.html",
                  context = {"form": form})

def logout_request(request):
    logout(request)
    messages.info(request, "Logged out successfully!")
    return redirect("main:homepage")

def login_request(request):
    if request.method == "POST":
        form = AuthenticationForm(request, data = request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username = username, password = password)
            if user is not None:
                login(request, user)
                messages.info(request, f"You are now logged in as: {username}")
                return redirect("main:homepage")
            else:
                messages.error(request, "Invalid username or password") 
        else:
            messages.error(request, "Invalid username or password")     
    form = AuthenticationForm()
    return render(request, 
                  "main/login.html",
                  {"form": form})

navbar.html

  {% load static %}

  
  <!-- Dropdown Structure -->
  <ul id='dropdown1' class='dropdown-content'>
    <li><a href="#!">one</a></li>
    <li><a href="#!">two</a></li>
    <li class="divider" tabindex="-1"></li>
    <li><a href="#!">three</a></li>
    <li><a href="#!"><i class="material-icons">view_module</i>four</a></li>
    <li><a href="#!"><i class="material-icons">cloud</i>five</a></li>
  </ul>
        

<nav>
  <div id="nav-wrapper">

    <div>
      <ul class="center hide-on-med-and-down">
        <li>
          <a href="">Home</a>
        </li>
        <li>
          <a href="about/">About Us</a>
        </li>
        <li>
          <a href="product/">Products</a>
        </li>
        <li>
          <a href="services/">Services</a>
        </li>
        <li>
          <a href="contact/">Contact</a>
        </li>
        <li>
          <a class='dropdown-trigger btn' href='#' data-target='dropdown1'>Drop Me!</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

categories.html

{% extends "main/header.html" %}

{% block content %}

  <div class="row">
        {% for cat in categories %}
            <div class="col s12 m6 l4">
              <a href="{{cat.category_slug}}", style="color:#000">
                <div class="card hoverable">
                    <div class="card-content">
                        <div class="card-title">{{cat.item_category}}</div>
                        <p>{{cat.category_summary}}</p>
                    </div>  
                </div>
              </a>  
            </div>
        {% endfor %}    
  </div>

{% endblock %}

When CSS worked This is home page

When CSS did not work This is the same homepage after clicking 'about us' link form navbar

I am stuck and don't know what to do next, any help would be appretiated.

Umang Vira
  • 23
  • 4
  • Does this answer your question? [Why would a developer place a forward slash at the start of each relative path?](https://stackoverflow.com/questions/7613274/why-would-a-developer-place-a-forward-slash-at-the-start-of-each-relative-path) – Abdul Aziz Barkat Jul 20 '21 at 06:21
  • Have you tried force reload the page? and restarted the server? https://www.getfilecloud.com/blog/2015/03/tech-tip-how-to-do-hard-refresh-in-browsers/#.YPZupRMza3I – AnonymousUser Jul 20 '21 at 06:34
  • Yes, adding a forward slash worked, thank you – Umang Vira Jul 20 '21 at 07:00

3 Answers3

0

According to the comment above, i changed my header.html,

<<!DOCTYPE html> <html>   <head>    <meta charset="utf-8">
    <!--Import Google Icon Font-->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    
    <!--Import materialize.css-->
    <link type="text/css" rel="stylesheet" href="/static/main/materialize.css"  media="screen,projection"/>
    
      </head>  

  <body>

    {% load static %}

    {% include "main/includes/navbar.html" %}

    {% include "main/includes/messages.html" %}
    
    <main>
      <div style="background-image: url('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwallpapersafari.com%2Fw%2FcU6JWo&psig=AOvVaw1eBAulQvnXOrIK1yQueVX5&ust=1623841457736000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPje9_--mfECFQAAAAAdAAAAABAD')">
        <div class = "container">
          <br>
          {% block content %}
          {% endblock %}
        </div>
      </div>  
    </main>

    {% include "main/includes/footer.html" %}

    <script type="text/javascript" src="/static/main/materialize.js"></script>   </body> </html>

notice the href of css and js link tag.

Umang Vira
  • 23
  • 4
0

There are a few things that I have seen here that don't look correct. Number 1, Are you inheriting your template from your base?

{% extends 'base.html' %}

This is where you should call your materialize.css (in the base template)

  1. Load static is in the wrong place, you are calling static after the link to the css, load static should be at the very top of the template:

    {% extends 'base.html' %} {% load static %}

You have an extra opening chevron in your DOCTYPE.

  1. Put materialize js in you base template before closing body tag. Django by default inherits the default base template so this is what you could be missing.

  2. In case I have misunderstood, and you are only wanting to call materialize.css for that header template you can use tags after the call for load static

    {% block extra_css %}<link rel="stylesheet" href="{% static 'main/materialize.css' %}"> {% endblock extra_css %}

0

This might help:

{% load static %} above doctype tag!

{% load static %}
<!DOCTYPE html> 
<html>
   <head>
      <meta charset="utf-8">
  <!--Import Google Icon Font-->
      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <!--Import materialize.css-->
      <link type="text/css" rel="stylesheet" href="{% static 'main/materialize.css' %}"  media="screen,projection"/>
   </head>
   <body>
  {% include "main/includes/navbar.html" %}
  {% include "main/includes/messages.html" %}
      <main>
         <div style="background-image: url('https://www.google.com/url?sa=i&url=https%3A%2F%2Fwallpapersafari.com%2Fw%2FcU6JWo&psig=AOvVaw1eBAulQvnXOrIK1yQueVX5&ust=1623841457736000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPje9_--mfECFQAAAAAdAAAAABAD')">
            <div class = "container">
               <br>
           {% block content %}
           {% endblock %}
            </div>
         </div>
      </main>
  {% include "main/includes/footer.html" %}
      <script type="text/javascript" src="{% static 'main/materialize.js' %}"></script>   
   </body>
</html>

In every html page {% load static %} below {% block content %} and above the html of the page!

Static files path: {% static 'app name/filefoldername' %}

Hope you understood! Look for some tutorials on youtube also!

Novfensec
  • 106
  • 1
  • 4