Reputation: 646
I am trying to capture 2 slugs/variables from a url, pass them to a view and then use the view to pass them to a template.
The relevant line from my urls.py:
url(r'^docs/(?P<ver>)[-\w]+/(?P<name>)[-\w]+/$', views.sphinxdoc),
My views.py:
from django.shortcuts import render
def sphinxdoc(request, ver, name):
context = {'ver': ver, 'name': name}
return render(request, 'sphinx-documentation.html', context)
I am trying to change the src
url of an <iframe>
based on the ver
and name
values like so:
<iframe src="{% static 'docs/'|add:ver|add:'/'|add:name|add:'/_build/html/index.html' %}"></iframe>
When viewing in the browser, I get an error saying 'docs///_build/html/index.html'
could not be found. It seems like the template variables 'ver' and 'name' never get set and are therefore rendered as empty strings. How would I allow for these variables to be set from the views.sphinxdoc
function?
Upvotes: 1
Views: 77
Reputation: 5194
change urls.py
like this:
url(r'^docs/(?P<ver>\w+)/(?P<name>\w+)/$', views.sphinxdoc),
Since the name
slug could have "-"'s in it this won't quite work. Instead I used this:
url(r'^docs/(?P<ver>[-\w]+)/(?P<name>[-\w]+)/$, views.sphinxdoc),
Upvotes: 2
Reputation: 646
I figured it out and thought I would post the solution here for anyone else that might stumble into this problem.
I am not sure why, but when pulling in ver
and name
as a functional parameter, its variable type was not being set as string
. To solve this I just converted the variables to strings like so:
from django.shortcuts import render
def sphinxdoc(request, ver, name):
ver = str(ver)
name = str(name)
return render(request, 'sphinx-documentation.html', {'ver': ver, 'name': name})
This fixed the issue.
Edit: Additionally, you can simplify by doing in-line string conversion...
def sphinxdoc(request, ver, name):
return render(request, 'sphinx-documentation.html', {'ver': str(ver),
'name': str(name)})
Upvotes: 1