Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialandetin
5,263 PointsHow to use URL parameters containing slashes?
Hello,
I've got an URL structure, such as:
url(r'^$', views.make_select_view, name="make-select"),
url(r'^(?P<make>[-()\w\d\s.,]+)/$', views.model_select_view, name="model-select"),
url(r'^(?P<make>[-()\w\d\s.,]+)/(?P<model>[-()\w\d\s.,]+)/$', views.platform_select_view, name="platform-select"),
In my database I've got many variables containing slashes, such as "Opel / Vauxhall" as <make> or "V8 / V12 Vantage" as <model>.
Above regex does not accept slashes in URL paramaters, thus returning NoReverseMatch error. But if I include "/", then Django presumes it as a separator instead of a part of the variable.
For instance, when I select "Opel / Vauxhall" as <make>, my URL becomes:
/Opel%20/%20Vauxhall/
while "Opel" is selected as a make and "Vauxhall" is selected as a model.
I've read this but still can not figure out how to escape those slashes in my variables.
Any suggestion or feedback will be welcomed and greatly appreciated.
Thanks!
And
1 Answer
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey andetin,
My first question would be if those makes and models have to exist in your database with "/" in them.
Secondly, do these makes and models have IDs associated with them in the database? It would probably be cleaner and much easier to anticipate the URL parameters if you use make and model IDs instead.
Let me know what your thoughts are!
andetin
5,263 Pointsandetin
5,263 PointsHey Chris,
I could replace all slashes with, for example, dashes, but it's a very large database consisting of 20.000+ models which will be regularly outsourced and updated. So I don't really want to edit it.
Each "Car" model instance has a make, model, platform etc. I'm filtering through these attributes to show a specific car. So "car"s have ID's, but not makes or models.
To solve the problem, I changed my URL structure from:
/<make>/<model>/<platform>/
to:
/<make>_<model>_<platform>/
So far so good.
Thank you for your feedback!
Best,
And
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsAh, gotcha. I'm glad you were able to find a solution :)!