0

I know this is an armature question but here goes.

I have a url path as follows: path('projects/<s>', PL.projects),

And I pass a string from the html template by putting it into an href tag like so projects/some_string. this works once but then the base url changes to <ip>/projects/some_string. so when I try to excite the path to pass a string in that domain then I get an error as the url is now <ip>/projects/projects/some_string.

How do I set it up so that I can pass as many strings as possible as many times as possible without having to clean my url in the browser everytime.

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • Does this answer your question? [Why would a developer place a forward slash at the start of each relative path?](https://stackoverflow.com/questions/7613274/why-would-a-developer-place-a-forward-slash-at-the-start-of-each-relative-path) – Abdul Aziz Barkat Sep 30 '21 at 03:10

2 Answers2

0

Learn how to use the reverse() function and the url template tag and your problems will be gone.

Those functions are built-in with Django and can handle all of that nasty URL stuff.

Reverse: https://docs.djangoproject.com/en/3.2/ref/urlresolvers/

Url Template tag: https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#url

mendespedro
  • 466
  • 3
  • 8
0

Django has inbuilt url lookup features

path("some_random_url_link_1/", views.Link1View.as_view(), name="url_link_1"),
path("some_random_url_link_2/<int:some_id>/<slug:some_slug>/", views.Link2View.as_view(), name="url_link_2"),

in your template you can use it like this, and pass variables/parameters like this. FYI you don't need to use {{variable}} tag here

<a href="{% url 'url_link_1' %}" >Link 1</a>
<a href="{% url 'url_link_2' some_id=id1 some_slug=random_slug %}" >Link 2</a>
Vipin Mohan
  • 1,631
  • 1
  • 12
  • 22
  • how do i fix this ```Reverse for 'projects_with_file' with keyword arguments '{'some_slug': 'templates copy'}' not found. 1 pattern(s) tried: ['projects_with_file/(?P[-a-zA-Z0-9_]+)/$'] ``` – Dawit Tilahun Sep 30 '21 at 14:51
  • which version of django are you using? also please share your urls.py. – Vipin Mohan Oct 01 '21 at 08:51