【问题标题】:how to make sure a certain django url path is only checked if all the other paths have been checked如何确保仅在检查了所有其他路径后才检查某个 django url 路径
【发布时间】:2022-11-11 13:35:58
【问题描述】:

我正在构建一个应用程序,用户可以通过一个简单的 slug 访问他们的档案,如下所示:

lekha.cc/<archive_slug>

这与 instagram 完全一样。但是,每当我转到任何其他页面时,例如

lekha.cc/dashboard

存档视图的代码运行,说它没有找到带有该 slug 的存档。这是一个问题有两个原因:我们不希望运行任何多余的代码,如果用户选择将他们的存档命名为“仪表板”,则整个网站可能会崩溃,因为没有人能够访问他们的仪表板。

我的 urls.py 文件夹如下:

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'),
]

有人对此问题有任何解决方案吗?

编辑:

这是仪表板视图的代码

    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()})

和存档视图

    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})

和仪表板模板:

<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>

【问题讨论】:

  • OP 可以显示仪表板视图及其模板的代码吗?

标签: django django-views


【解决方案1】:

该问题是由浏览器请求 /favicon.ico/ django 正在通过我的视图引起的。理论上,url.py 文件应该按顺序查看所有 URL,直到找到正确的视图,因此默认情况下 django 只检查一次 URL。

真正的问题进一步描述here

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2014-03-09
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多