2

I am a beginner and did a lot of search but every time I got only "django to html" as search result every time. I am following this tutorial:

http://www.djangobook.com/en/2.0/chapter07.html

but on the way I am not able to pass paramter from html to view.py.

Here is my directory:

enter image description here

directory: mysite:

enter image description here

directory: books

enter image description here

directory: templates

enter image description here

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
from 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 an empty form.'
    return HttpResponse(message)

urls.py for books

from django.conf.urls import url,include
from . 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

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Import the include() function: from django.conf.urls import url, include
    3. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^books/', include('books.urls')),
]

Now the problem is when I type: http://127.0.0.1:8000/books/ it successfully shows the form's textbox and submit button but when I press submit it shows me this:

enter image description here

4
  • 1
    You did not define the URL in urls.py. Commented Apr 26, 2016 at 11:59
  • change your form action to be <form action="{% url 'search' %}" method="get"> and never use hardcoded urls in templates. Also i'd suggest to start using class based views Commented Apr 26, 2016 at 12:00
  • it did remove the error but nothing is printing such as in view i have place : "You searched for: %r'" which shd be printing such thing Commented Apr 26, 2016 at 12:02
  • Klaus did u mean i shd declare something in mysite urls? Commented Apr 26, 2016 at 12:03

3 Answers 3

1

Firstly, you need two different regexes for the search_form and search results. For example:

url(r'^$',views.search_form,name='search_form'),
url(r'^search/$', views.search,name='search'),

Next, you need to update the form's action to point to the search view. Since you have included your books/urls.py with the /books/ prefix, you need:

<form action="/books/search/" method="get">

It is better to use the url tag instead of hardcoding your urls. In this case, you would do:

<form action="{% url 'search' %}" method="get">
Sign up to request clarification or add additional context in comments.

5 Comments

Absolutely correct. Thnx bro. but tell me how to use url tag? instead of hardcoding
I showed you how to use the url tag already - {% url 'search' %}. There's more info about it in the docs.
one thing more why i m getting "u" in the output string: "You searched for: u'java'"
Because you used %r instead of %s.
oh just like C. Thnx
1

In addition to the answer of Alasdair I would use "books:search" for clear namespace:

<form action="{% url 'books:search' %}" method="get">

2 Comments

u'books' is not a registered namespace
If you do this, you also have to include the namespace in the url config, i.e. url(r'^books/', include('books.urls', namespace='books')),
0

The url /search/ doesn't exist, you didnt define that it should exist.

It would be /books/ judging from that URLs file you showed. Also on a side note, don't use http://www.djangobook.com/en/2.0/index.html

They have a warning on the main page that it is no longer up to date.

Use masteringdjango.com and other up to date resources.

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.