how to make sure a certain django url path is only checked if all the other paths have been checked

Viewed 20

I am building an app where users can access their archives through a simple slug, as follows:

lekha.cc/<archive_slug>

This is exactly as instagram does it. However, whenever I go to any other page, such as

lekha.cc/dashboard

The code for the archive view runs, saying that it has not found an archive with that slug. This is an issue for 2 reasons: we dont want any excess code to run, and if a user chooses to name their archive 'dashboard', the entire website would potentially break down since no one would be able to access their dashboard.

My urls.py folder is as follows:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main.urls'), name='index'),
    path('onboarding/', account_views.onboarding, name='onboarding'),
    path('register/', account_views.register, name='register'),
    path('login/', auth_view.LoginView.as_view(authentication_form=LoginForm, template_name='accounts/login.html'), name="login"),
    path('logout/', account_views.logout_view, name='logout'),
    path('dashboard/', archival_views.dashboard, name='dashboard'),
    path('account_settings/', account_views.account_settings, name='account_settings'),
    path('<str:slug>/', main_views.archive, name='archive'),
    path('item/<str:slug>/', main_views.work, name='work'),
]

Does anyone have any solutions to this issue?

EDIT:

Here is the code for the dashboard view

    def dashboard(request):
    user = get_current_user()
    archive = Archive.objects.get(creator=user)
    filesystem = Folder.objects.get(archive=archive)

    if request.method == "POST":
        if 'addCategory' in request.POST:
            category_name = request.POST['folderName']
            filesystem = Folder.objects.get(pk=filesystem.pk)
            filesystem.add_child(name=category_name)
            
    
    return render(request, "archival/dashboard.html", {'filesystem': filesystem, "archve": archive, "fileSystemParse": filesystem.get_annotated_list()})

And the archive view

    def archive(request, slug):
    # retrieve archive with the corresponding slug requested (lekha.cc/dhruva will return the archive with slug='dhruva')
    archive = Archive.objects.get(archive_slug=slug)
    filesystem = Folder.objects.get(archive=archive)
    return render(request, 'archive.html', {'archive': archive, 'filesystem': filesystem})

And the dashboard template:

<html lang="en">
  <head>
    <style>        
    </style>
  </head>
</html>
{% extends 'navbar.html' %} 

{% block content %}
{% load static %}

<div style="height: 200px; width: 100%;"></div>

<p>
  archive: {{ archive.archive_slug }}, filesystem: {{ filesystem.name }}
</p>

<div id="folder_view">
  {% include 'partials/folder_view.html' %}
</div>

<input type="button" value="addFolder">
<input type="button" value="addFile">


<form action="/dashboard/" method="post">
  {% csrf_token %}
  <input type="text" name="folderName">
  <input type="submit" value="Add Category" name="addCategory">
</form>

<!-- Popups -->

<div id="new_folder_popup" class="dashboard-popup">
  <div class="dashboard-popup-content">
    <span class="close">&times;</span>
    <!-- <form action="/dashboard/" method="post">
      {% csrf_token %}
      <input type="text" name="folderName">
      <input type="submit" value="Add Category" name="addCategory">
    </form> -->
  </div>
</div>
0 Answers
Related