While I'm trying to add a urls configuration in my urls.py file, I'm getting an error like ModuleNotFoundError: No module named 'employee.urls' and OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('employee/',include("employee.urls"))
]
directory and file path
It's a convention to name all your url config files urls.py not url_<appname> unless you have a very special use-case.
But in either case, you must import the url file after your filename. So if you want to keep the filename as is, try this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('employee/',include("employee.urls_employee"))
Related
In my project I have an app called api where I keep my viewsets in a directory called views. I keep getting ModuleNotFoundError: No module named 'api.views.book'; 'api.views' is not a package where I import in my api/urls.py
api/urls.py
from rest_framework import routers
from api.views.book import BookViewset
router = routers.DefaultRouter()
router.register(r'books', BookViewset, 'books')
api_urls = router.urls
In my main urls.py I'm doing this:
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from api.urls import api_urls
urlpatterns = [
path('api', include(api_urls)),
path('admin/', admin.site.urls),
]
I didn't get an error until I imported api_urls
I should mention that api is included in INSTALLED_APPS
You need to include a file named __init__.py in a directory to make it a package, then you can import from it
In your case you need a file api/views/__init__.py
I am absolute new to django and I have followed the example of Django documentation https://docs.djangoproject.com/en/3.0/intro/tutorial01/
but getting a problem in main project's urls.py file which is Module not found error pls help. Heres my main projects urls.py:
`from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('product.urls')),
path('admin/', admin.site.urls),
]
I'm using Django 1.9
from django.contrib admin
from django.conf.urls import include, url
from slack.views import DRSWebhookTransactionView, DRSMessageView
from django.urls import path
api_patterns = ([
path('web/', DRSWebhookTransactionView.as_view()),
path('events/', DRSMessageView.as_view()),
], 'api')
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('api/v1/', include(api_patterns)),
]
After running python manage.py runserver:
from django.urls import ( # noqa
ModuleNotFoundError: No module named 'django.urls'
I'm getting this error after I tried including path. If I don't include path it's not showing the error for the 'path', it's still showing the same error. Can someone tell how can I rewrite this program? and tell me what I'm doing wrong?
for django 1.9
from django.conf.urls import url
refer this
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
models.py
from django.conf.urls import include, url
app_name = "Review"
urlpatterns = [
url(r'^books/', include("Review.urls", namespace='reviews')),
]
Review\urls.py
from django.conf.urls import include, url
from django.contrib import admin
from .views import (
ReviewUpdate,
ReviewDelete,
)
urlpatterns = [
url(r'^reviews/(?P<pk>\d+)/edit/$', ReviewUpdate.as_view(), name='review_update'),
url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='review_delete'),
]
I am providing app_name before my urlpatterns. But it's giving me error while running my code. The errors are given below:
File "E:\workspace\python\web\Book_Review_App\Book\urls.py", line 13, in <module>
url(r'^books/', include("Review.urls", namespace='reviews')),
File "E:\workspace\python\web\Book_Review_App\venv\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.
Please help.
The app_name needs to be set in your app's urls.py not the main urls.py.
In review.urls.py add the following,
from django.conf.urls import include, url
from django.contrib import admin
from .views import ( ReviewUpdate, ReviewDelete, )
app_name = 'Review'
urlpatterns = [
url(r'^reviews/(?P<pk>\d+)/edit/$', ReviewUpdate.as_view(), name='review_update'),
url(r'^reviews/(?P<pk>\d+)/delete/$', ReviewDelete.as_view(), name='review_delete'),
]
and remove the app_name from the main urls
EDIT: for the admin issue that the op mentioned in the comments,
in the main urls.py
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
You need to define app_name in urls.py and pass urlconf_module in tuple . Here is example
app_name = "example"
from typing import NamedTuple
class NamedURL(NamedTuple):
urlconf_module: None
app_name: None
daily_urls = NamedURL(<your custom urls in list>, "example")
urlpatterns = [
...
re_path(r'^daily/', include(daily_urls, namespace='daily')),
]