Antu
Antu

Reputation: 2313

How to pass # in url as a parameter in django 2

I am trying to pass a code "Req-2019#000001" Django URL. I want to pass this code as well as normal string and number also in URL as arguments.

path('generateBomForSingleProduct/<requisition_no>/' , views.generateBomForSingleProduct, name='generateBomForSingleProduct'),

Its work properly but the problem is its add extra / before #

My URL is now this

http://127.0.0.1:8000/production/generateBomForSingleProduct/Req-2019/#000001

But I want to like this

http://127.0.0.1:8000/production/generateBomForSingleProduct/Req-2019#000001

Not an extra "/" before my "#"

Upvotes: 1

Views: 617

Answers (3)

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

Part after # is called fragment identifier, which is used by browsers and never sent to server.

In http://127.0.0.1:8000/production/generateBomForSingleProduct/Req-2019/#000001 url 000001 is never sent to server. Hence using important part of url after # is useless. Better to pass 000001 as query parameters or separate argument.

Upvotes: 1

Matthew Gaiser
Matthew Gaiser

Reputation: 4763

Using 'get' would be the proper way to do this. # is for html fragments, which are not sent to the server. Why can't it just be

/production/generateBomForSingleProduct/Req-2019?code=000001 and then you handle everything in the view that way?

Upvotes: 1

JPG
JPG

Reputation: 88509

The portion of the URL which follows the # symbol is not normally sent to the server in the request for the page. So, it's not possible to have a URL as /production/generateBomForSingleProduct/Req-2019#000001


Workaround

Just modify the url as /production/generateBomForSingleProduct/Req-2019/000001, so you need to modify the view also

# views.py
def generateBomForSingleProduct(request, part_1, part_2):
    unique_id = "{}#{}".format(part_1, part_2)
    # use the "unique_id"
    ...
#urls.py
urlpatterns = [
    ...,
    path('foo/<part_1>/<part_2>/', generateBomForSingleProduct, name="some-name"),
    ...

]

Upvotes: 2

Related Questions