This "problem" is quite related to : django 2.0 url.py include namespace="xyz"
The previous dev used Django 1.9 (or maybe even before) but we are now migrating to Django 2.0.
We got 3 sites on the same project, all with only 1 specific URLS Conf
### MAIN PROJECT REFERENCING ALL SITES ###
### Nothing changed here ###
from django.conf import settings
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path
from frontend import urls as frontend_urls
from search import urls as search_urls
from international import urls as international_urls
# Customisation admin
admin.site.site_header = 'INT - ADMIN'
temppatterns = [
# Admin Sites
url(r'^admin/', admin.site.urls),
# Organisation Sites
url(r'^frontend/', include(frontend_urls)),
# 1st Platform
url(r'^search/', include(search_urls)),
# 2nd Platform
url(r'^international/', include(international_urls)),
]
urlpatterns = temppatterns + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Here is the previous frontend URLs CONF
FRONTEND_PATTERNS = [
url(r'^conseiller$', views.GuidanceView.as_view(), name='guidance'),
.......
url(r'^contact$', views.ContactView.as_view(), name='contact'),
url(r'^$', views.HomeView.as_view(), name='home'),
]
COMPANY_PATTERNS = [
url(r'^companydetail/(?P<slug>[-\w]+)/$',
views.MemberLgView.as_view(),
name='lg-detail'),
url(r'^asso/orga/(?P<slug>[-\w]+)/$',
views.MemberOrgView.as_view(),
name='org-detail'),
]
CONTENT_PATTERNS = [
.......
]
EVENT_PATTERNS = [
.......
]
REDIRECT_PATTERNS = [
url(r'^actualite/(?P<pk>\d+)/(?P<slug>[-\w]+)/$',
views.OldBlogRedirectView.as_view(),
name='blog-redirect'),
.......
url(r'^ressources$',
RedirectView.as_view(
url=reverse_lazy('frontend:doc'), permanent=True)),
]
urlpatterns = [
url(r'^', include(FRONTEND_PATTERNS, namespace='frontend')),
url(r'^', include(COMPANY_PATTERNS, namespace='companies')),
url(r'^', include(CONTENT_PATTERNS, namespace='contents')),
url(r'^', include(REDIRECT_PATTERNS, namespace='redirects')),
url(r'^', include(EVENT_PATTERNS, namespace='events')),
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But now, the include with namespace seems to be deprecated. So we changed few things in this docs
# Added app_name
app_name="frontend"
# Deleting namespace - deprecated
urlpatterns = [
re_path(r'^', include(FRONTEND_PATTERNS)),
re_path(r'^', include(COMPANY_PATTERNS)),
re_path(r'^', include(CONTENT_PATTERNS)),
re_path(r'^', include(REDIRECT_PATTERNS)),
re_path(r'^', include(EVENT_PATTERNS)),
] + static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But now we got this error at 127.0.0.1:8000/frontend :
NoReverseMatch at /frontend/ 'companies' is not a registered namespace
on
'companies:%s-detail' % self.route_name, args=(self.slug, ))
Which is logic. So I tried to configure URLs like this
re_path(r'^', include(COMPANY_PATTERNS, namespace='companies')),
But got
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.
Reading the doc, it appears I should use
re_path(r'^', include((COMPANY_PATTERNS, app_name), namespace='companies')),
So let's go, but we are back to this :
NoReverseMatch at /frontend/ 'companies' is not a registered namespace
So I sat and tried to add frontend: before companies which work !
'frontend:companies:%s-detail' % self.route_name, args=(self.slug, ))
However, this is a really big big work of refactorisation, is there a better way to include URLs and set a dedicated namespace ?
Have you tried adding the namespace to the pattern lists?
REDIRECT_PATTERNS = ( [
url(r'^actualite/(?P<pk>\d+)/(?P<slug>[-\w]+)/$',
views.OldBlogRedirectView.as_view(),
name='blog-redirect'),
.......
url(r'^ressources$',
RedirectView.as_view(
url=reverse_lazy('frontend:doc'), permanent=True)),
] , 'redirects')
...
urlpatterns = [
re_path(r'^', include(REDIRECT_PATTERNS)),
...
I assume, this is what your first ImproperlyConfigured exception complains about.
There is another way (shown here in the answer of py_dude); both are explained in the docs.
For Django 2.0 this should look like:
temppatterns = [
# Admin Sites
path('admin/', admin.site.urls),
# Organisation Sites
path('frontend/', include('frontend.urls')),
# 1st Platform
path('search/', include('search.urls')),
# 2nd Platform
path('international/', include('international.urls')),
]
And so on...
Look at url() functions are replaced with path() and regexp strings replaced with strings
Did you read the docs?
Also, see the include() docs to know, what object could be passed to this function
Related
I am trying to include the urls of my custom admin site of one of my apps in a Django project. I would like the path to be foo-admin/.... I managed to do so by including this prefix in all urlpatterns in the app's urls.py. However if I try to add the prefix to the project's urls.py and have only the suffixes in the app's urls, it breaks. It's something like this:
This works:
Project's urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_app.urls')),
]
my_app/urls.py:
urlpatterns = [
path('foo-admin/', my_app_admin_site.urls),
path('foo-admin/foo/', views.some_view),
path('foo-admin/foo/<slug>/', views.MyGenericView.as_view()),
]
This doesn't work:
Project's urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('foo-admin/', include('my_app.urls')),
]
my_app/urls.py
urlpatterns = [
path('', my_app_admin_site.urls),
path('foo/', views.some_view),
path('foo/bar/<slug>/', views.MyGenericView.as_view()),
]
The error I'm encountering is:
NoReverseMatch at /foo-admin/
Reverse for 'app_list' with keyword arguments '{'app_label': 'my_app'}' not found. 1 pattern(s) tried: ['admin/(?P<app_label>auth|app1|app2|django_mfa|axes)/$']
I am having a difficult time understanding the connection between app_name and namespace.
consider the project level urls.py
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls', namespace='blog')),
]
consider the app level (blog) urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
]
if I comment out app_name, I get the following.
'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.
If I rename app_name to some arbitrary string, I don't get an error.
app_name = 'x'
I've read the documentation but its still not clicking. Can someone tell me how/why app_name and namespace are connected and why are they allowed to have different string values? Isn't manually setting app_name redundant?
try removing app_name='blog'
In your case you should be using:
'blog:post_list'
and
'blog:post_detail'
You can also remove the namespace='blog' in your first url like so:
urlpatterns = [
path('blog/', include('blog.urls')),
]
and then in your templates you can reference the urls without the 'blog:.....':
'post_list'
'post_detail'
try using tuple.
urlpatterns = [
path('blog/', include(('blog.urls', 'blog'))),
]
Hello I'm having a problem when trying to run server from an Django 2.0 old project.
this is the error:
File "/usr/lib/python3.7/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.'
this is my urls.py
from django.conf.urls import url, include
from django.contrib import admin
from apps.sysgrub.views import LoginUser, LogoutUser
app_name = 'apps'
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^alumno/', include ('apps.alumno.urls', namespace="alumno")),
url(r'^grupo/', include ('apps.grupo.urls', namespace="grupo")),
url(r'^maestro/', include ('apps.maestro.urls', namespace="maestro")),
url(r'^rol/', include ('apps.rol.urls', namespace="rol")),
url(r'^sysgrub/', include ('apps.sysgrub.urls', namespace="sysgrub")),
url(r'^$', LoginUser),
url(r'^logout/$', LogoutUser),
]
and my urls by app
urlpatterns = [
url(r'^index$', index),
url(r'^horarios$', horarios),
url(r'^listar$', Alumno_grupoList.as_view(), name='alumno_listar'),
url(r'^agregar$', AlumnoCreate.as_view(), name='add_student'),
]
thank you so much for your help greetings.
The error describes the resolution please remove app name from the urls.py as below
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^alumno/', include ('alumno.urls', namespace="alumno")),
url(r'^grupo/', include ('grupo.urls', namespace="grupo")),
]
I have just started with django now and was configuring urls. I am able to map different urls like /posts, /posts/create etc. Somehow I am not able to configure root url, I am not sure what wrong I am doing. Here is my configuration :
urlpatterns = [
# Examples:
url(r'',homeViews.posts),
# url(r'^blog/', include('blog.urls')),
url(r'^posts/',homeViews.posts),
url(r'^createPost/',homeViews.createPost),
url(r'^createUser/',userViews.createUser),
url(r'^post/(?P<title>[a-z,A-Z]+)/$',homeViews.post),
]`
Cheers!
here is an example, my friend.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'home.views.home',name='index'),
url(r'^contactanos/$', 'home.views.contacto',name='contacto'),
url(r'^post/$', 'home.views.blog',name='blog')
]
I'm new to Django and I currently have two problems that I can't figure out from reading online:
URLs... I have an app 'cq' and the project is 'mysite' here is what I have in mysite's urls.py
urlpatterns = [
url(r'^cq/', include('cq.urls')),
url(r'^admin/', include(admin.site.urls)),
]
and this is what I have in cq's urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<team_id>[0-9]+)/$', views.team, name='team'),
]
However I don't want do to cq/team_id. I just want to be able to go directly to /team_id. Is there any way to do it?
I want an entry in the admin view of the database to look very custom, i.e. I want to write my own html.. How do I do it?
Thanks!
When you define
url(r'^cq/', include('cq.urls')),
it means all urls in cq.urls will have to start with cq/. For you case, just do
urlpatterns = [
url(r'^cq/', include('cq.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<team_id>[0-9]+)$', views.team, name='team') #without trailing slash for /team_id, but with for /team_id/
]
Of course, you don't need this rule anymore in cq urls.py