Django阻止代码无法渲染

时间:2018-07-24 23:01:19

标签: django

正在做一个网站,我需要一些有关块标记的帮助。看来django不会渲染{% block home %} {%endblock home %}。让我们假设在我的代码块中有这个div,并且如果按照下面的示例将div放入我的base.html中,则它不会出现。仅当我将源代码放入base.html中时才会显示,因此不包括我的标签。我该怎么办? PS我在我的块中有扩展代码,例如{% extends "base.html" %}

<div>
    <H1>Aici trebuie adaugat restul</H1>
</div>

我的base.html:

{% load staticfiles %}
<!--DOCTYPE html -->
<html>
<head>
<title>{% block head_title %}Advancing the Blog{% endblock head_title %}</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css' >


<link rel='stylesheet' href='{% static "css/base.css" %}' />
<style>
{% block style %}{% endblock style %}
</style>

{% block head_extra %} {% endblock head_extra %}
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>



{% include "messages_display.html" %}
<div class='container'> 
    <ol class='breadcrumb'>
        <li><a href='{% url "posts:list" %}'>Home</a></li>



        {% block post_detail_link %}
        {% endblock %}
        <!-- {% block index %}
        {% endblock %} -->

        {% if not request.user.is_authenticated %}
        <li class='pull-right'><a href='{% url "register" %}'>Register</a></li>
        <li class='pull-right'><a href='{% url "login" %}'>Login</a></li>
        {% else %}
        <li class='pull-right'><a href='{% url "logout" %}'>Logout</a></li>
        {% endif %}
    </ol>
<div>
    <H1>Aici trebuie adaugat restul</H1>
</div>
{% block home %} {%endblock home %}
{% block content %}{% endblock content %}

</div>


<!-- Latest compiled and minified JavaScript -->
<script   src="http://code.jquery.com/jquery-1.12.2.min.js"   integrity="sha256-lZFHibXzMHo3GGeehn1hudTAP3Sc0uKXBXAzHX1sjtk="   crossorigin="anonymous"></script>


<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<script src='https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js'></script>

<script type="text/javascript">
$(document).ready(function(){
    $(".content-markdown").each(function(){
            var content = $(this).text()
            var markedContent = marked(content)
            $(this).html(markedContent)
    })
    $(".post-detail-item img").each(function(){
            $(this).addClass("img-responsive");
    })



    var contentInput = $("#id_content");

    function setContent(value){
        var markedContent = marked(value)
        $("#preview-content").html(markedContent)
        $("#preview-content img").each(function(){
            $(this).addClass("img-responsive")
        })
    }
    setContent(contentInput.val())

    contentInput.keyup(function(){
        var newContent = $(this).val()
        setContent(newContent)
    })

    var titleInput = $("#id_title");



    function setTitle(value) {
        $("#preview-title").text(value)
    }
    setTitle(titleInput.val())

    titleInput.keyup(function(){
        var newContent = $(this).val()
        setTitle(newContent)
    })

    $(".comment-reply-btn").click(function(event){
        event.preventDefault();
        $(this).parent().next(".comment-reply").fadeToggle();
    })


    // preview-title
    // preview-content

})

</script>

</body>
</html>

comment_thread.html

{% extends "base.html" %}
{% load urlify %}
{% load crispy_forms_tags %}


{% block head_title %}
{{ instance.title }} | {{ block.super }}
{% endblock head_title %}

<H1>TEST </H1>

{% block content %}
{{ object }}



<div class='col-sm-6 col-sm-offset-3'>


          <p>{{ comment.content }}</p>
          <footer>via {{ comment.user }} | {{ comment.timestamp|timesince }} ago | {% if comment.children.count > 0 %}{{ comment.children.count }} Comment{% if comment.children.count > 1 %}s{% endif %} {% endif %} {% if request.user == comment.user %}<a href='{{ comment.get_delete_url }}'>Delete</a> {% endif %}</footer>
          <hr/>
          <div>
              {% for child_comment in comment.children %}
                <blockquote>
                <p>{{ child_comment.content }}</p>
                <footer>via {{ child_comment.user }} | {{ child_comment.timestamp|timesince }} ago | {% if request.user == child_comment.user %}<a href='{{ child_comment.get_delete_url }}'>Delete</a>{% endif %}</footer>
                </blockquote>
             {% endfor %}
             {% if request.user.is_authenticated %}
            <form method="POST" action="."> {% csrf_token %}
                {{ form|crispy }}
                <input type='hidden' name='parent_id' value='{{ comment.id }}'>
                <input type='submit' value='Reply' class='btn btn-default'>
            </form>
            {% else %}
            <p>You must login to comment </p>
            {% endif %}
        </div>



        <hr/>
</div>


{% endblock content %}

confirm_delete.html

{% extends "base.html" %}
{% load urlify %}
{% load crispy_forms_tags %}


{% block head_title %}
{{ instance.title }} | {{ block.super }}
{% endblock head_title %}



{% block content %}

<div class='col-sm-6 col-sm-offset-3'>
  <h1>Confirm Delete</h1>


            <form method="POST" action="."> {% csrf_token %}
                <p>A re you sure you want to delete: "{{ object.content }}"?</p>

                <input type='submit' value='Confirm' class='btn btn-warning'>
                <a href='{{ object.get_absolute_url }}' class='btn btn-default'>Cancel</a>
            </form>
        </div>



        <hr/>
</div>


{% endblock content %}

form.html

{% extends "base.html" %}
{% load crispy_forms_tags %}


{% block content %}
<!-- {% block index_home  %}
{% endblock %} -->
<div class='col-sm-6 col-sm-offset-3'>
<h1>{{ title }}</h1>
<hr/>

<form method='POST' action='' enctype='multipart/form-data'>{% csrf_token %}
{{ form|crispy }}
<input type='submit' class='btn btn-default' value='{{ title }}' />
</form>
</div>
{% endblock content %}

messages_display.html

 {% if messages %}
    <div class='messages'>

    <ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{% if "html_safe" in message.tags %}{{ message|safe }}{% else %}{{ message }}{% endif %}</li>
    {% endfor %}
    </ul>

    </div>
{% endif %}

post_detail.html

{% extends "base.html" %}
{% load urlify %}
{% load crispy_forms_tags %}


{% block head_title %}
{{ instance.title }} | {{ block.super }}
{% endblock head_title %}


 {% block post_detail_link %}
  <li><a href='{{ instance.get_absolute_url }}'>{{ instance.title }}</a></li>
  {% endblock %}



{% block content %}
<div class='col-sm-6 col-sm-offset-3'>
    {% if instance.image %}
    <img src='{{ instance.image.url }}' class='img-responsive' />
    {% endif %}
<h1>{{ title }} <small>{% if instance.draft %}<span style='color:red;'>Draft</span> {% endif %}{{ instance.publish }}</small></h1>

<!-- {% if instance.read_time|time:"i" <= "01" %} < 1 minute {% else %}{{ instance.read_time|time:"i" }} minutes {% endif %} -->


<p>Read time: {% if instance.read_time <= 1 %} < 1 Minute {% else %}{{ instance.read_time }} minutes {% endif %}</p>
{% if instance.user.get_full_name %}
<p>Author: {{ instance.user.get_full_name }}</p>
{% endif %}



<p><div class="fb-like" data-href="{{ request.build_absolute_uri }}" data-layout="button_count" data-action="like" data-show-faces="true" data-share="true"></div>
<hr/>
</p>



<p>
<a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}">
Facebook
</a>

<a href="https://twitter.com/home?status={{ instance.content|truncatechars:80|urlify }}%20{{ request.build_absolute_uri }}">
Twitter
</a>

<a href='https://plus.google.com/share?url={{ request.build_absolute_uri }}'>


<a href="https://www.linkedin.com/shareArticle?mini=true&url={{ request.build_absolute_uri }}&title={{ instance.title }}&summary={{ share_string }}&source={{ request.build_absolute_uri }}">
Linkedin
</a>

<a href="http://www.reddit.com/submit?url={{ request.build_absolute_uri }}&title={{ share_string }}.">Reddit</a>

</p>
<div class='row'>
<div class='col-sm-12 '>

   <div class='post-detail-item'>{{ instance.get_markdown }}</div>

    <hr/>
    <br/>

    <div>

        <p class='lead'>Comments</p>
        {% if request.user.is_authenticated %}
        <form method="POST" action="."> {% csrf_token %}
            {{ comment_form|crispy }}
            <input type='submit' value='Post comment' class='btn btn-default'>
        </form>
        {% else %}
        <p>You must login to comment </p>
        {% endif %}
        <hr/>
        {% for comment in comments %}

        <blockquote>
          <p>{{ comment.content }}</p>
          <footer>via {{ comment.user }} | {{ comment.timestamp|timesince }} ago | {% if comment.children.count > 0 %}{{ comment.children.count }} Comment{% if comment.children.count > 1 %}s{% endif %} | {% endif %} <a class='comment-reply-btn' href='#'>Reply</a> | <a class='' href='{{ comment.get_absolute_url }}'>Thread</a></footer>
          <div class='comment-reply'>
              {% for child_comment in comment.children %}
                <blockquote>
                <p>{{ child_comment.content }}</p>
                <footer>via {{ child_comment.user }} | {{ child_comment.timestamp|timesince }} ago</footer>
                </blockquote>
             {% endfor %}
             {% if request.user.is_authenticated %}
            <form method="POST" action="."> {% csrf_token %}
                {{ comment_form|crispy }}
                <input type='hidden' name='parent_id' value='{{ comment.id }}'>
                <input type='submit' value='Reply' class='btn btn-default'>
            </form>
            {% else %}
        <p>You must login to comment </p>
        {% endif %}
        </div>

        </blockquote>

        <hr/>
        {% endfor %}
    </div>




</div>
</div>
</div>


{% endblock content %}

post_form.html

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block head_extra %} 
{{ form.media }}

{% endblock head_extra %}



{% block content %}
<div class='col-sm-6'>
<h1>Preview</h1>
<hr/>
<div class='content-preview'>
    <h3 id='preview-title'></h3>
    <p id='preview-content'></p>
</div>
</div>
<div class='col-sm-6'>
<h1>Form</h1>
<hr/>

<form method='POST' action='' enctype='multipart/form-data'>{% csrf_token %}
{{ form|crispy }}
<input type='submit' class='btn btn-default' value='Create Post' />
</form>
</div>
{% endblock content %}

post_list.html

{% extends "base.html" %}
{% block content %}


<div class='col-sm-6 col-sm-offset-3'>
    <h1>{{ title }}</h1>
<form method='GET' action='' class='row'>
        <div class='col-sm-6'>
            <div class='input-group'>
                <input class='form-control' type='text' name='q' placeholder='Search posts' value='{{ request.GET.q }}'/>
                <span class='input-group-btn'>
                    <!-- <input class='btn btn-default' type='submit' value='Search' /> -->
                    <button class='btn btn-default' type='submit'>Search <i class="fa fa-search"></i></button>
                </span>
            </div>
        </div>
</form>
{% for obj in object_list %}
<div class="row">
  <div class="col-sm-12">
    <div class="thumbnail">
        {% if obj.image %}
         <img src='{{ obj.image.url }}' class='img-responsive' />
        {% endif %}
      <div class="caption post-detail-item">
        {% if obj.draft %}<h3>Staff only: Draft</h3>{% endif %} {% if obj.publish > today %}<h3>Staff Only: Future Post</h3>{% endif %}
        <h3><a href='{{ obj.get_absolute_url }}'>{{ obj.title }}</a> <small>{{ obj.publish }}</small></h3>
        {% if obj.user.get_full_name %}<p>Author: {{ obj.user.get_full_name }}</p>{% endif %}
        {{ obj.get_markdown|truncatechars_html:120 }}
        <p><a href="{{ obj.get_absolute_url }}" class="btn btn-primary" role="button">View</a></p>
      </div>
    </div>
  </div>
<hr/>
</div>
{% endfor %}


<div class="pagination">
    <span class="step-links">
        {% if object_list.has_previous %}
            <a href="?{{ page_request_var }}={{ object_list.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ object_list.number }} of {{ object_list.paginator.num_pages }}.
        </span>

        {% if object_list.has_next %}
            <a href="?{{ page_request_var }}={{ object_list.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a>
        {% endif %}
    </span>
</div>




</div>

{% endblock content %}

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用{% endblock home %}而不是{%endblock home %}。缺少空间有时会使事情搞砸。 如果这不起作用,请尝试仅使用{% endblock %}。 希望这会有所帮助!