title: Comparing url path in Django 2 middleware
body: <p>I need to develop a Django middleware and created class like this:</p>
  
  <pre><code>import re
  from django.conf import settings
  from django.shortcuts import redirect
  from django.contrib.auth import logout
  
  
  class LoginRequiredMiddleware:
  
      def __init__(self, get_response):
          self.get_response = get_response
  
      def __call__(self, request):
          response = self.get_response(request)
          return response
  
      def process_view(self, request, view_func, view_args, view_kwargs):
          assert hasattr(request, 'user')
  
          path = request.path_info
          print(path)
  
          p = path.lstrip('/')
          compiled_login_url = re.compile(settings.LOGIN_URL)
  
          print(compiled_login_url.match(p))
  
  
          if not request.user.is_authenticated:
              if path == '/':
                  print("==============&gt; INDEX PAGE")
  </code></pre>
  
  <p>Now here, I want to compare the path of urls. I have to determine if path match with <strong>settings.LOGIN_URL</strong> or not. I have tried using regular expressions, but they did no help.</p>
  
  <p>Here are my urls</p>
  
  <pre><code>urlpatterns = [
                path('admin/', admin.site.urls),
                path('', views.index, name='index'),
                path('&lt;slug:slug&gt;/', include('app.urls')),
                path('&lt;slug:slug&gt;/accounts/login/', views.login, name='login'),
                path('&lt;slug:slug&gt;/accounts/logout/', views.logout, name='logout'),
                path('&lt;slug:slug&gt;/dashboard/', 'views.dashboard', name='dashboard'),
            ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  </code></pre>
  
  <p>I want to exempt <strong>login</strong> and <strong>logout</strong> from being checked in middleware also I need <strong>slug</strong>.</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>def user_login(request, slug):
      """
      Login user using email and password
  
      :param request: WSGI Request
      :param slug: to verify the facility
      :return: login page or redirect
      """
  
      context = {}
      try:
          facility = models.Facility.objects.get(slug=slug)
          context['facility'] = facility
  
      except models.Facility.DoesNotExist:
          return redirect('index')
  
      if request.method == "POST":
          email = request.POST['email']
          password = request.POST['password']
  
          user = authenticate(request, username=email, password=password)
          if user is not None:
              if user.profile.facility.slug == slug:
                  login(request, user)
                  return redirect('dashboard', slug=slug)
              else:
                  context['error'] = "You don't belong to this facility"
  
          else:
              context['error'] = "Invalid email or password"
  
      return render(request, 'app/user/login.html', context)
  
  
  @login_required
  def dashboard(request, slug):
      """
      Display the dashboard of facility and list down bird
      view for the daily, weekly, monthly and yearly progress
  
      :param request:
      :return:
      """
      context = {}
  
      try:
          facility = models.Facility.objects.get(slug=slug)
          context['facility'] = facility
      except models.Facility.DoesNotExist:
          return render(request, 'app/404.html', status=404)
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;ul&gt;
      {% for f in facilities %}
          &lt;li&gt;&lt;a href="{% url 'login' f.slug %}"&gt;{{ f.name }}&lt;/a&gt;&lt;/li&gt;
      {% empty %}
          &lt;li&gt;No Facility&lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>