On Jan 22, 2008 10:16 PM, Jason Witherspoon <[EMAIL PROTECTED]>
wrote:

> Hi folks--
>
> I've got the following code in a view:
>
> from django.shortcuts import get_object_or_404, render_to_response
> from django.http import HttpResponseRedirect
> from django.core.urlresolvers import reverse
> from mysite.officemeetingpoll.models import Meetingtime
> from django.contrib.auth.decorators import login_required
> from django.contrib.auth import authenticate, login
> from django.newforms import form_for_instance
>
> @login_required
> def vote(request):
>        m = Meetingtime.objects.get(user=request.user)
>        UserForm = form_for_instance(m)
>        f = UserForm(auto_id=False)
>        return render_to_response('officemeetingpoll/form.html',
> {'form':f})
>
> I'm hoping this will check if the user is logged in, go to the default
> login page & redirect back if not, & then render an edit form for the
> office meeting poll.
>
> Here's the model:
>
> from django.db import models
> from django.contrib.auth.models import User
>
> class Meetingtime(models.Model):
>        time = models.CharField(max_length=40)
>        user = models.ManyToManyField(User, blank=True, null=True)
>        CONVENIENCE_CHOICES = (
>                ('A', 'I can make this time regularly'),
>                ('S', 'I could occasionally make this time'),
>                ('N', 'I could rarely if ever make this time'),
>        )
>        convenience = models.CharField(max_length=1,
> choices=CONVENIENCE_CHOICES)
>        unique_together = (("user", "time"),)
>        def __unicode__(self):
>                return self.time
>        class Admin:
>                list_display = ('time', 'convenience')
>                fields = (
>                        (None, {'fields': ('time', 'convenience',
> 'user')}),
>                )
>
>
>
> However, it doesn't send me to the login page, it generates the
> following error:
>
>
> DoesNotExist at /officepoll/officepolls/
> Meetingtime matching query does not exist.
> Request Method:         GET
> Request URL:    http://www.rainbow.coop/officepoll/officepolls/
> Exception Type:         DoesNotExist
> Exception Value:        Meetingtime matching query does not exist.
> Exception Location:     /usr/local/lib/python2.5/site-packages/django/db/
> models/query.py in get, line 263
> Python Executable:      /usr/local/bin/python
> Python Version:         2.5.1
> Python Path:    ['/home/jason/killdjangokill', '/usr/local/lib/python2.5/
> [snipped]
>
> ...which is no surprise, since the view is relying upon the logged-in
> user info, & I'm not getting redirected to the login page, so there's
> no user info to match.
>
> Now, this is my first run at this, & frankly (& obviously, no doubt)
> I'm more than a bit baffled by the whole sessions framework business.
> Am I anywhere in the ballpark?  Any advice much appreciated!
>

Are you sure you are not logged in?  If you assign a local variable to the
value of request.user before trying the get then when you get the debug page
you can see the user value among the local variables.  If it's a
django.contrib.auth.models.AnonymousUser, then somehow you got past the
@login_required without logging in.  I think it's more likely you are logged
in and no Meetingtime matching your user exists.  (Also, are you sure you
want a get there?  Using get implies there will be exactly one Meetingtime
per user, otherwise the get will raise an exception.)

Karen

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

Reply via email to