Saturday, September 6, 2014

try, except problem

I'm trying to write a try: except: to check for three things. First I want to check if an application has already been filled out, thus, check for existence. Second if it has been filled out a flag that is is complete is set to true. Then the else would be neither of those things, they need to create a new app.

This is what I have started with:

class Main(TemplateView):
    # app_started = Application.objects.get(created_by=request.user)
    @method_decorator(login_required(login_url='/accounts/login/'))
    def dispatch(self, *args, **kwargs):
        try:
            app = Application.objects.get(created_by=self.request.user)
            # if it is true check for if the app is set to complete
        except app.DoesNotExist:
            app = None
            return HttpResponseRedirect(reverse('requestform:registrant_create'))
        else:
            return HttpResponseRedirect(reverse('requestform:registrant_update'))

I keep getting this error:

local variable 'app' referenced before assignment


Why would app not be not be set if it is in the try statement?


How would I check if it does exist, to then check if the flag is true?


I'm sort of a Django newbie and this portion of the code I am writing is really mystifying me.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/261bf20e-c41e-4898-9c8a-8d3315388de6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment