__instancecheck中超出/ person /最大递归深度的RecursionError

时间:2016-08-30 09:39:01

标签: python django

我的包含标记在/ person /

中给出" RecursionError

instancecheck &#34>超出了最大递归深度错误。

完整追溯: 模板错误: 在模板/home/ohid/test_venv/alumni/member/templates/member/person_list.html中,第15行出错

my person_list.html:

{% extends 'alumni/base.html' %}

{% block content %}
    <h2>Members</h2>
    <table>

        <tr>
            <th>sl.</th>
            <th>Name and Position</th>
            <th>Photo</th>
            <th>Organisation & Address</th>
            <th>Contact</th>
        </tr>
        {% for person in persons %}
        <tr>
            <td>{{forloop.counter}}.</td>
            <td>{{person.name}}<br>
            {{person.present_position}}
</td>
            <td><a href="{% url 'member:person-list' %}">
            <img src="{{ person.photo_url|default_if_none:'#'}}" class="img-responsive">
        </a></td>
            <td>
            {{person.organization}}<br>
           {{person.address}}
            </td>
            <td>
             {{person.tele_land}}<br>
             {{person.tele_cell}}<br>
             {{person.email}}
            </td>
        </tr>
        {% endfor %}
    </table>
{% endblock %}

我的包含标签:

from django import template
from ..models import Person

register = template.Library()

@register.inclusion_tag('member/person_list.html')
def get_person_list():
    return {'persons': Person.objects.all()}

我的人模特:

class Person(models.Model):    
    name = models.CharField(max_length=128)
    present_position=models.CharField(max_length=100)
    organization= models.CharField(max_length=150,blank=True)
    address = models.CharField(max_length=150, blank=True)
    tele_land = models.CharField(max_length=15,blank=True)
    tele_cell = models.CharField(max_length=15, blank=True)
    email = models.EmailField(max_length=70, blank=True, null=True, unique=True)
    photo= ResizedImageField(size=[60, 60],crop=['middle', 'center'],upload_to='persons/%Y/%m/%d/',null=True,
        blank=True,
        editable=True,
        help_text="Person Picture")    
    category = models.ForeignKey('Membership', on_delete=models.CASCADE)
    member_since = models.DateField(blank=True)
    image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
    image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")

    def get_absolute_url(self):
        return reverse('member:detail', kwargs={'pk': self.pk})

我的网址:

app_name = 'member'
urlpatterns = [
    # Member
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'member/add/$', views.AlbumCreate.as_view(), name='member-add'),
    url(r'^person/$', views.PersonListView.as_view(), name='person-list'),
]

如何避免最大递归深度?任何人都可以提出方法吗?

编辑: 的ListView:

class PersonListView(generic.ListView):
    model = Person
    context_object_name = 'persons'

我的base.html:

<!DOCTYPE html>
<html lang="en">
{% load staticfiles %}
{% load member_template_tags %}
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="{% static 'images/favicon.ico' %}">

      <title>Alumnai - {% block title %}Applied Chemistry & Chemical Engineering{% endblock %}</title>
      <!-- Bootstrap core CSS -->
    <link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="http://v4-alpha.getbootstrap.com/examples/dashboard/dashboard.css" rel="stylesheet">

  </head>

  <body>

    <nav class="navbar navbar-dark navbar-fixed-top bg-inverse">
      <button type="button" class="navbar-toggler hidden-sm-up" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Project name</a>
       <div id="navbar">
        <nav class="nav navbar-nav pull-xs-left">
            <a class="nav-item nav-link" href="#">Home</a>
            <a class="nav-item nav-link" href="#">About</a>
            <a class="nav-item nav-link" href="#">Search</a>


        </nav>
      </div>

    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            {% block sidebar_block %}
                {% get_person_list %}
            {% endblock %}
        </div>
        <div class="col-sm-9 offset-sm-3 col-md-10 offset-md-2 main">
         {% block body_block %}{% endblock %}


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

     <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY" crossorigin="anonymous"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js" integrity="sha384-Plbmg8JY28KFelvJVai01l8WyZzrYWG825m+cZ0eDDS1f7d/js6ikvy1+X+guPIB" crossorigin="anonymous"></script>
    <script src="http://v4-alpha.getbootstrap.com/dist/js/bootstrap.min.js"></script>
    <!-- Just to make our placeholder images work. Don't actually copy the next line! -->
    <script src="http://v4-alpha.getbootstrap.com/assets/js/vendor/holder.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="http://v4-alpha.getbootstrap.com/assets/js/ie10-viewport-bug-workaround.js"></script>

  </body>
</html>

1 个答案:

答案 0 :(得分:2)

你在这里做了一件非常奇怪的事情,我无法解决原因。您说您的基本模板正在使用this.differ = differs.find({}).create(null); 包含标记,该标记呈现person_list模板。但那是你视图的模板;所以当你去/ register /时,Django进入视图,开始渲染person_list模板,看到它扩展了基本模板,开始渲染,遇到模板标签,开始渲染该模板,转到基础模板。正如你所看到的那样,这是一次无限递归。

首先应该没有理由在基础模板中放置这样的模板标记,但即使这样做,包含标记应始终呈现自己的模板,而不是从视图中重用现有模板。