2

I'm writing a page that takes an id and finds a user that matches that id. this is the URL pattern that I wrote in urls.py:

re_path(r'^users/id=(?P<username>[0-9]{9})$' , views.usershow , name = 'usershow') ,

I want to pass username using forms and so I wrote this in templates:

<form action="{% url 'CMS:usershow' %}" method="GET" >
{% csrf_token %}
<input name="id" type="number" placeholder="search">
<button type="submit">find</button>
</form>

but it shows me this error :

Reverse for 'usershow' with no arguments not found. 1 pattern(s) tried: ['dashboard/users/id=(?P<username>[0-9]{9})$']

How can I pass it using forms with this URL pattern?

2
  • Do you really have to pass {% csrf_token %} when your form method is GET? I think not. Commented Jun 3, 2019 at 18:47
  • @HassanBaig no. I tried post method before that and I forget to delete Commented Jun 3, 2019 at 19:09

2 Answers 2

1

That's not how forms work. Remove the username from the pattern:

path(r'^users/' , views.usershow , name = 'usershow') ,

and in the view get the value from the request;

username = request.GET["id"]
Sign up to request clarification or add additional context in comments.

Comments

1

First, make sure CMS app's urls.py is having the path.

Second, pass user id value as shown below:

<form action="{% url 'CMS:usershow' user_id %}" method="GET" >

REF: https://docs.djangoproject.com/en/2.1/topics/http/urls/#s-reverse-resolution-of-urls

1 Comment

but I want to user enters the username with input.

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.