Django 2 namespace and app_name - python

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

Related

How to solve urls not found issue in django

I am building an app using django and notice that some new urls that i have created, the list is provided below are not being found.
I read somewhere that the slug might be the reason django cannot found the urls but i am not sure
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(config("ADMIN_URL"), admin.site.urls),
path("", include("core.urls", namespace="core")),
]
application urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
app_name = 'core'
urlpatterns = [
path('index/', views.index, name='index'),
path('home/', views.home, name='home'),
path('home/<slug:slug>/', views.user_lessons_details, name='user-lessons-details'),
path('home/join-transaction/', views.user_lessons_join_transaction, name='user-lessons-join-transaction'),
path('home/start-transaction/', views.new_test_epoint, name='new-test-epoint'),
path('home/draft/<slug:slug>/', views.user_lessons_core_action, name='user-lessons-draft'),
path('teachers/lessons/create/', views.merchant_lessons_create, name='teachers-lessons-create'),
path('teachers/lessons/list/', views.teachers_lessons_list, name='teachers-lessons-list'),
path('teachers/lessons/<slug:slug>/', views.teachers_lessons_details, name='teachers-lessons-details'),
path('teachers/lessons/<slug:slug>/update/', views.teachers_lessons_update, name='teachers-lessons-update'),
path('teachers/lessons/<slug:slug>/delete/', views.teachers_lessons_delete, name='teachers-lessons-delete'),
path('teachers/drafts/create/', views.teachers_core_actions_create, name='teachers-drafts-create'),
path('teachers/drafts/list/', views.teachers_core_actions_list, name='teachers-drafts-list'),
path('teachers/drafts/<slug:slug>/', views.teachers_core_actions_details, name='teachers-drafts-details'),
path('teachers/drafts/<slug:slug>/update/', views.teachers_core_actions_update, name='teachers-drafts-update'),
path('teachers/drafts/<slug:slug>/delete/', views.teachers_core_actions_delete, name='teachers-drafts-delete'),
path('teachers/home/list/', views.teachers_home_list, name='teachers-home-list'),
path('teachers/home/<slug:slug>/', views.teachers_home_details, name='teachers-home-details'),
path('teachers/home/create/course/', views.teachers_home_transaction_quote, name='teachers-home-create-course'),
path('teachers/home/create/something/', views.teachers_home_create_transaction, name='teachers-home-create-something'),
path('teachers/home/create/fields-selection/', views.teachers_home_fields_selection, name='teachers-home-fields-selection'),
# ALL THE NEW URLS BELOW return NOT FOUND in django no matter what i do
path('teachers/home/account/settings/', views.teachers_settings, name='teachers-settings'),
path('teachers/account/settings/update-password/', views.teachers_settings_update_password, name='teachers-settings-update-password'),
path('teachers/account/payments/', views.teachers_payments, name='teachers-payments'),
path('teachers/account/payments/update/', views.teachers_payments_update, name='teachers-payments-update'),
path('teachers/account/analytics', views.teachers_analytics_stuff, name='teachers-analytics-stuff')
]
How can i possibly solve this urls not found issue in django?
Please dont mind the urls meaning

I can not run server Django 2.0

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

Django 2.0 | include multiple urls conf namespace

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

type error django include url... thing that should not be django1.11 python2.7

I am using djangos include feature in my main urls file, from my app urls file.
main urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^posts/', include('posts.urls')),
]
posts.urls.py
from django.conf.urls import url
from . import views #relative import to post views
urlpatterns = [
url(r'$',"views.posts_list" ), #list all posts
url(r'create/$',"views.posts_create" ),
url(r'detail/$',"views.posts_detail" ),
url(r'update/$',"views.posts_update" ),
url(r'delete/$',"views.posts_delete" ),
]
here is the error:
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
I have looked at the docs on this issue:
https://docs.djangoproject.com/en/1.11/ref/urls/#include
and the source code:
https://docs.djangoproject.com/en/1.11/_modules/django/conf/urls/#include
and I have no idea what I am doing wrong.
Please help. Hugs kisses, and high fives
Django 1.10+ no longer allows you to specify views as a string (e.g. 'myapp.views.index') in your URL patterns.
So you should config your posts urls.py like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.posts_list, name='list'),
url(r'^create/$', views.posts_create, name='create'),
url(r'^detail/$', views.posts_detail, name='detail'),
url(r'^update/$', views.posts_update, name='update'),
url(r'^delete/$', views.posts_delete, name='delete'),
]
It is also a good practice to add name to your urls.

<module 'myblog.urls' from 'path\\to\\python\\file\\urls.py'>' is not a callable or a dot-notation path

I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.

Categories