Reputation: 169
So I am a new Django programmer and was wondering about how to pass a user-given value from a form into my URL address using Get method.
So I have this search bar, and when the user presses enter, it automatically creates a url address of "?search_bar=user_input"
But my urls.py, which is programmed to take the user_input variable, now fails to recognize my url address and my address is no longer matched to proceed to view.py.
urls.py
urlpatterns = [
url(r'^class/(?P<user_input>\w+)', views.newview),
]
I understand that you cannot simply do the following because you shouldn't match query string with URL Dispatcher
urlpatterns = [
url(r'^class/?search_bar=(?P<user_input>\w+)', views.newview),
]
I should I solve this issue? How can I make the url.py recognize my new address and carry the user_input over to views.py?
Upvotes: 0
Views: 52
Reputation: 66
when we talk about url
like something/?search_bar=user_input
,something/
is the url
part, and the ?search_bar=user_input
part belongs to the GET
method,so :
urls.py
urlpatterns = [
url(r'^class/$', views.newview),
]
and in your view.py:
def newview(request):
user_input = request.GET['user_input']
# or
# user_input = request.GET.get('user_input', None)
Upvotes: 2