Django URL Configuration: Fixing Import Conflicts and URL Pattern Issues
This code snippet demonstrates a common problem encountered in Django URL configurations: import conflicts and incorrect URL patterns.
The primary issue lies in the conflicting import of the admin module from both django.contrib and app02. This leads to ambiguity and potential errors. To resolve this, we can rename the admin import from app02 to app02_admin:
from app02 import admin as app02_admin
Secondly, the url function is not correctly imported from django.conf.urls. To fix this, we need to import url from django.urls:
from django.urls import path, re_path as url
The corrected code incorporates these changes:
from django.contrib import admin
from django.urls import path, re_path as url
from app01 import views
from app02 import admin as app02_admin
urlpatterns = [
path('index/', views.index),
path('index/userlist', views.userlist),
path('index/useradd', views.useradd),
path('info/list/', views.infolist),
path('sqltest/index1/', views.sqltest1),
url(r'^admin/', app02_admin.site.urls),
]
In this revised code, the import statements are fixed, and the admin.site.urls is replaced with app02_admin.site.urls to specify the admin site from app02. This ensures that the code is error-free and clearly defines the URL patterns for your application.
原文地址: https://www.cveoy.top/t/topic/qtMc 著作权归作者所有。请勿转载和采集!