簡體   English   中英

Django 嘗試了這些 URL 模式:空路徑與其中任何一個都不匹配

[英]Django tried these URL patterns: The empty path didn’t match any of these

我收到這個錯誤

請求方法:GET 使用 Webwork.urls 中定義的 URLconf,Django 按以下順序嘗試了這些 URL 模式:

 admin/ ^ ^department/$ ^ ^department/([0-9]+)$ ^ ^employee/$ ^ ^employee/([0-9]+)$

空路徑與其中任何一個都不匹配。

這是我的代碼:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include    
urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^',include('EmpApp.urls'))
]

from django.conf.urls import url
from EmpApp import views
urlpatterns=[
    url(r'^department/$',views.departmentApi),
    url(r'^department/([0-9]+)$',views.departmentApi),
    url(r'^employee/$',views.employeeApi),
    url(r'^employee/([0-9]+)$',views.employeeApi),
    
]

誰能幫我解決這個錯誤?

你沒有任何錯誤這是你沒有為http://127.0.0.1:8000這個路徑創建任何頁面

使用路徑而不是我在代碼中顯示的url

from django.contrib import admin
from django.urls import path
from django.conf.urls import  include

urlpatterns = [
    path('',include('app.urls')),
    path('admin/', admin.site.urls),
]

from django.urls import path
from app import views
urlpatterns=[
    path('',views.index), # added this line 
    path('departments',views.departmentApi),
    path('departments/<slug:id>',views.departmentApi)
]

並將代碼添加到EmpAppapp\views.py文件

def index(request):
    return render(request, './index.html')

現在在EmpAppapp\templates\index.html中創建index.html文件並在http://127.0.0.1:8000/頁面上寫任何你想顯示的內容

<h1>hello</h1>

如果你得到ImportError: cannot import name 'url' from 'django.conf.urls'這個錯誤比參考這個頁面ImportError: cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM