Using login_required with direct_to_template?

278 views
Skip to first unread message

snew...@gmail.com

unread,
Jun 6, 2007, 12:12:45 AM6/6/07
to Django users
I'm trying to use the login_required decorator with the
direct_to_template generic view. I've read through the "Extending
Generic Views" section of DjangoBook (as recommended in a similar post
in this group) but I must be missing something.

Here's my best attempt:

urlpatterns = patterns('',
url(r'^$', 'django.views.generic.simple.direct_to_template',
{'template': 'main.html', 'login_required': 'True'}),
)

What am I missing?

Malcolm Tredinnick

unread,
Jun 6, 2007, 1:21:02 AM6/6/07
to django...@googlegroups.com

An explanation of what you expect to happen and what is actually
happening for a start, along with any error message.

How are your trying to use login_required in your template? Your initial
paragraph says you are trying to use the login_required *decorator*, but
that is something that applies to Python code, not inside a template.

Regards,
Malcolm

snew...@gmail.com

unread,
Jun 6, 2007, 12:09:50 PM6/6/07
to Django users
> An explanation of what you expect to happen and what is actually
> happening for a start, along with any error message.
>
> How are your trying to use login_required in your template? Your initial
> paragraph says you are trying to use the login_required *decorator*, but
> that is something that applies to Python code, not inside a template.

I'm sorry, my terminology was probably improper. I think should have
said I'm trying to use login_required as an argument for a generic
view.

What I'm trying to do is have a URL that uses direct_to_template to
send people to my application's main page. I want to require login to
view this page.

If I were doing it with a regular view, I'd just put the
login_required decorator above it. Since the page has no logic that
requires a view to be written, direct_to_template will work nicely.

The documentation says that CRUD generic views take an optional
argument of login_required.

Nathaniel Whiteinge

unread,
Jun 6, 2007, 3:03:39 PM6/6/07
to Django users
Until Malcolm swoops in with the answer to your question about the
docs, you can use callables in your urlconf to accomplish what you
want.

from django.contrib.auth.decorators import login_required
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
(r'^someurl/', login_required(direct_to_template), { 'template':
'template.html', }),
)

- whiteinge

snew...@gmail.com

unread,
Jun 6, 2007, 10:54:08 PM6/6/07
to Django users
> urlpatterns = patterns('',
> (r'^someurl/', login_required(direct_to_template), { 'template':
> 'template.html', }),
> )

That did it perfectly - thanks!

Reply all
Reply to author
Forward
0 new messages