【问题标题】:Django: TemplateSyntaxError: Could not parse the remainderDjango:TemplateSyntaxError:无法解析剩余部分
【发布时间】:2013-10-26 01:15:25
【问题描述】:

我在输入 localhost:8000/admin/ 时遇到了这个问题。

`TemplateSyntaxError:无法解析剩余部分:来自“admin:password_change”的“:password_change”。 'url' 的语法在 Django 1.5 中发生了变化,请参阅文档。

这是我settings.py的一部分:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'grappelli',
    'filebrowser',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    #'django.contrib.admindocs',
     'tinymce',
     'sorl.thumbnail',
     'south',
     'django_facebook',
     'djcelery',
     'devserver',
     'main',
)
AUTH_USER_MODEL = 'django_facebook.FacebookCustomUser'

AUTHENTICATION_BACKENDS = (
    'django_facebook.auth_backends.FacebookBackend', 
    'django.contrib.auth.backends.ModelBackend',
    # Uncomment the following to make Django tests pass:
    'django.contrib.auth.backends.ModelBackend',
)

我是不是做错了什么?

PS:这是我的完整回溯 https://gist.github.com/anonymous/e8c1359d384df7a6b405

编辑:

我正在根据请求粘贴 grep 的输出:

$ ack-grep --type=python -r ':password_change' .
lib/python2.7/site-packages/django/contrib/admin/sites.py
264:url = reverse('admin:password_change_done', current_app=self.name)

lib/python2.7/site-packages/grappelli/dashboard/dashboards.py
147:reverse('%s:password_change' % site_name)],

$ ack-grep --type=html -r ':password_change' .
lib/python2.7/site-packages/django/contrib/admin/templates/admin/base.html
36:<a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a> /

lib/python2.7/site-packages/grappelli/templates/admin/includes_grappelli/header.html
12:{% url admin:password_change as password_change_url %} 

【问题讨论】:

  • 你能在文件夹范围内搜索:password_change 并显示包含代码的模板吗?
  • 好的,谢谢。我刚刚粘贴了输出。
  • 我刚刚遇到了这个问题,因为模板有像{% if thing in ['literal', 'list', 'of', 'elements'] %} 这样的代码,它在 jinja 中有效,但不适用于 Django 模板。
  • 当你写{{ url ..而不是{% url ..时也会发生这种情况

标签: django django-admin django-facebook


【解决方案1】:

此错误通常意味着您在尝试渲染的模板中的某处忘记了结束引号。例如:{% url 'my_view %}(错误)而不是 {% url 'my_view' %}(正确)。在这种情况下,导致问题的是冒号。通常你会编辑模板以使用正确的{% url %}syntax

但是 django 管理站点没有理由抛出这个,因为它知道它自己的语法。因此,我最好的猜测是 grapelli 会导致您的问题,因为它会更改管理模板。从已安装的应用中删除 grappelli 有帮助吗?

【讨论】:

  • 谢谢,我刚刚在我的 virtualevn 中做了一个 grep,似乎 grapelli 必须对它做些什么。可以修吗?
  • 好的,我将{% url admin:password_change as password_change_url %} 更改为{% url 'admin:password_change' as password_change_url %}(添加了单引号),它似乎有效。单引号也需要添加到其他地方。需要更改的文件是这样的:lib/python2.7/site-packages/grappelli/templates/admin/includes_grappelli/header.html.
  • 我想补充一点,当我应该使用{{}} 时,我得到了这个错误,而我应该使用{%%}
【解决方案2】:

name 后面不能有空格。

不正确:

{% url 'author' name = p.article_author.name.username %}

正确:

{% url 'author' name=p.article_author.name.username %}

【讨论】:

    【解决方案3】:

    templates/admin/includes_grappelli/header.html 的第 12 行中,您忘记将 admin:password_change 放在 ' 之间。

    url Django 标记语法应该总是这样:

    {% url 'your_url_name' %}
    

    【讨论】:

      【解决方案4】:

      当你使用 jinja 模板(调用对象方法的语法不同)并且你忘记在 settings.py 中设置它时也会发生

      【讨论】:

      • {% csrf_token %}
        你能在这段代码中找到错误吗?Pizzacart 是一个列表,其中包含与比萨饼相关的详细信息,比萨饼代表它们中的每一个。
      【解决方案5】:

      模板语法错误:由于多种原因,其中之一是 {{ post.date_posted|date: "F d, Y" }} 是冒号 (:) 和引号 (") 之间的空格,如果您删除空格然后它像这样工作..... {{ post.date_posted|date:"F d, Y" }}

      【讨论】:

      • 根据错误信息,这似乎不是这个问题中语法错误的原因。
      【解决方案6】:

      您已在 settings.py 中缩进了部分代码:

      # Uncomment the next line to enable the admin:
           'django.contrib.admin',
          # Uncomment the next line to enable admin documentation:
          #'django.contrib.admindocs',
           'tinymce',
           'sorl.thumbnail',
           'south',
           'django_facebook',
           'djcelery',
           'devserver',
           'main',
      

      因此,它给你一个错误。

      【讨论】:

        【解决方案7】:

        对我来说,它使用的是 {{ }} 而不是 {% %}:

        href="{{ static 'bootstrap.min.css' }}"  # wrong
        href="{% static 'bootstrap.min.css' %}"  # right
        

        【讨论】:

          【解决方案8】:

          确保您正确使用静态文件和 URL

          <link href="{% static 'css/semantic.min.css' %}" rel="stylesheet">
          
          • 检查是否用引号正确括起来
          • 检查开始和结束括号
          • 检查标签是否正确关闭

          【讨论】:

            猜你喜欢
            • 2022-10-05
            • 2021-01-02
            • 2018-11-06
            • 2011-04-03
            • 2011-11-06
            • 2018-03-31
            • 2020-05-10
            • 2017-11-18
            • 1970-01-01
            相关资源
            最近更新 更多