I made a copy of a working Django poll, however i did something wrong, because my poll does not work
Question: I am trying to post a value to my database. I have been looking for a long
time now at a working poll app, but i cannot seem to get my poll working.
Hereunder the parts used by the poll app where somewhere the error must be.

Thanks in advance for any help.

The error i get when i dont make a choice for the poll is:

**NoReverseMatch at /wedding/2/vote/ Reverse for 'vote' with arguments '('',)'
and keyword arguments '{}' not found. 1 pattern(s) tried:
[u'wedding/(?P\d+)/vote/$']**

The error i get when i do make a choice for the poll is:

**ValueError at /wedding/2/vote/ invalid literal for int() with base 10: ''**

urls.py

    
    
    from django.conf.urls import patterns, url
    
    from wedding import views
    
    urlpatterns = patterns('',
        url(r'^$', views.IndexView.as_view(), name='index'),
        url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
        url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
        url(r'^(?P<invitee_id>\d+)/vote/$', views.vote, name='vote'),
    )
    

views.py

    
    
    from django.shortcuts import render, get_object_or_404
    from django.http import HttpResponseRedirect, HttpResponse
    from django.core.urlresolvers import reverse
    from django.views import generic
    from django.template import RequestContext, loader
    # Create your views here.
    from wedding.models import Invitee, Invitee_extra
    
    class IndexView(generic.ListView):
        template_name = 'wedding/index.html'
        context_object_name = 'party_list'
    
        def get_queryset(self):
            """Return the party names"""
            return Invitee.objects.all().order_by('-party_name')[:5]
    
    class DetailView(generic.DetailView):
        model = Invitee
        template_name = 'wedding/detail.html'
    
    class ResultsView(generic.DetailView):
        model = Invitee
        template_name = 'wedding/results.html'
    
    def vote(request, invitee_id):
        p = get_object_or_404(Invitee, pk=invitee_id)
        try:
            selected_choice = p.invitee_extra_set.get(pk=request.POST['choice'])
        except (KeyError, Invitee_extra.DoesNotExist):
            # Redisplay the poll voting form.
            return render(request, 'wedding/detail.html', {
                'wedding': p,
                'error_message': "You didn't select a attendance.",
            })
        else:
            selected_choice.attend += 1
            selected_choice.save()
            # Always return an HttpResponseRedirect after successfully dealing
            # with POST data. This prevents data from being posted twice if a
            # user hits the Back button.
            return HttpResponseRedirect(reverse('details:results', args=(p.id,)))
    

details.html

    
    
    <h1>name: {{ invitee.party_name }} id: {{ invitee.id }} </h1>
    
    {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    
    <form action="{% url 'wedding:vote' invitee.id %}" method="post">
    {% csrf_token %}
    {% for guests in invitee.invitee_extra_set.all %}
        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
        <label for="choice{{ forloop.counter }}">{{ guests.guest }}</label><br />
    {% endfor %}
    <input type="submit" value="Update gegevens" />
    </form>
    

models.py

    
    
    from django.db import models
    
    # Create your models here.
    class Invitee(models.Model):
        party_name = models.CharField(max_length=50)
        created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.party_name
    
    class Invitee_extra(models.Model):
        invitee = models.ForeignKey(Invitee)
        guest = models.CharField(max_length=50)
        attend = models.IntegerField(default=0)
        def __unicode__(self):  # Python 3: def __str__(self):
            return self.invitee
    

Full traceback when i do select a radiobutton

    
    
    Environment:
    
    
    Request Method: POST
    Request URL: http://127.0.0.1:8000/wedding/2/vote/
    
    Django Version: 1.6.2
    Python Version: 2.7.5
    Installed Applications:
    ('django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'wedding',
     'food',
     'polls')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')
    
    
    Traceback:
    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
      114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/patrick/python/mysite/wedding/views.py" in vote
      28.         selected_choice = p.invitee_extra_set.get(pk=request.POST['choice'])
    File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in get
      151.         return self.get_queryset().get(*args, **kwargs)
    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get
      298.         clone = self.filter(*args, **kwargs)
    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in filter
      590.         return self._filter_or_exclude(False, *args, **kwargs)
    File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
      608.             clone.query.add_q(Q(*args, **kwargs))
    File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
      1198.         clause = self._add_q(where_part, used_aliases)
    File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in _add_q
      1234.                     current_negated=current_negated)
    File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_filter
      1125.         clause.add(constraint, AND)
    File "/Library/Python/2.7/site-packages/django/utils/tree.py" in add
      104.         data = self._prepare_data(data)
    File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in _prepare_data
      79.             value = obj.prepare(lookup_type, value)
    File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in prepare
      352.             return self.field.get_prep_lookup(lookup_type, value)
    File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
      369.             return self.get_prep_value(value)
    File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
      613.         return int(value)
    
    Exception Type: ValueError at /wedding/2/vote/
    Exception Value: invalid literal for int() with base 10: ''
    

and the fulltraceback if i dont make a choice:

    
    
    Environment:
    
    
    Request Method: POST
    Request URL: http://127.0.0.1:8000/wedding/1/vote/
    
    Django Version: 1.6.2
    Python Version: 2.7.5
    Installed Applications:
    ('django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'wedding',
     'food',
     'polls')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')
    
    
    Template error:
    In template /Users/patrick/python/mysite/wedding/templates/wedding/detail.html, error at line 5
       Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'wedding/(?P<invitee_id>\\d+)/vote/$']
       1 : <h1>name: {{ invitee.party_name }} id: {{ invitee.id }} </h1>
    
    
       2 : 
    
    
       3 : {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
    
    
       4 : 
    
    
       5 : <form action=" {% url 'wedding:vote' invitee.id %} " method="post">
    
    
       6 : {% csrf_token %}
    
    
       7 : {% for guests in invitee.invitee_extra_set.all %}
    
    
       8 :     <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    
    
       9 :     <label for="choice{{ forloop.counter }}">{{ guests.guest }}</label><br />
    
    
       10 : {% endfor %}
    
    
       11 : <input type="submit" value="Update gegevens" />
    
    
       12 : </form>
    
    Traceback:
    File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
      114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/Users/patrick/python/mysite/wedding/views.py" in vote
      33.             'error_message': "You didn't select a attendance.",
    File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render
      53.     return HttpResponse(loader.render_to_string(*args, **kwargs),
    File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
      169.         return t.render(context_instance)
    File "/Library/Python/2.7/site-packages/django/template/base.py" in render
      140.             return self._render(context)
    File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
      134.         return self.nodelist.render(context)
    File "/Library/Python/2.7/site-packages/django/template/base.py" in render
      840.                 bit = self.render_node(node, context)
    File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
      78.             return node.render(context)
    File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
      447.                         six.reraise(*exc_info)
    File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
      433.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
    File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
      509.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
    File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
      429.                              (lookup_view_s, args, kwargs, len(patterns), patterns))
    
    Exception Type: NoReverseMatch at /wedding/1/vote/
    Exception Value: Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'wedding/(?P<invitee_id>\\d+)/vote/$']
    


Answer: In your vote view, if the submission is not valid, you re-render the template
but you don't pass the Invitee to it - or rather you do, but you call it
`wedding` rather than `invitee`. So when the template tries to calculate the
URL of the vote submission for the invitee, it can't find the if value to use.
You need to be sure to pass the data with the correct names.

Note this would have been easier to debug if you'd provided more information
about the error, such as the full traceback and what you did leading up to it.

