gpt4 book ai didi

带有登录装饰器的 Django 测试框架 - 它们有效吗?反向错误

转载 作者:行者123 更新时间:2023-11-28 19:56:22 27 4
gpt4 key购买 nike

我正在使用 Django 测试框架(它很有用,但感觉笨拙和笨拙)。测试一直失败,回溯让我相信这是登录装饰器的问题。以下是测试、错误和相关代码:

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

真正奇怪的是关键字参数被插入到请求中,让我认为这是一个模板请求。

编辑:更多代码

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.models 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)

这就是整个 View 。

这是一个巨大的帖子,所以模板在这里:http://dpaste.com/110860/

最佳答案

那里报告的错误日志告诉您大部分您需要知道的内容:

首先你有:

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.

它在尝试反转模板中的 url 时引发异常,这意味着您使用 View 函数的路径和参数“this_week”在某处调用了 {% url %} 标记,但这不是根据您的 URLconf 调用 View 函数的合适方式。

修正你的 {% url %} 标签或你的 View 函数定义,它就会工作。

关于带有登录装饰器的 Django 测试框架 - 它们有效吗?反向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1610093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com