python - how to send a parameter from html to django view.py -
i beginner , did lot of search every time got "django html" search result every time. following tutorial:
http://www.djangobook.com/en/2.0/chapter07.html
but on way not able pass paramter html view.py.
here directory:
directory: mysite:
directory: books
directory: templates
search_form.html
<html> <head> <title>search</title> </head> <body> <form action="/search/" method="get"> <input type="text" name="q"> <input type="submit" value="search"> </form> </body> </html>
views.py
from django.shortcuts import render django.http import httpresponse def search_form(request): return render(request, 'books/search_form.html') def search(request): if 'q' in request.get: message = 'you searched for: %r' % request.get['q'] else: message = 'you submitted empty form.' return httpresponse(message)
urls.py books
from django.conf.urls import url,include . import views urlpatterns = [ url(r'^$',views.search_form,name='search_form'), url(r'^$', views.search,name='search'), ]
and urls.py in mysite directory
"""mysite url configuration `urlpatterns` list routes urls views. more information please see: https://docs.djangoproject.com/en/1.9/topics/http/urls/ examples: function views 1. add import: my_app import views 2. add url urlpatterns: url(r'^$', views.home, name='home') class-based views 1. add import: other_app.views import home 2. add url urlpatterns: url(r'^$', home.as_view(), name='home') including urlconf 1. add import: blog import urls blog_urls 2. import include() function: django.conf.urls import url, include 3. add url urlpatterns: url(r'^blog/', include(blog_urls)) """ django.conf.urls import url,include django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^books/', include('books.urls')), ]
now problem when type: http://127.0.0.1:8000/books/
shows form's textbox , submit button when press submit shows me this:
firstly, need 2 different regexes search_form
, search
results. example:
url(r'^$',views.search_form,name='search_form'), url(r'^search/$', views.search,name='search'),
next, need update form's action point search view. since have included books/urls.py
/books/
prefix, need:
<form action="/books/search/" method="get">
it better use url tag instead of hardcoding urls. in case, do:
<form action="{% url 'search' %}" method="get">
Comments
Post a Comment