try, except problem

40 views
Skip to first unread message

Brad Rice

unread,
Sep 6, 2014, 12:38:28 PM9/6/14
to django...@googlegroups.com
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.

Collin Anderson

unread,
Sep 6, 2014, 1:19:01 PM9/6/14
to django...@googlegroups.com
could you paste a traceback?

Andreas Kuhne

unread,
Sep 6, 2014, 1:19:57 PM9/6/14
to django...@googlegroups.com
Hi Brad,

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

Because the except parameter has the app variable in it (which it shouldn't). The correct except definition should be: "except Application.DoesNotExist". You are not looking for if the app variable doesn't exist but rather an object of type Application in the database.

2. See above :)

Med vänliga hälsningar,

Andréas Kühne
Software Development Manager
Suitopia Scandinavia AB


--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Brad Rice

unread,
Sep 6, 2014, 5:32:24 PM9/6/14
to django...@googlegroups.com
Andréas that was it. I guess I wasn't thinking about that correctly. I sure appreciate the help. This Django group is a very good group.

Andreas Kuhne

unread,
Sep 6, 2014, 5:38:26 PM9/6/14
to django...@googlegroups.com
Glad to be of help.

Regards,

Andréas

Reply all
Reply to author
Forward
0 new messages