Rishi Saraswat
Rishi Saraswat

Reputation: 449

Django: - accessing value of a single button pressed on a page on next page

I'm displaying multiple buttons out of which one can be selected for any one contest that are available in database. here is my page

<h1>Contests Active</h1>
{% for contest in all_contest %}
    <form action="/confirm/" method = 'post'>
        Select the contest
        {% csrf_token %}
        <button name="Contest" type="submit" value="{{ contest.name }}">{{ contest.name }}</button>
    </form>
{% endfor %}

I want to get the value of the button pressed on next page to display contents related to that contest only here are my views.

def listcontest(request):
    all_contest = Contest.objects.all()
    return render(request,'sure.html',{'all_contest':all_contest})

def sure(request):
    if request.method == "POST":
        contest = request.POST.get['Contest']
        do_something();
        return render_to_response('sure.html',{'contest_name':contest})

here are my urls

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^contest/',views.listcontest),
    url(r'^confirm/',views.sure),

]

I'm getting an error instance method has no attribute __getitem__ please help me. I'm new to django.

Upvotes: 0

Views: 113

Answers (1)

Alasdair
Alasdair

Reputation: 309009

This is a Python syntax problem, it isn't really about Django. Since request.POST.get() is a method, you should use parentheses, not square brackets.

request.POST.get('Contest')

If you accessed the dictionary directly, you would use square brackets:

request.POST['Contest']

note that this will throw a KeyError if the key doesn't exist in the dictionary.

Upvotes: 2

Related Questions