Reputation: 601
This question is quite related to my last question.
I've seen that working with Templates creates some Django version problem with a tutorial I'm following, I think there's something wrong with the urls. Here's my urls.py:
from django.conf.urls import patterns, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^preguntas/$', 'preguntasyrespuestas.views.index', name='preguntas'),
url(r'^preguntas/(?P<pregunta_id>\d+)/$', 'preguntasyrespuestas.views.pregunta_detalle', name='pregunta_detalle')
)
And the link isn't working is in that index.html file:
{% extends "base.html" %}
{% block title %}Questions{% endblock %}
{% block content %}
{% for pregunta in preguntas %}
<a href="(% url "pregunta_detalle" pregunta.id %}">{{ pregunta.asunto }} ?</a><br/>
{% endfor %}
{% endblock %}
Also, here are my views.py:
from django.http import HttpResponse,Http404
from preguntasyrespuestas.models import Pregunta
from django.shortcuts import get_object_or_404, render_to_response
def index(request):
preguntas = Pregunta.objects.all()
return render_to_response('preguntasyrespuestas/index.html',
{'preguntas': preguntas})
def pregunta_detalle(request, pregunta_id):
pregunta = get_object_or_404(Pregunta, pk=pregunta_id)
return render_to_response('preguntasyrespuestas/pregunta_detalle.html',
{'pregunta': pregunta})
The error reported by the browser says something like:
Using the URLconf defined in primerproyecto.urls, Django tried these URL patterns, in this order:
^preguntas/$ [name='preguntas']
^preguntas/(?P<pregunta_id>\d+)/$ [name='pregunta_detalle']
The current URL, preguntas/(% url, didn't match any of these.
Instead, if I type the URL in the browser '127.0.0.0.1:8000/preguntas/1' it appears. What kind of URL connection could be wrong? Can't find the answer... really appreciated, thank you
Upvotes: 1
Views: 307
Reputation: 11971
It should work if you use single quotation marks around the URL name. You also need to alter the opening bracket in your href
to a curly-brace {
:
<a href="{% url 'pregunta_detalle' pregunta.id %}">{{ pregunta.asunto }} ?</a>
^ ^ ^
Upvotes: 2