1

I'm having an issue with this following line of code getting passed a arguement with spaces inside it.

<a href="{% url graph_exercisename exercise_name=e.exercisename|slugify %}" class="button" >Graph</a>

When i cut the piece of code above out of the html file, the page compiles fine. It also compiles when an arg that doesn't have a space in it is passed to exercise_name.

Here is my url pattern

urlpatterns = patterns('',
url('^exercises/(\d{1,6})/$', exercises,name='displayexercises'),
url('^workouts/$',workouts,name="displayworkouts"),
url('^graph/$',bodyweight_graph), 
url('^graph/(?P<exercise_name>\w+)/$',graph_exercisename,name="graph_exercisename"), 
)

and finally the view:

def graph_exercisename(request,exercise_name):
    exercise_name = exercise_name.replace('-'," ").title()

    exercises = Exercise.objects.filter(workout__user=request.user.id).filter(exercisename = exercise_name)
    exercises = exercises.order_by('workout__workoutdate')[:10]
    values = Rep_Table(exercises).table

    return render(request,'graph.html',{'values':values}) 

What's even more strange is that Django isn't giving me any code on the error page at all, it just states that Django has encountered an error.

Thanks for the help!

4
  • and you're sure that this line is the offender? have you tried {% load url from future %}{% url 'graph_exercisename' exercise_name=e.exercisename|slugify %}? Commented Jan 7, 2013 at 3:26
  • I know the page displays when I cut that line of code out of the page. What does that snippet do? Commented Jan 7, 2013 at 3:29
  • that snippet uses the "newer" style url tag which may have fixes for piped arguments. Worth a try at least. Commented Jan 7, 2013 at 3:32
  • I pasted it in to the href in my url tag, and it did not work. I'm using Django 1.3.1 Commented Jan 7, 2013 at 3:36

1 Answer 1

2

Please change your url entry:

url('^graph/(?P<exercise_name>\w+)/$',graph_exercisename,name="graph_exercisename"),

to:

url('^graph/(?P<exercise_name>[^/]+)/$',graph_exercisename,name="graph_exercisename"),
Sign up to request clarification or add additional context in comments.

2 Comments

this worked but I have no idea why. Could you elaborate further?
Because of \w+ it is leaving out the minus character (-), as you can see on Python RE, and slugify filter uses this character for replacing the spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.