模板渲染期间的TemplateSyntaxError

时间:2016-01-17 02:51:05

标签: python django python-3.x django-templates django-views

Python / Django初学者 - 我收到此错误:

  

反转'主题'有参数'('',)'和关键字参数' {}'   未找到。尝试了1种模式:['主题/(?P \ d +)/ $']

尝试加载我的模板时。 这是我的模板:

{% extends "learning_logs/base.html" %}

{% block content %}

<p>Topics</p>

<ul>
{% for topic in topics %}
  <li>
    <a href="{% url 'learning_logs:topic' topic_id %}">{{ topic }}</a>
  </li>
{% empty %}
  <li>No topics for now</li>
{% endfor %}
</ul>

{% endblock content %}

这是我的views.py

from django.shortcuts import render
from .models import Topic

# Create your views here.
def index(request):
    '''Home page for learning log'''
    return render(request, 'learning_logs/index.html')

def topics(request):
    '''Show all topics'''
    topics = Topic.objects.order_by('date_added')
    context = {'topics': topics}
    return render(request, 'learning_logs/topics.html', context)

def topic(request, topic_id):
    '''Show a single topic and all its entries'''
    topic = Topic.objects.get(id=topic_id)
    entries = topic.entry_set.order_by('-date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'learning_logs/topic.html', context)

我现在已经有一段时间了,在这里阅读一些以前的答案,但它们都与auth / login无法正常工作有关。还尝试删除&#39;&#39;在网址之后作为一些答案建议,但它没有工作。我正在使用 Python Crash Course:一个实践,基于项目的编程入门来完成我的教程。

任何帮助将不胜感激。

最后,这是我的urls.py代码     来自django.conf.urls import url     来自。导入视图

urlpatterns = [
    # Home page
    url(r'^$', views.index, name='index'),  
    url(r'^topics/$', views.topics, name='topics'), 
    url(r'^topics/(?P<topic_id>\d+)/$', views.topics, name='topic'),

1 个答案:

答案 0 :(得分:3)

根据错误,有一个参数传递到url标记,但它是空的:

  

使用参数'('',)'反转'主题'...

那是因为topic_id变量,它没有被定义。您应该使用topic.id代替:

<a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a>
相关问题