django 2.0 url.py include namespace="xyz" - django

I've a problem with the routing django.conf.urls include() - main project folder - urls.py
Line 22 of urls.py (pip freeze and urls.py - see below) throws the error in the console:
Quit the server with CONTROL-C.
[02/Jan/2018 14:22:49] "GET /api/compositions/ HTTP/1.1" 200 30058
[02/Jan/2018 14:22:53] "GET /api/compositions/1/ HTTP/1.1" 200 6195
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f17ee06e400>
Traceback (most recent call last):
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 397, in check
for pattern in self.url_patterns:
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 536, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
return import_module(self.urlconf_name)
File "/home/ernst/django_virtualenv/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/ernst/django_virtualenv/mwrench/mwrench/urls.py", line 23, in <module>
url(r'^api/compositions/', include("compositions.api.urls", namespace="compositions-api")),
File "/home/ernst/django_virtualenv/lib/python3.4/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.
urls.py:
from compositions.api.views import AlloyelementDetailAPIView
1 # Without settings the API URL queries do not work
2 from django.conf.urls.static import static
3 from django.conf import settings
4 from django.conf.urls import include, url
5 from django.contrib import admin
6 from mwrench.views import (
7 template,
8 ChartData,
9 CompositionAPI,
10 testview
11 )
12 from rest_framework.urlpatterns import format_suffix_patterns
13 from accounts.views import (login_view, register_view, logout_view)
14
15 urlpatterns = [
16 url(r'^admin/', admin.site.urls),
17 url(r'^', include('compositions.urls' )),
...
22 url(r'^api/compositions/', include("compositions.api.urls", namespace="compositions-api")),
console$ pip freeze
Django==2.0
Jinja2==2.10
Markdown==2.6.10
MarkupSafe==1.0
PyYAML==3.12
appdirs==1.4.3
certifi==2017.11.5
chardet==3.0.4
deprecation==1.0.1
django-braces==1.12.0
django-crispy-forms==1.7.0
django-filter==1.1.0
djangorestframework==3.7.7
gunicorn==19.7.1
idna==2.6
include==0.2.1
iso8601==0.1.12
jsonpatch==1.21
jsonpointer==1.14
keystoneauth1==3.3.0
mysqlclient==1.3.12
numpy==1.13.3
openstacksdk==0.9.19
os-client-config==1.28.0
pbr==3.1.1
psycopg2==2.7.3.2
pytz==2017.3
requests==2.18.4
requestsexceptions==1.3.0
reverse==0.1.0
six==1.11.0
stevedore==1.28.0
url==0.4.2
urllib3==1.22
I already tried defining the app_name= variable within the urls.py, as suggested here How to register DRF router url patterns in django 2. The problem occured after porting from Django 1.11 to 2.0.
Is there a better way except calling the view by defining the name="yournametothedetailview" directly from the project urls.py.
Thank you very much,
best Ernst!

Just in case you have updated from an older version of Django it would be advisable if you go to the specific app urls.py file and add the following line just above the url patterns;
app_name = 'current_app_name'
Below is an example of how it will look like;
app_name = 'current_app_name'
urlpatterns = [
url(r'^$', views.index, name='index'),
]

i think to be able to reference a namespace ->
in your urls.py of composition app you're supposed to set the variable app_name = 'your app name'

Related

Django unable to resolve the import type

Iam a newbie learning django iam struck here can someone help on this.
i followed this tutorial https://www.youtube.com/watch?v=RMTVAIVrdtM
Iam using pycharm IDE, created on the similar lines but still iam getting the error.
from poll import view as poll_views ### Getting error in this line .....----<<<<<<<
urlpatterns = [
path('admin/', admin.site.urls),
path(''.poll_views.home,name='home'),
path('create/'.poll_views.create, name='create'),
path('vote/<poll_id>'.poll_views.vote, name='vote'),
path('results/<poll_id>'.poll_views.results, name='results'),
]
views.py
from django.shortcuts import render
# Create your views here.
def home(request):
context = {}
return render(request, 'poll/home.html',context)
def create(request):
context = {}
return render(request, 'poll/create.html',context)
def vote(request, poll_id):
context = {}
return render(request, 'poll/vote.html',context)
def results(request, poll_id):
context = {}
return render(request, 'poll/results.html',context)
On terminal
(test) C:\Users\Learner\projects\poll_project>python manage.py migrate
Traceback (most recent call last):
File "C:\Users\Learner\projects\poll_project\manage.py", line 21, in <module>
main()
File "C:\Users\Learner\projects\poll_project\manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\base.py", line 366, in execute
self.check()
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = self._run_checks(
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\commands\migrate.py", line 64, in _run_checks
issues.extend(super()._run_checks(**kwargs))
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Learner\Envs\test\lib\site-packages\django\urls\resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "C:\Users\Learner\Envs\test\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Learner\Envs\test\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "c:\users\Learner\appdata\local\programs\python\python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1021, in _gcd_import
File "<frozen importlib._bootstrap>", line 998, in _find_and_load
File "<frozen importlib._bootstrap>", line 977, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Learner\projects\poll_project\poll_project\urls.py", line 19, in <module>
from poll import view as poll_views
ImportError: cannot import name 'view' from 'poll' (C:\Users\Learner\projects\poll_project\poll\__init__.py)
Please find the project folder screenshot below.
this is my folder structure
Can someone please help me where iam going wrong ???
The import statement should be like
If want to import specific view
from poll.views import <nameoftheview> as poll_views
Or if you want to import all the views
import poll.views as poll_views
Update: Correct error in your urls.py (also pointed out by #sheraram)
urlpatterns = [
path('admin/', admin.site.urls),
path('create/', poll_views.create, name='create'),
path('vote/<poll_id>', poll_views.vote, name='vote'),
path('results/<poll_id>', poll_views.results, name='results'),
path('', poll_views.home,name='home'),
]
url takes following argument path(route, view, name{optional})
but you are giving path(route.view, name)
replace . after route by ,

How to fix 'Specifying a namespace in include() without providing an app_name '

I am facing with the problem of setting the app_name attribute in the included module.
Despite the fact that I have seen similar answers on how to fix it, I did not find something working for me.
Here is my urls.py
from django.conf.urls import url,include
from django.contrib import admin
from intranet import views,forms
from django.contrib.auth import views as auth_views
from django.conf import settings
from rest_framework import routers
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import reverse
router = routers.DefaultRouter()
router.register(r'pharmakeia', views.PharmakeiaViewSet,base_name='pharmakeia')
router.register(r'doctors', views.DoctorsViewSet,base_name='doctors')
router.register(r'new-pharmakeia', views.VriskoViewSet,base_name='new-pharmakeia')
router.register(r'new-all-pharmakeia', views.VriskoAllViewSet,base_name='new-all-pharmakeia')
views.json_data,base_name='test_pharmakeia')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^rest/', include(router.urls, namespace='rest')),
url(r'^api-auth/', include(rest_framework.urls, namespace='rest_framework')),
Then I changed the script:
I tried to declare an app_name variable
app_name="intranet"
and then I tried to fix the urls containing the include function
url(r'^rest/', include((router.urls,app_name), namespace='rest')),
url(r'^api-auth/', include(('rest_framework.urls',app_name), namespace='rest_framework')),
I face a new problem
"ModuleNotFoundError: No module named 'router' despite the fact that I have already defined rooter**
Here is my output from command line
self._target(*self._args, **self._kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/www/wwwroot/geolocator/geolocator/urls.py", line 40, in <module>
url(r'^rest/', include(router.urls, namespace='rest')),
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/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.
^C(geolocator_venv) [root#76 geolocator]# clear
(geolocator_venv) [root#76 geolocator]# python manage.py runserver 76.neurosynthesis.com:19999
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/root/.pyenv/versions/3.7.2/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/root/.pyenv/versions/3.7.2/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/www/wwwroot/geolocator/geolocator/urls.py", line 40, in <module>
url(r'^rest/', include(router.urls, namespace='rest')),
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/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.
Any idea how to fix this?

NameError: account not defined

I'm trying to create a simple registration form using django1.11.5
I created a project called mysite using the command:
django-admin.py startproject mysite .
And I have created an app called account using the code:
python manage.py startapp account
In the urls.py file of the folder mysite I specified the urlto be accessed as follows:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^account/',include(account.urls)),
url(r'^admin/', admin.site.urls),
]
However when I'm trying to migrate or runserver I'm getting the following error.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000273BFBDFC80>
Traceback (most recent call last):
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\core\management\base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\urls\resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Welcome\django\myvenv\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Welcome\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:\Users\Welcome\django\mysite\urls.py", line 19, in <module>
url(r'^account/',include(account.urls)),
NameError: name 'account' is not defined
So can someone please help. I'm new at this
When you 'include' urls, the reference to the app containing the urls should be a string like so:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
...
url(r'^account/', include('account.urls')),
...
]
You are trying to import the actual app, but that is not necessary in this case.
Does that make sense?

Installing django-rest-auth, ' NameError: name 'FacebookLogin' is not defined '

I followed http://django-rest-auth.readthedocs.io/en/latest/installation.html#registration-optional
As django-rest-auth installation doc said, i installed django-allauth first, but url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
NameError: name 'FacebookLogin' is not defined comes, and i don't know how to solve this.
blog/views.py
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
NameError: name 'FacebookLogin' is not defined
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import filters
from rest_framework import generics
from django.contrib.auth.decorators import login_required
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class FacebookLogin(SocialLoginView):
adapter_class = FacebookOAuth2Adapter
blog/urls.py
from django.conf.urls import url, patterns
from blog import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^blog/$', views.post_list, name='list'),
url(r'^blog/create/$', views.post_create, name='create'),
url(r'^blog/(?P<pk>[0-9]+)$', views.post_detail, name='detail'),
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.sites',
'blog',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
]
SITE_ID = 1
When i command python3 manage.py runserver,
it gives me
python3 manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f65f81601e0>
Traceback (most recent call last):
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/keepair/djangogirls/mysite/urls.py", line 21, in <module>
url(r'', include('blog.urls')),
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/keepair/djangogirls/blog/urls.py", line 14, in <module>
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
NameError: name 'FacebookLogin' is not defined
Why this error has been occurred?
What is my fault?
Thanks for reading.
Error is self explanatory that FacebookLogin is not defined in urls.py.
Fault - You are using FacebookLogin but haven't imported it in the urls.py.
Solution:
You have imported blog.views in the urls.py, so replace FacebookLogin.as_view() with views.FacebookLogin.as_view()
from django.conf.urls import url, patterns
from blog import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^blog/$', views.post_list, name='list'),
url(r'^blog/create/$', views.post_create, name='create'),
url(r'^blog/(?P<pk>[0-9]+)$', views.post_detail, name='detail'),
url(r'^rest-auth/facebook/$', views.FacebookLogin.as_view(), name='fb_login'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
OR
import the class itself from blog.views import FacebookLogin.

Django could not import [function]. The path must be fully qualified

I recently started Django and I'm having a problem rendering a "hello world" page, the issue goes like this :
Could not import 'index'. The path must be fully qualified.
Django is not detecting my views.index function
here is the code:
src/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^pages/', include('pages.urls')),
]
pages/urls.py
from django.conf.urls import url
urlpatterns = [
url(r'', 'index', name="index"), #error
]
pages/views.py
from django.shortcuts import render
# Create your views here.
def index(request):
context = {}
return render('index.html', context)
I want to render the index.html, how can I do this?
--------------- UPDATE ---------------
CMD error
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044D70D0>
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python34\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\src\src\urls.py", line 26, in <module>
url(r'^pages/', include('pages.urls')),
File "C:\Python34\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Python34\Scripts\src\pages\urls.py", line 5, in <module>
url(r'^$', views.index, name="index"),
AttributeError: 'module' object has no attribute 'index'
added from pages import views in urls.py
In Django 1.8+, you should import the view and include the callable in your URL patterns.
from .views import index
urlpatterns = [
...
url(r'^$', index, name="index"),
Note that the second argument is now index, the view we have imported, instead of the string 'index'. I have also changed the regex so that it only matches the index page. Using '' would match every URL.
Another option would be to import the views module instead of the individual view. This will make it easier to include other views e.g. views.about or views.contact.
from . import views
urlpatterns = [
...
url(r'^$', views.index, name="index"),