title: Allowing URL access only to logged-in users
body: <p>I am getting started with the django framework and I created a simple website where the user first logs in and then is redirected to another page.</p>
  
  <p>I tried typing the URL of the redirected page before logging in, and it loads. Instead, I want it to redirect back to the login page (home page).</p>
  
  <p>I tried checking if the user is authenticated to view the page only if it is. It is written in the <code>views.py</code> file of the redirected page:</p>
  
  <pre><code>def index(request):
      if request.user.is_authenticated():
          return render(request, 'professors/index.html')
      else:
          return redirect('' % request)
  </code></pre>
  
  <ul>
  <li><code>professors/</code> is the URL of the redirected page</li>
  <li><code>''</code> is the URL of the login page (home page)</li>
  </ul>
  
  <p><strong><em>Update</em></strong></p>
  
  <p><code>views.py</code>:</p>
  
  <pre><code>from django.shortcuts import render, redirect
  from django.contrib.auth.decorators import login_required
  
  # Create your views here.
  @login_required
  def index(request):
      return render(request, 'professors/index.html')
  </code></pre>
  
  <p><code>urls.py</code>:</p>
  
  <pre><code>from django.urls import path
  from . import views
  
  urlpatterns = [
      path(r'', views.index, name='index')
  ];
  </code></pre>