Hemanth S R
Hemanth S R

Reputation: 1113

How to get data from get method

How to send title value in get method?

getBooksDetails.html

    <a href="getBookDetails" style="text-decoration: none">
    <h4 class="overflow title" title="{{ book.title.upper }}">{{ book.title.upper }}</h4>
    </a>

urls.py

urlpatterns = patterns('',
    (r'^index', 'views.index'),
    (r'getBookDetails', 'views.getBookDetails'),
)

I am trying to print value of title in this method just to confirm value is coming or not. Can someone please help me, how to send and get values in GET method.

views.py

def getBookDetails(request):
    print request.GET // how to get title value here from get method
    return render_to_response('getBookDetails.html')

Upvotes: 1

Views: 1022

Answers (2)

Chris McKinnel
Chris McKinnel

Reputation: 15102

A better, more search engine friendly way might be to use keyword arguments in your URL, and have a slug on your book object.

getBooksDetails.html

<a href="getBookDetails" style="text-decoration: none">
<a href="{% url 'book' book.slug %}>{{ book.title.upper }}</a>
</a>

slug attribute on Book model

from django.utils.text import slugify

class Book(Model):
    ...
    slug = slugify(self.title)
    ...

urls.py

urlpatterns = patterns('',
                       (r'^index', 'views.index'),
                       (r'getBookDetails/(?P<slug>\w+/', 'views.getBookDetails'),
)

And you can access the title-slug in your view either by using **kwargs if you're using class based views, or by adding slug=None to your view definition if you're using function based views.

Upvotes: 1

Victor Castillo Torres
Victor Castillo Torres

Reputation: 10811

You can send data in the URL:

for example say we have an url like this:

http://localhost:8000/hi?id="bye"

You can obtain the data in Django from two ways:

request.GET['id']

or

request.GET.get('id', 'default value')

Upvotes: 1

Related Questions