Unable identify mistake in URL pattern - django

I have 3 apps inside django project (leadmanager) leads, frontend, accounts
Everything is working fine if I dont include accounts.urls (from accounts app) in leadmanager.urls but as soon as I include accounts.urls I getting the following error (all of these apps are registered in settings.py):
$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 591, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Danial Ahmed\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Danial Ahmed\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\registry.py", line 70,
in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'leadmanager.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.
leadmanager.url (main):
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('frontend.urls')),
path('',include('leads.urls')),
path('',include('accounts.urls')),
]
lead.urls:
from rest_framework import routers
from .api import LeadViewSet
router = routers.DefaultRouter()
router.register('api/leads', LeadViewSet, 'leads')
app_name = 'leads'
urlpatterns = router.urls
accounts.urls:
from django.urls import path, include
from .api import ReigsterAPI
from knox import views as knox_views
app_name = 'accounts'
urlpatterns = [
path('api/auth', include('knox.urls')),
path('api/auth/register', RegisterAPI.as_view()),
]
frontend.urls:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index),
]

In .api I was import serializers.py but I had a typing mistake, my file was called serailizers.py, after fixing name of file everything started working!
I just wish that there was someway to detect this syntax errors, because the error I received was not very helpful.

Related

Circular Import error in Django occurs only when i import geemap package

I am trying to use geemap in combination with django to build a web app for plotting satellite data. I have installed the geemap package in my django project. My projects name is CustomMaps, and the directory structure is as below.
enter image description here
The code for my CustomMaps.urls.py
is as below
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
urlpatterns = [
path('admin/', admin.site.urls),
path('plotmap/', include('PlotMap.urls')),
]
The code for my PlotMap.urls.py is as follows
from django.urls import path
from django.conf.urls import include, url
from .views import hello
urlpatterns = [
path('hello/', hello, name = 'hello'),
]
And my PlotMap.views.py is as follows
from django.http import HttpResponse
from django.shortcuts import render
import geemap as gm
#import pandas as pd
def hello(request):
map = gm.Map()
return render(request, "PlotMap/hello.html", { "m" : map})
But I am getting the following error on running the project
Exception in thread django-main-thread:
Traceback (most recent call last):
File "D:\Internship\Django App\MyProject\lib\site-packages\django\urls\resolvers.py",
line 604, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "D:\Internship\Django App\MyProject\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Internship\Django App\MyProject\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "D:\Internship\Django App\MyProject\lib\site-packages\django\core\management\base.py", line 423, in check
databases=databases,
File "D:\Internship\Django App\MyProject\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "D:\Internship\Django App\MyProject\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "D:\Internship\Django App\MyProject\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "D:\Internship\Django App\MyProject\lib\site-packages\django\urls\resolvers.py",
line 416, in check
for pattern in self.url_patterns:
File "D:\Internship\Django App\MyProject\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\Internship\Django App\MyProject\lib\site-packages\django\urls\resolvers.py",
line 611, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'CustomMaps.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.
But if I run same code after changing my views.py like below it works perfectly fine.
from django.http import HttpResponse
from django.shortcuts import render
#import geemap as gm
#import pandas as pd
def hello(request):
#map = gm.Map()
return render(request, "PlotMap/hello.html", { })
I am not able to understand why is my 'import geemap' leading to circular import error in CustomMaps.urls.py. I tried searching for the same, but have not been able to find anything till now. If someone can please help me out then i will be able to proceed with my work soon, Thank You.
Actually, I tried installing a new version of python(3.8) and also downloaded a new version of geemap(0.11.4) which solved the issue.
But, I am still struggling on how to display the instance of geemap.Map() in my django template, any suggestions would be much helpful.
I will post a new question for the same.
Here: How to display an instance of geemap.Map() in Django Template

Problem in Django 2.2 with Debug=True on Ubuntu

I am trying to run a project in Django 2.2 with Debug=True on Ubuntu but I get this error in python manager.py runserver:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 581, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 588, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'moeaforhdl.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.
This is /myparentapp/urls.py:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'', include('moeaforhdlweb.urls')),
url(r'^admin/', admin.site.urls),
url(r'^tinymce/', include('tinymce.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is /mychildapp/urls.py:
from django.conf.urls import url
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^help/$', views.help, name='help')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I test this project on Windows 10 and it works perfectly, but not on Ubuntu.
Besides, if I comment this line:
url(r'', include('moeaforhdlweb.urls'))
it works.
Any help will be grateful, thanks a lot.

Django 'ImportError: cannot import name url'

Hi could anyone help me fix 'ImportError: cannot import name url' problem?
I have followed tutorial here https://docs.djangoproject.com/en/1.9/intro/tutorial01/
I have tried another tutorial https://docs.djangoproject.com/zh-hans/2.0/ref/urls/#django.urls.include
but neither of them worked
My Django version is 1.11.20
Performing system checks...
Unhandled exception in thread started by Traceback
(most recent call last):
File
"/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py",
line 228, in wrapper fn(*args, **kwargs)
File
"/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py",
line 124, in inner_run self.check(display_num_errors=True)
File
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
line 359, in check
include_deployment_checks=include_deployment_checks,
File
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
line 346, in _run_checks return
checks.run_checks(**kwargs)
File
"/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py",
line 81, in run_checks new_errors =
check(app_configs=app_configs)
File
"/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py",
line 16, in check_url_config return
check_resolver(resolver)
File
"/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py",
line 26, in check_resolver return check_method()
File
"/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
line 256, in check for pattern in
self.url_patterns:
File
"/usr/local/lib/python2.7/dist-packages/django/utils/functional.py",
line 35, in get res =
instance.dict[self.name] = self.func(instance)
File
"/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
line 407, in url_patterns patterns =
getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File
"/usr/local/lib/python2.7/dist-packages/django/utils/functional.py",
line 35, in get res =
instance.dict[self.name] = self.func(instance)
File
"/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py",
line 400, in urlconf_module return
import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/init.py", line 37, in
import_module
import(name) File "/home/adduser/cantera_correction/mysite/urls.py", line 16, in
from
django.conf.urls import include, path
ImportError: cannot import name path
path was introduced in django since Django 2.0. So, if you are using Django 1.11, then you can't use it. You need to define urls like this:
from django.conf.urls import url, include
urlpatterns = [
# rest of the urls
url(r'^$', HomeView.as_view()),
]
correct your imports to this:
from django.urls import path, include
This code will work for you.
from django.urls import path
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
]

cannot import LoginView?

i'm learning some things about Django 1.9 and i want to make a login form but with class-based views.
This is mi view code:
from django.shortcuts import render
from django.contrib.auth.views import LoginView
class AlumnoLoginView(LoginView):
template_name = "alumno/login.html"
redirect_authenticated_user = True
And this is my urls code:
from django.conf.urls import url, include
from .views import AlumnoLoginView
app_name = 'alumno'
urlpatterns = [
url(r'^login/$', AlumnoLoginView.as_view(), name="login"),
]
This make an error:
ImportError: cannot import name LoginView
But if I delete the code inside urls nothing happens.
This is the full error trace (with François change):
Unhandled exception in thread started by <function wrapper at 0x7f044ad8ac08>
Traceback (most recent call last):
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/plafhz/Envs/StudentAdmin/studentadmin/studentadmin/urls.py", line 21, in <module>
url(r'^alumno/', include('alumno.urls')),
File "/home/plafhz/Envs/StudentAdmin/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/plafhz/Envs/StudentAdmin/studentadmin/alumno/urls.py", line 3, in <module>
from alumno.views import AlumnoLoginView
File "/home/plafhz/Envs/StudentAdmin/studentadmin/alumno/views.py", line 3, in <module>
from django.contrib.auth.views import LoginView as BaseLoginView
ImportError: cannot import name LoginView
What is wrong?
How I fix it?
Thank :)
Class-based views LoginView and LogoutView are introduced on Django 1.11 and function-based views 'login' and 'logout' are deprecated since then.
You can use class-based views LoginView and LogoutView like this:
from django.contrib.auth.views import LoginView, LogoutView
urlpatterns = [
url(r'^login/$', LoginView.as_view(template_name='...'), name="login"),
]
There's no view called LoginView in django.contrib.auth.views
I think you need.
from django.contrib.auth.views import login
LoginView only available from Django 1.11
cannot import LoginView?
You must be using lower version of django ..Login view supported only in 1.11 or higher version..
You need to install 1.11 or higher version
pip install django==1.11

Django, upgrading to 1.9

I evolve my Django version to 1.9 (before I had the 1.6 or 1.7), so I modify many obseltes things...
But I have a problem with theses lines in my urls.py :
import django
import main_app
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView, ListView
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from main_app.views import *
from main_app.views import password_reset_confirm
... # many urls
url(r'^authentification/$', django.contrib.auth.views.login),
url(r'^forget/send/$', django.contrib.auth.views.password_reset_done),
url(r'^password/$', django.contrib.auth.views.password_reset),
url(r'^password_forget/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', main_app.views.password_reset_confirm),
url(r'^password-init/$', django.contrib.auth.views.password_reset_complete),
I have this error when I write "python manage.py runserver" :
Unhandled exception in thread started by <function wrapper at 0x7f5bcf01af50>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/yb/web/carzuip/carzuip/urls.py", line 55, in <module>
url(r'^authentification/$', django.contrib.auth.views.login),
NameError: name 'django' is not defined
I don't understand why I have a problem just with those 5 urls ??!
Thank's
That code doesn't show you importing django: it shows you importing elements underneath it, but never the name itself. It is a fundamental principle of Python that you must import or define every name you use. In your case, import django at the top would work, although note you would then have another problem when the code gets to the password_reset URL since that is referenced from main_app which you again haven't imported.
Try to create a new project with django-admin startproject and copy over the needed settings / copy over manage.py and wsgi.py.
Some releases had breaking changes, i.e. on how the wsgi module imports certain django stuff. Most projects can easy be recreated and you have the newest django defaults in the files.
It's of course just a guess, but worth trying. If you're using a virtualenv, you may need to recreate some things inside, when your python was upgraded / you moved the virtualenv. Such stuff can be quite annoying sometimes.
Resolved !
Euh I think...
Just I add these lines :
from django.contrib.auth.views import login
from django.contrib import auth
And it's works !
It's normal hein ?