> I have written the code
> model.py
> from django.db import models
>
> class Math(models.Model):
>    input1 = models.IntegerField()
>    input2 = models.IntegerField()
>    output = models.IntegerField()
>
> Views.py file
>
> from django.template import RequestContext
> from django.shortcuts import render_to_response
> from mysite.maths.models import Math
> from django.http import Http404
> from django.shortcuts import render_to_response, get_object_or_404
> from django.http import HttpResponseRedirect, HttpResponse
> from django.template import RequestContext
>
> def index(request):
>    latest_math_list = Math.objects.all().order_by('
> id')[:5]
>    return render_to_response('maths/index.html', {'latest_math_list':
> latest_math_list})
>
> def detail(request, math_id):
>    p = get_object_or_404(Math, pk=math_id)
>    return render_to_response('maths/detail.html', {'math': p},
>                               context_instance=RequestContext(request))
> def results(request, math_id):
>    p = get_object_or_404(Math, pk=math_id)
>    return render_to_response('maths/results.html', {'math': p})
>
> def vote(request, math_id):
>    p = get_object_or_404(Math, pk=math_id)
>    try:
>        selected_input1 = p.math_set.get(pk=request.POST['input1'])
>        selected_input2 = p.math_set.get(pk=request.POST['input2'])
>    except (KeyError, Math.DoesNotExist):
>   # Redisplay the poll voting form.
>        return render_to_response('maths/detail.html', {
>            'poll': p,
>            'error_message': "You didn't fill a value.",
>        }, context_instance=RequestContext(request))
>    else:
>        selected_output = selected_input1 + selected_input2
>        selected_output.save()
>
>        return HttpResponseRedirect(reverse('mysite.maths.views.results',
> args=(p.id
>
> ,)))
>
> problem is in def vote function.

And you still don't tell us what the actual problem is. There are two
obvious issues with the code you've posted, but you haven't told us
which issue you are encountering.

Firstly, you get a Math object `p`, then get another two Math
instances via `p.math_set.get()`. With the code you've posted, the
Math model doesn't have a `math_set` attribute - for that to exist,
the model would need to have a foreign key relationship to self, which
it doesn't have. It's not clear what you are trying to do here.

Secondly, presuming that works, selected_input1 and selected_input2
are both instances of the Math model. You haven't defined an __add__
method on Math, so adding two instances makes no sense. If you just
want to add two numbers and save the result, just do that, don't try
to add the instances.
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to