开发者

Django Testing Framework with login decorators - do they work? Error with reverse

开发者 https://www.devze.com 2022-12-09 09:39 出处:网络
I am using the Django testing framework (which is useful, but feels clunky and awkward). A test keeps failing, and the traceback leads me to believe it\'s an issue with the login decorators. Here are

I am using the Django testing framework (which is useful, but feels clunky and awkward). A test keeps failing, and the traceback leads me to believe it's an issue with the login decorators. Here are the tests, the error, and the relevant code:

class TestMain(TestCase):
    fixtures = ['timetracker']

    def test_login(self):
        c = Client()
        login = c.login(username='testclient', password='not.a.real.password')
        self.failUnless(login, 'Could not log in')

    def test_main(self):
        c = Client()
        login = c.login(username='testclient', password='not.a.real.password')
        self.failUnless(login, 'Could not log in')

        response = c.get('/', follow=True)
        print response.content
        #assert response.status_code == 200, response.status_code




markov:biorhythm vinceb$ nosetests -v --with-django
test_login (biorhythm.timetracker.tests.test_urls.TestMain) ... ok
test_main (biorhythm.timetracker.tests.test_urls.TestMain) ... ERROR

======================================================================
ERROR: test_main (biorhythm.timetracker.tests.test_urls.TestMain)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/vinceb/Code/python/biorhythm/timetracker/tests/test_urls.py", line 20, 

in test_main
        response = c.get('/', follow=True)
      File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/test/client.py", line 281, in get
 [...]
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py", line 792, in render_node
        return node.render(context)
      File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/defaulttags.py", line 382, in render
        raise e
    NoReverseMatch: Reverse for '<django.contrib.auth.decorators._CheckLogin object at 0x22d4650>' with arguments '()' and keyword arguments '{'report_type': u'this_week'}' not found.

----------------------------------------------------------------------
Ran 2 tests in 1.112s

FAILED (errors=1)
Destroying test database...
markov:biorhythm vinceb$ 






@login_required
def time(request):
    # initials
    from biorhythm.timetracker.forms import TimeForm, TimeFormSet
    from django.forms.formsets import formset_factory

    # instantiate our formset factory
    TimeSet = formset_factory(TimeForm, extra=1)
    formset = None

    # sorting worklogs
    order_by = ordered(request)
    success = None

What's really strange is that keyword args being inserted into the request, leading me to think it's a template request.

EDIT: more code

urlpatterns = patterns('biorhythm.timetracker.views',

    # Home
    url(r'^/?$', 'time', name='home'),

    # CSV exports
    url(r'^reports/csv/(?P<from_date>\d{8})/(?P<to_date>\d{8})/?$', 'export_csv_report', name='csv_out'),   
    url(r'^dashboard/csv/?$', 'export_qbcsv', name='csv_report'),

    # Reports
    url(r'^summary/?$', 'reports', name='reports'),
    (r'^summary/(?P<from_date>\d{8})/(?P<to_date>\d{8})/?$', 'reports'),
    (r'^summary/(?P<report_type>.*?)/?$', 'reports'),

    # test
    (r'new_dashboard/$', 'new_dashboard'),
    url(r'remove_query/(?P<position>.*?)/$', 'remove_query', name='remove_query'),


    # Aggregate timesheets
    url(r'^dashboard/$', 'new_dashboard', name='dashboard'),
    url(r'old_dashboard/$', 'dashboard', name='old_dashboard'),
    (r'^dashboard/(?P<user_id>.*?)/?$', 'dashboard'),
    (r'^dashboard/(?P<from_date>\d{8})/(?P<to_date>\d{8})/?$', 'dashboard'),
)



@login_required
def time(request):
    # initials
    from biorhythm.timetracker.forms import TimeForm, TimeFormSet
    from django.forms.formsets import formset_factory

    # instantiate our formset factory
    TimeSet = formset_factory(TimeForm, extra=1)
    formset = None

    # sorting worklogs
    order_by = ordered(request)
    success = None

    if request.method == 'POST':
        from django.contrib.admin.models import LogEntry, ADDITION
        from django.contrib.contenttypes.mod开发者_JAVA技巧els import ContentType
        # we have to make sure we have a matching contenttype for this model
        # normally we will, but first entry may not have this
        worklog, created = ContentType.objects.get_or_create(name='worklog', app_label='timetracker', model='worklog')

        # pertinent fields
        dats = 'project duration note category start_date'.split()

        # instantiate formset
        formset = TimeSet(request.POST)
        for i, form in enumerate(formset.forms):
            labs = ['form-%d-%s' % (i, d) for d in dats]
            project, duration, note, category, start_date = [request.POST.get(l, None) for l in labs]
            check_against = [project, duration, note, start_date] 

            # Checks that required fields have been filled with input longer than 0.
            if 0 not in [len(x) for x in check_against]:
                if form.is_valid():
                    project, duration, note, category, start_date = [form.cleaned_data.get(l, None) for l in dats]

                    if None not in check_against:
                        # this is a form we can process & save so let's
                        # get our pk for logging activity
                        instance = form.save(commit=False)
                        if instance:
                            # default values and save again
                            instance.start_date = form.cleaned_data.get('start_date')
                            instance.user = request.user
                            instance.save()

                            # Output a nice message to the client
                            s = 's' if duration > 1 else '' # pluralization!
                            message = "%s hour%s spent %s on %s" % (duration, s, note, project.name)
                            request.user.message_set.create(message=message)

                            # Append this action (addition) to the LogEntry table
                            _send_LogEntry(request.user.pk, worklog.id, worklog.id,
                                           force_unicode(project), message)


                            success = True
                else:
                    # Form _not_ valid. Continue outputting the formset with error messages.
                    pass
            else:
                # Form missing required field
                continue  
        if success is True:
            return http.HttpResponseRedirect(request.get_full_path())

    # get our data
    worklogs = Worklog.objects.select_related().filter(user=request.user.id).order_by(order_by, '-id')
    time_week = worklogs.filter(start_date__gte=start_of_week()).aggregate(Sum('duration'))
    time_day = worklogs.filter(start_date__gte=today()).aggregate(Sum('duration'))
    #time_week = worklogs.filter(start_date__gte=start_of_week()).objects.objects.get_total_time()
    #time_day = worklogs.filter(start_date__gte=today()).objects.get_total_time()

    reports = return_time_report()

    # instantiate projects for initial data for form for the last 14 days
    # was 30 days, now 14 at Nik's request -J
    month = datetime.timedelta(days=14)
    worklog_projects = worklogs.filter(start_date__gte=today()-month)
    projects = dict.fromkeys([p.project for p in worklog_projects]).keys()

    if not formset:
        initial_data = [{'project':p.id, 'start_date':'today'} for p in projects]
        formset = TimeSet(initial=initial_data)
    has_logs = True if int(worklogs.count()) > 0 else False
    logs = paginate(request, worklogs, 'worklogs')
    logs_list = logs.object_list


    # Initialize categories from a query object, so they can be sent to JQuery
    # via JSON to make auto-complete work.
    cat_list = []
    for c in Category.objects.all():
        cat_list.append(dict(id=str(c.id), name=c.name))
    cats = json.dumps(cat_list)


    context = RequestContext(request)
    return render_to_response('log.html', {'formset':formset, 'worklogs':logs_list, 'v':locals(), 't':reports}, context)

That's the entire view.

This is a huge post, so the template is here: http://dpaste.com/110860/


The error log reported there tells you most of what you need to know:

First you've got:

in test_main
        response = c.get('/', follow=True)

Which means it's dying while trying to process that request. Next:

      File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/test/client.py", line 281, in get
 [...]
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/__init__.py", line 792, in render_node
        return node.render(context)

It's dying while rendering a node in the template, which means it's getting to the template, not dying at the login. Last:

      File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/defaulttags.py", line 382, in render
        raise e
    NoReverseMatch: Reverse for '<django.contrib.auth.decorators._CheckLogin object at 0x22d4650>' with arguments '()' and keyword arguments '{'report_type': u'this_week'}' not found.

It's raising an exception while trying to reverse a url in your template, which means you called the {% url %} tag somewhere with the path to your view function and the argument "this_week", but that's not the appropriate way to call your view function according to your URLconf.

Fix your {% url %} tag or your view function definition and it'll work.


Did you check your regex's in urls.py? Maybe the are bogus.

Also check this Bug Ticket Reverse for '<django.contrib.auth.decorators._CheckLogin object at 0x8ee144c>' not found

Btw. this is what the django source-code says around line 382 in defaulttags.py

# Try to look up the URL twice: once given the view name, and again
# relative to what we guess is the "main" app. If they both fail,
# re-raise the NoReverseMatch unless we're using the
# {% url ... as var %} construct in which cause return nothing.


Do you have LOGIN_URL defined in your settings.py file?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号