Importing from views directory - django

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

Related

Routing does not work in Django: page not found (GET)

I've tried multiple methods of writing views but I don't think it is a problem here. App is installed in settings.py
It displays error every time.
project structure:
structure
views.py (app folder)
from django.http import HttpResponse
from django.shortcuts import render
def home_view(request):
return HttpResponse('Hello World')
url.py in apps folder
from django.urls import path
from . import views
urlpatterns = [
path('home_view/', views.home_view)
]
apps.py in app folder
from django.apps import AppConfig
class AppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app'
urls.py in store folder
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/home_view/', include('app.url')),
path('admin/', admin.site.urls),
]
error message:
error
As a django web development expert I saw some small corrections to be done:
In store urls.py file its app/ and app.urls
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/', include('app.urls')),
path('admin/', admin.site.urls),
]
Then change the app url.py file name to standard name urls.py.
also don't forget to add your app to installed_apps variable in settings.py file:
INSTALLED_APPS=[
'app',
'django.contin.auth',
#and other already specified apps
]
Rest all files have no bugs!!
According to this the correct path for HttpResponse is:
http://localhost:8000/app/home_view/
OR
http://127.0.0.1:8000/app/home_view/
In your urls.py file
Change
path('app/home_view/', include('app.url')),
To
path('', include('app.url')),
Then
On your browser go to:
127.0.0.1:8000/home_view/
The main thing it must be app.urls not app.url. change your file to url.py to urls.py, its recommended.
If you have defined path('app/home_view/', include('app.urls')) in urls.py of your store folder, then it goes to your urls.py which is in app.
In your app's urls.py you have written path('home_view/',views.home_view).
It means if you type 127.0.0.1:8000/app/home_view/home_view/ then it will render your HttpResponse that is Hello world.

(Django) include does not import other apps' urls

In my Django project I have 2 apps: core and books. In my core/urls.py, I use python include('books.url') to import the urls from books/urls.py, but I keep getting this error
I have been having this issue now that's bugging me. I had a workaround for it, though I really want to fix this.
core/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
# local
urlpatterns = [
path('/', include('books.urls')),
path('admin/', admin.site.urls),
]
books/urls.py
from django.contrib import admin
from django.urls import path
# local
from graphene_django.views import GraphQLView
from books.schema import schema
urlpatterns = [
path('graphql/', GraphQLView.as_view(graphiql=True, schema=schema)),
]
As suggested by SO, I have:
put books.urls inside the single quotes ' '
placed the path('/', include('books.urls')) on top
switch from from django.urls import include to
from django.conf.urls import include
The only workaround I have is place all urls into the core/urls.py, but that seems too chunky in the long run. I don't get why include works for everyone but not me!
Could you help me with this issue? Thank you!
Now GraphQLView is called with the URL 127.0.0.1:8000/graphql/, if you want to call it with the URL 127.0.0.1:8000/, you need to change your code:
core/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
# local
urlpatterns = [
path('/', include('books.urls')),
path('admin/', admin.site.urls),
]
books/urls.py
from django.contrib import admin
from django.urls import path
# local
from graphene_django.views import GraphQLView
from books.schema import schema
urlpatterns = [
path('/', GraphQLView.as_view(graphiql=True, schema=schema)),
]

FIX ImportError: cannot import name 'patterns'

I am writing documentaton for the API I have already built. I have already installed drfdocs and added it in INSTALLED_APPS then configured the urls yet I get the following logs below. I think the version of rest_framework_docs is importing patterns which I believe its deprecated, new versions of django are no longer using it. How do I fix this?
I have django 1.11 and using python3. I get the following logs when I try to run ther server:
Logs
File "/usr/local/lib/python3.5/dist-packages/rest_framework_docs/urls.py", line 1, in <module>
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Views.py
from django.conf.urls import include, url
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
# from organizations.backends import invitation_backend
from rest_framework.authtoken.views import obtain_auth_token
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'api/token/', obtain_auth_token, name='api-token'),
# url(r'^account/', include('accounts.urls', namespace='account')),
url(r'^hr/', include('hr.urls', namespace='hr')),
url(r'^acc/', include('Accounting.urls', namespace='Accounting')),
url(r'^payroll/', include('payroll.urls', namespace='payroll')),
url(r'^bill/', include('bill.urls', namespace='bill')),
url(r'^docs/', include('rest_framework_docs.urls')),
url(r'^proc/', include('procurement.urls', namespace='procurement')),
# url(r'^accounts/', include('organizations.urls')),
# url(r'^authority/', include('authority.urls')),
url(r'^organization/', include('organizations.urls')),
url(r'^admin/', admin.site.urls),
# url(r'^invitations/', include(invitation_backend().get_urls())),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

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

Django Name Error

I have the following code in my urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^testModule/',include(testModule.urls)),
url(r'^admin/', admin.site.urls),
]
and testModule is my app name which includes :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]
And my views.py is
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
# Create your views here.
However, i get the following error while running the server:
line 20: url(r'^testModule/',include(testModule.urls)),
NameError: name 'testModule' is not defined
You haven't imported testModule in your main urls.
Had the same problem but solved it like this:
add "import testModule" to your urls.py
add "from django.conf.urls import url" to your urls.py in the app directory, in this case testModule
Once done you will get a "Page not found at/" error when you reload 127.0.0.1:8000. This is bacause the url to your app is actually at 127.0.0.1:8000/testModule