0

I have a django project where url is like this

url(r'^invoice/(?P<invoice_id>[A-Za-z0-9]+)/(?P<order_id>[A-Za-z0-9]+)$',GenerateInvoicePdf,name='invoice'),

which generates url localhost:8000/invoice/2341wq23fewfe1231/3242

but i want url to be like localhost:8000/invoice?invoice_id=2341wq23fewfe1231&order_id=3242

i tried documentation and used syntax like this re_path(r'^comments/(?:page-(?P<page_number>\d+)/)?$', comments), But did not get desired result.

how can i do so?

1
  • If you're using Django 2.0 + you can change your URL patterns so they don't need to have regex in them. Commented Dec 23, 2018 at 13:08

1 Answer 1

3

The parts which you are trying to write after ? is called url query string. You don't need to define them in the urls.py. You can just use:

re_path(r'^comments/$', comments),

And inside comments views, you can access the query string like this:

def comments(request):
    invoice_id = request.GET.get('invoice_id')
    order_id = request.GET.get('order_id')
    # rest of the code
Sign up to request clarification or add additional context in comments.

Comments

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.