首页 > 解决方案 > 当我在 Django 中使用“扩展”和“阻止”时,我的 CSS 没有出现

问题描述

我花了半天时间试图弄清楚这一点。我最终通过 HTML 样式属性传递了 CSS。我做了什么并尝试过:

  1. settings.py我包括:
   django.contrib.staticfiles
   STATIC_URL = '/static/' 
  1. layout.html中:
   {% load static %}
   <link href="{% static 'auctions/styles.css' %}" rel="stylesheet">
  1. 静态文件夹按以下方式组织:

app_name/static/auctions/styles.css 4. 扩展布局和填充body块

{% extends "auctions/layout.html" %} 
{% load humanize %}
{% load crispy_forms_tags %}

{% block body %}

    {% if listing.closed == True %}
        <div style="background-color: whitesmoke; height: 110%; width: 100%; position: absolute; opacity: 0.6; z-index: 2;">
            <div style="margin-top: 30%; font-size: xxx-large;">
                <center>
                    <strong>
                        This listing is closed. <br>
                        <p style="color: green;">{% if request.user == winner.user %} You ({{ winner.user }}) have Won the auction! Congrats! {% endif %}</p>
                    </strong>
                </center>
            </div>
        </div>
    {% endif %}
    {% if error_check == True %}
        <div class="alert alert-warning" role="alert">
            <center>
                Your bid is lower than the current bid! Try with a higher one.
            </center>
        </div>
    {% elif error_check == False %}
        <div class="alert alert-success" role="alert">
            <center>
                Your bid was successfully placed!
            </center>
        </div>
    {% endif %}
    <div>
        <div>
            {% if button %}
                <form method="POST" action="{% url 'view_list' listing.id %}">
                    {% csrf_token %}
                    <input type="hidden" name="close_list" value="True">
                    <input type="submit" class="btn btn-success" value="Close the Auction">
                </form>
            {% endif %}
        </div>
        <div>
            {% if not watchlisted or watchlisted.watchlist == False %}
                <form method="POST" action="{% url 'view_list' listing.id %}">
                    {% csrf_token %}
                    <input type="hidden" name="watchlist_add" value="True">
                    <input type="submit" class="btn btn-primary" value="Add to Watchlist">
                </form>
            {% else %}
                <form method="POST" action="{% url 'view_list' listing.id %}">
                    {% csrf_token %}
                    <input type="hidden" name="watchlist_add" value="False">
                    <input type="submit" class="btn btn-primary" value="Remove from Watchlist">
                </form>
            {% endif %}
        </div>
        <h3>Listing: {{ listing.title }}</h3>
        <img src="{{ listing.image }}" alt="Listings' Images">
        <p>{{ listing.description }}</p>
        {% if not bid %}
            <strong>${{ listing.price|stringformat:"1.2f" }}</strong>
        {% else %}
            <strong>${{ bid|stringformat:"1.2f" }}</strong>
        {% endif %}
        
        <p> {{ total_bids }} bid(s) so far. {% if bid_user %} {{ bid_user }} {% endif %}</p>
        <form method="POST" name="bidding" action="{% url 'view_list' listing.id %}">
            {% csrf_token %}
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text">$</span>
                </div>   
                <div style="margin: 0; padding: 0 2px; height: 6px; z-index: 1;">
                    {% crispy form %}
                </div>   
                <div class="input-group-append" >
                    <span class="input-group-text">.00</span>
                </div>
                <input type="submit" class="btn btn-primary" value="Place Bid">
            </div>
        </form>
        <div style="width: 222;">
            <form action="{% url 'view_list' listing.id %}" name="comment" method="POST">
                <input type="submit" class="btn btn-secondary btn-sm" value="Comment">
                {% crispy comment %}
                
            </form>
            <div>
                {% for c in comments %}
                    <ul>{{ c.user }}: {{ c.comment }} {{c.created_at}}</ul> 
                {% empty %}
                    No comments yet.
                {% endfor %}
            </div>
        </div>

        <h4>Details</h4>
            <li>Listed by: {{ listing.user }} </li>
            <li>Category: {{ listing.category }} </li>
            <li>Listing created at: {{ listing.created_at }} </li>
    </div>

{% endblock %}

标签: htmlcssdjangodjango-templates

解决方案


问题出在浏览器上,尤其是缓存...


推荐阅读