Circular Import in Django - python

I have been trying to run a client-server application using Django. When I am trying to run my server in Django , it is giving me the following error.
django.core.exceptions.ImproperlyConfigured: The included URLconf '' does not appear to
have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
The project urls.py -
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('chat.views')),
]
App's views.py -
from django.shortcuts import render
from django.http import JsonResponse
def home(request):
if request.method == 'POST':
if request.is_ajax():
//code
return JsonResponse(data)
return render(request,'index.html')
Where am I going wrong?

include method takes app urls.py model not views.py. You need to create urls.py file inside your app and replace url(r'^', include('chat.views')) with url(r'^', include('chat.urls')) in projects urls file. See django docs.

Include method in url.py file is used to include url patterns that are specified in other file. and when you are doing this url(r'^', include('chat.views')), it is unable to find url patterns in your views file. Hence giving error this:
django.core.exceptions.ImproperlyConfigured: The included URLconf ''
does not appear to have any patterns in it. If you see valid patterns
in the file then the issue is probably caused by a circular import.
we generally create a urls.py file in our app folder and write all our url patterns regarding this app in this file.
create a new urls.py file in your app folder and write url patterns in that file.
and then include your app's urls.py file in main urls.py file like this:-
url(r'^', include('chat.urls')),
and your app's urls.py file should look like:
from django.conf.urls import url
urlpatterns = [
url(r'', views.home, name = "home")),
]
you can find out more about django urls from documentation:- django urls
And if you don't want to create new urls.py file in you app directory then you can just import your views in main urls.py file and write url pattern in this file. then your main urls.py file will look like this:-
from django.conf.urls import url,include
from django.contrib import admin
from chat.views import home
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', home, name = "home"),
]

Related

importing apps into urls.py

I am trying to import views from my apps into the urls.py file. For awhile, I was able to using "from app_name.views import view_name", but for some reason now it is not recognizing the app name.
I did not change anything in my settings.py file so I'm not sure what caused this. I did delete some migrations and an my database.
One thing that is weird is that I can import them if I go up a folder so, "from src.appname.views" and this seems to work, but is not what I want since by base directory is the src folder.
from django.contrib import admin
from django.urls import path
from pages.views import home_page, history, more_information, prop_analysis
from Product.views import property_analysis_tools, property_analysis_results
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_page),
path('home', home_page),
path('history', history),
path('information', more_information),
path('analyze', prop_analysis),
path('results', property_analysis_results)
Another odd thing is that when I run the server, all of these views seem to work. Can Django sometimes give off false errors?
In Django in order to import an app.view into project urls, we create an additional urls.py in every app and link that urls.py into
our project urls.py , just as shown below
change project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls', namespace="pages")),
]
create another urls.py in your pages app
as below, similarly in any other apps.
from django.urls import path
from .views import (home_page,history,....)
app_name = 'pages'
urlpatterns = [
path(' ', homepage , name='homepage-view'),
path('history/',history, name='history-view'),
]
Every other view from respective app.views will be included in corresponding app.urls [if u have multiple apps]
nB: changes may occur according to your settings.py configurations,
for more, read this django URL documentation
watch Corey Schafer playlist best Django tutorial for beginners

Django 3.1 gives error | URLconf does not have any pattern in it, If you see valid patterns in the file then the issue is probably

Python: 3.6.0 | Django: 3.1.4
By going through the documentation, I created one app, below is my app url.py (from app) file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
My site urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('npimgapp/', include('npimgapp.urls')),
path('admin/', admin.site.urls)
]
My view.py file in app
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Welcome ...")
On executing this code I am getting below error:
django.core.exceptions.ImproperlyConfigured: The included URLconf
'npimgpro.urls' does not appear to have any patterns in it. If you see
valid patterns in the file then the issue is probably caused by a
circular i mport.
Also, below is my directory structure for ref:
Your directory structure is wonky; there shouldn't be two nested npimgpro folders.
Move the contents of that npimgpro folder up one level, so it's
npimgpro/
(etc.)
npimgapp/
(etc.)
manage.py
db.sqlite3

django.core.exceptions.ImproperlyConfigured: error is coming

I am new to Django and hence don't have much idea about it. I am trying to create an app having the following contents in my view.py file:
from django.shortcuts import render
from fusioncharts.models import City
def pie_chart(request):
labels = []
data = []
queryset = City.objects.order_by('-population')[:3]
for city in queryset:
labels.append(city.name)
data.append(city.population)
return render(request, 'pie_chart.html', {
'labels': labels,
'data': data,
})
The content of my urls.py file inside the app is as follows:
from django.urls import path
from fusioncharts import views
urlpatterns = [
path('pie-chart/', views.pie_chart, name='pie-chart'),
]
and the content of my urls.py file inside the main project folder is as follows:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('fusioncharts.urls'))
]
In the settings.py file, I have added the following:
ROOT_URLCONF = 'Piechart.urls'
and under the TEMPLATES, added the following:
'DIRS': ['Piechart/fusioncharts/templates'],
Now while running my app, I am getting the following error:
django.core.exceptions.ImproperlyConfigured: The included URLconf 'Piechart.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Can anyone please say what's going wrong?
you forgot to import include in the project urls.py, i was about to comment this but can't in my current reputation level.
from django.urls import path,include

My code doesn't run it says there is a circular import error I'm try to load the home view so that it redirects me

I am working on a django project, but it returns
the included urlconf "myapp.urls"does not appear to have any patterns in it.
I tried checking my views to ensure I imported everything correctly
from django.contrib import admin
from django.urls import path
from .views import home
from accounts.views import login_view
urlpatterns = [
path('admin/', admin.site.urls),
path('',home),
path('accounts/login/', login_view),
]
I expect the site to run and redirect me to the login page
This is my views in the same directory with the urls.py
#login_required
def home(request):
return render(request,"home.html")
Your code doesn't have a problem, send the repository link
Include this In your settings.py
settings.py
LOGIN_URL = '/accounts/login/'
LOGOUT_URL = "/accounts/logout/"
LOGIN_REDIRECT_URL = " "

Why am I getting the error message ImportError: No module named <app>.urls?

I was recently building a django project. I have been looking through the files for a couple of hours now and can't find the problem that would result in this kind of error message. Below, I will show you all of the relevant files within the project.
base url.py:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('diplay.urls')),
]
app url.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
app views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h2>HEY!</h2>")
I'm not sure why this is not working because I found a similar format online, and it seemed that every line was similar to the other one. When I try running the server, it gives me the error statement
ImportError: No module named diplay.urls
Any ideas?
1, make sure your app name is diplay which is same as in your base urls.py, I think maybe there is typo, should change to:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^display/', include('display.urls')),
]
2, make sure the file name of urls should be urls.py instead of url.py in both base and app folder
Does app diplay exists or is it typo of display
> diplay.urls
Also does urls file in app exists

Categories

Resources