Provide the namespace argument to include() instead on django 3.0 - django

In Django 3.0 this gives the error:
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.
How should I change url(r'^admin/', include(admin.site.urls))? I tried to look at the documentation,
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', include(myapp.views)),
]

remove include from admin urls and use path
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('myapp.views')),
]
refer this django 3 doc.

The problem is with include(myapp.views):
First you should include a string (include('myapp.views')), not the actual module. That's what is causing the error.
But second, you don't include views, you include other url patterns. So unless myapp.views contains a top-level urlpatterns, you'll get an error as well.
Finally, as #c.grey pointed out, use path(), not url().

Related

Path error in django rest framework : Specifying a namespace in include() without providing an app_name is not supported

In my django rest framework app I have Urls.py as follows:
from django.urls import include, path
from .shipper import*
from .views import *
urlpatterns = [path('shipper/',
include(path('shipperitems/', MasterItemsList.as_view()),
path('shippercreateapi/', shipperCreateAPIView.as_view()),)),
]
When I try to run the app it gives me the following error:
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 t he list of patterns and app_name instead.
what should I do to resolve this ?
You are using the include(...) function in the wrong way. include(...) usually takes the module which contains url-patterns. So change your code as below,
#root urls.py
from django.urls import include, path
urlpatterns = [
path('shipper/', include('shipper.urls'),
]
and
#/shipper/urls.py
urlpatterns = [
path('shipperitems/', MasterItemsList.as_view()),
path('shippercreateapi/', shipperCreateAPIView.as_view()),)),
]
Check the docs for include here. You should pass paramters to include like this
url(r'^reviews/', include('app_name.urls', 'app_name'), namespace='app_name')),
(credits)

Specifying a namespace in include() without providing an app_name ' django.core.exceptions.ImproperlyConfigured

from django.urls import path
from django.conf.urls import include, url #22.JUN.2018 #25.Jun.2018
from django.contrib import admin
#from bookmark.views import BookmarkLV, BookmarkDV
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^bookmark/',include('bookmark.urls', namespace='bookmark')),
url(r'^blog/', include('blog.urls', namespace='blog')),
I need you guys help!!!
This is my code. And i have a error....please help me....
'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.
You have to add a variable called app_name in the included urls.py module.
For example if you have this include in your project urls.py:
url(r'^bookmark/',include('bookmark.urls', namespace='bookmark'))
you have to add a variable:
app_name = 'bookmark'
just before the definition of urlpatterns variable in bookmark/urls.py file.
I didn't know where you i write app_name=blog.
However, i got it
Solution is going into the app file ex)/blog/urls.py
and then write app_name='blog'
$ cd /home/꾸르잼/Django/mysite/bookmark
$ vi urls.py
from django.conf.urls import url
from bookmark.views import BookmarkLV, BookmarkDV
app_name='bookmark'
urlpatterns = [
# Class-based views
url(r'^$', BookmarkLV.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', BookmarkDV.as_view(), name='detail'),
]
If you have a same problem like me, hopefully you can solve it as watching this.
Have a good day

How to register DRF router url patterns in django 2

My DRF routers specify a namespace so that I can reverse my urls:
urls.py:
router = DefaultRouter()
router.register('widget/', MyWidgetViewSet, base_name='widgets')
urlpatterns =+ [
url(r'/path/to/API/', include(router.urls, namespace='widget-api'),
]
Which, when upgrading to django 2, gives:
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.
Django 2 now requires app_name if the namespace kwarg is specified when using include. What's the right way to specify app_name when the url patterns are constructed by a DRF url router? I don't think the documentation is up-to-date for django 2 on this subject.
You need to put app_name = 'x' in your application's url.py file. This is a little buried in the documentation:
https://docs.djangoproject.com/en/2.0/topics/http/urls/#id5
For example, if in /project/project/urls.py you have:
path('', include('app.urls', namespace='app'))
Then in the corresponding url file (in /project/app/urls.py) you need to specify the app_name parameter with:
app_name = 'app' #the weird code
urlpatterns = [
path('', views.index, name = 'index'), #this can be anything
]
It's just necessary to use '{basename}-list' in reverse function.
In your case, it's going to be: reverse('widgets-list')
You need to include the router.urls as a tuple and add the app name to the tuple instead of only include router.urls
According to your example you should try with something like:
router = DefaultRouter()
router.register('widget/', MyWidgetViewSet, base_name='widgets')
urlpatterns =+ [
url(r'/path/to/API/', include((router.urls, 'my_app_name'), namespace='widget-api'),
]
The recommended approach is
from django.conf.urls import url, include
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'widget/', MyWidgetViewSet)
urlpatterns = [
url(r'^path/to/API/', include('rest_framework.urls', namespace='widget-api'))
]
See http://www.tomchristie.com/rest-framework-2-docs/tutorial/quickstart#urls

In django 1.10, how do I handle my urls now that patterns is deprecated?

from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r"^$", home),
url(r"^storefront/", storefront),
url(r"^sell/", get_entry),
.
ImportError: cannot import name patterns
The above is a snippet of my urls.py, is fixing this just a matter of changing my import statement or will I literally need to rewrite my entire urls.py now that the patterns module has been deprecated?
In django 1.10 the urls can be defined in the following way:-
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = i18n_patterns(
url("^admin/", include(admin.site.urls)),
)
if settings.USE_MODELTRANSLATION:
urlpatterns += [
url('^i18n/$', set_language, name='set_language'),
]
urlpatterns += [
url("^", include("your_app.urls")),
]
So you dont have to change all your urls. Just place correctly i.e if you are useing I18N place them with admin in urlpatterns = i18n_patterns section else in another section as in example above replace the name with your_app.urls.

Django URL mapping - NameError: name X is not defined

[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:
myproject/
myapp/
urls.py
views.py
myproject/
urls.py
The myproject/urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include(myapp.urls)),
]
The myapp/urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^hello/', myapp.views.hello),
]
The myapp/views.py is as follows:
from django.shortcuts import render
def hello(request):
return render(request, "hello.html", {})
However, running 'python manage.py runserver' results in the following error:
url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined
INSTALLED_APPS in settings.py contains 'myapp'.
I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which means that the import is not required.
url(r'^myapp/', include('myapp.urls')),
Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.
Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
Make sure you have imported following modules to urls.py.
from django.conf.urls import url
from django.contrib import admin
in django 2.0
use these
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index, name="index"),
path('admin/', admin.site.urls),
]
your app URL has to be a string
so, here is how the code should look like.
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
also, note that from python 2 upward the regular expression is not needed.
change URL to path
from django.conf.URLs import include path
from Django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
]
In Django 2.1.7 here is the default urls .py file
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
so we need to add this line as well
from django.conf.urls import url
I have followed #Alasdair answers
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which
means that the import is not required.
Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api/article/', include(article_users)),
path('api/user/', include(user_urls)),
]
Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path