include ()에서 네임 스페이스를 사용할 때 app_name에 대한 ImpropyConfiguredError
나는 현재 장고를 시험하고 있습니다. urls.py namespace
의 내 include()
s 중 하나 에서 인수를 사용합니다. 서버를 실행하고 찾아 보려고하면이 오류가 발생합니다.
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include
'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
다음은 내 urls.py 파일입니다.
#project/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^reviews/', include('reviews.urls', namespace='reviews')),
url(r'^admin/', include(admin.site.urls)),
]
과
#app/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /
url(r'^$', views.review_list, name='review_list'),
# ex: /review/5/
url(r'^review/(?P<review_id>[0-9]+)/$', views.review_detail, name='review_detail'),
# ex: /wine/
url(r'^wine$', views.wine_list, name='wine_list'),
# ex: /wine/5/
url(r'^wine/(?P<wine_id>[0-9]+)/$', views.wine_detail, name='wine_detail'),
]
app_name
오류 메시지에 명시된대로 무엇을 통과 합니까?
당신이 한 일은 포함 할 매개 변수를 전달하는 허용 가능한 방법이 아닙니다. 다음과 같이 할 수 있습니다.
url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')),
Django 1.11 이상, 2.0 이상
포함하고있는 URL 파일에 app_name을 설정해야합니다.
# reviews/urls.py <-- i.e. in your app's urls.py
app_name = 'reviews'
그런 다음 수행하는 방식으로 포함 할 수 있습니다.
또한 Django 문서가 https://docs.djangoproject.com/en/1.11/ref/urls/#include에서 말하는 내용에 주목할 가치가 있습니다 .
버전 1.9부터 폐지 : app_name 인수에 대한 지원이 폐지되었으며 Django 2.0에서 제거됩니다. URL 네임 스페이스에 설명 된대로 app_name을 지정하고 대신 URLconf를 포함합니다.
( https://docs.djangoproject.com/en/1.11/topics/http/urls/#namespaces-and-include )
장고 2.0은 사용자가 지정해야 APP_NAME을 당신의 urls.py 포함에 APP_NAME 인수를 지정할 필요가 없습니다.
기본 URL 파일.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('apps.main.urls')),
path('admin/', admin.site.urls),
]
포함 된 URL.
from django.urls import path
from . import views
app_name = 'main_app'
urlpatterns = [
path('', views.index, name='index'),
]
그런 다음 템플릿에서 사용
<a href="{% url main_app:index' %}"> link </a>
자세한 내용 : https://code.djangoproject.com/ticket/28691 Django 2.0 문서
아직 (완전히) django 2.1과 호환되지 않는 라이브러리 (django_auth_pro_saml2)를 포함했습니다. 따라서 두 번째 파일을 만듭니다 saml_urls.py
.
from django_saml2_pro_auth.urls import urlpatterns
app_name = 'saml'
다음과 같이 URL을 포함 할 수 있습니다.
from django.urls import include, re_path as url
urlpatterns = [
..., url(r'', include('your_app.saml_urls', namespace='saml')), ...
]
Hacky, but it worked for me, whereas the url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews'))
did not.
I am also face the same error in Django 2.2 and i solve it this way
urls.py file
urlpatterns = [
path('publisher-polls/', include('polls.urls', namespace='publisher-polls')),
]
polls/urls.py file
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index')
]
example use of namespace in calss based view method
def get_absolute_url(self):
from django.urls import reverse
return reverse('polls.index', args=[str(self.id)])
example use of namespace in templates
{% url 'polls:index' %}
Here polls:index mean app_name[define in polls/urls.py file]:name[define in polls/urls.py file inside path function]
their official which is pretty good you can check for more info namespace_django_official_doc
In my case, I was writing the urls outside the urlpatterns list. Please double check.
'Nice programing' 카테고리의 다른 글
ASP.NET MVC / WebAPI 응용 프로그램에서 HTTP OPTIONS 동사를 지원하는 방법 (0) | 2020.10.13 |
---|---|
Dataframe을 csv에 s3 Python에 직접 저장 (0) | 2020.10.13 |
HTTP 범위 헤더 (0) | 2020.10.13 |
인라인 변수로 여러 줄 Python 문자열을 어떻게 생성합니까? (0) | 2020.10.13 |
포함 옵션을 사용하여 특정 유형의 파일에만 rsync 복사 (0) | 2020.10.13 |