title: Django: "How to get the slug from url() before passing it to MyClass.as_view()?" or "How to check if registration is open?"
body: <p>I want to check if a user is allowed to register for a specific event. I thought in order to save code I could do it like the <code>login_required</code> decorator right between the <code>url()</code> and <code>MyClass.as_view()</code>. But how do I get that slug? Or is this solution totally wrong? (I unfortunatelly can't use the <code>user_passes_test</code> because I don't want to test someting on the user but on the url.)</p>
  
  <p>So I tryed this:</p>
  
  <p>views.py</p>
  
  <pre><code>from registration.models import Event
  from django.utils import timezone
  from django.contrib.auth.decorators import login_required
  from django.shortcuts import get_object_or_404
  from django.http import HttpResponseRedirect
  from django.core.urlresolvers import reverse
  
  def reg_is_open(event_slug):
      """
      Return True if registration is open.
      """
      event = get_object_or_404(Event, slug=event_slug)
      if event.open_date &lt;= timezone.now() and event.cut_off &gt;= timezone.now():
          return True
  
  def allow_view(cls, **initkwargs):
      """
      Check weather registration is open and user is logged in.
      Returns to registration start page if registration is closed.
      """
      slug = initkwargs.get('event') # Does not work!
      if not reg_is_open(slug):
          return HttpResponseRedirect(reverse('registration:event_index', args=slug))
      return login_required(cls.as_view(**initkwargs))
      # Also works when I remove **initkwargs. That means that what I'm looking for just passes...
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>from django.conf.urls import patterns, url, include
  from registration import views
  
  event_patterns = patterns('', 
      url(r'^person/$', views.allow_view(views.PersonList), name='person_list'), 
      # instead of 
      # url(r'^person/$', login_required(views.PersonList.as_view()), name='person_list'),
      # ...
  
  urlpatterns = patterns('',
      url(r'^(?P&lt;event&gt;[-a-zA-Z0-9_]+)/$', views.EventDetails.as_view(), name='event_index'),
      url(r'^(?P&lt;event&gt;[-a-zA-Z0-9_]+)/', include(event_patterns)),
      # ...
  </code></pre>