I can't found what is wrong with urls.py - python

I can't find where is bug. It's my begining with django. If somebody helping me i will grateful.
urls.py :
from django.conf.urls import url, patterns, include
from django.contrib import admin
from superlista.lists import views
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^$', views.home_page, name='home'),
]
views.py :
from django.shortcuts import render
# Create your views here.
def home_page():
pass
python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03917468>
Traceback (most recent call last):
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\commands\runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\checks\urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\checks\urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\vnbox\AppData\Local\Programs\Python\Python35-32\lib\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 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\vnbox\PycharmProjects\kurs_h\superlista\superlista\urls.py", line 19, in <module>
from superlista.lists import views
ImportError: No module named 'superlista.lists'
I can't find where is bug. It's my begining with django. If somebody helping me i will grateful.

Judging by your screenshot of your folder structure, the outer superlista is the project root, and the installed apps are superlista and lists.
If you have correctly defined the INSTALLED_APPS setting, your import should be:
from lists import views

Related

ModuleNotFoundError: No module named 'members.urls'

I tried to start a app so that I can create a Login for my blog but I came by this error.
So I thought to post a question in stack over flow. I hope yu can take some time to answer my question.
This is the trace back error,
Traceback (most recent call last):
File "C:\Users\Selvi\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\Selvi\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "C:\simpleblog\virt\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\simpleblog\virt\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\simpleblog\virt\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\simpleblog\virt\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\simpleblog\virt\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\simpleblog\virt\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\simpleblog\virt\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\simpleblog\virt\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\simpleblog\virt\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\simpleblog\virt\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\simpleblog\virt\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Selvi\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 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\simpleblog\ablog\ablog\urls.py", line 8, in <module>
path('members/', include('members.urls')),
File "C:\simpleblog\virt\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Selvi\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 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'members.urls'
Here is the url for members
from django.urls import path
from .views import UserRegisterView
urlpatterns = [
path('register/', UserRegisterView.as_view()name='register'),
]
Here is the urls for ablog
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('myblog.urls')),
path('members/', include('django.contrib.auth.urls')),
path('members/', include('members.urls')),
]
Here is my views.py
from django.shortcuts import render
from django.views import generic
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
class UserRegisterView(generic.CreateView):
form_class = UserCreationForm
template_name = 'registration/register.html'
success_url = reverse_lazy('login')
How do I solve this error.
At first Correct your urlpatterns:(you forgot , )
path('register/', UserRegisterView.as_view(),name='register'),
also make sure for migrations
I got the same problem and I was missing the urls.py file in my app.
Add urls.py file in members app.

How to setup Django every time you use it? (i.e. using **source bin/activate** and ** python manage.py runserver**)

I am a beginner with Django, and I am very confused as to how to consistently setup Django everytime you are opening a new terminal. Whenever I do so, I have to always restart and do the following:
Change directory (cd) to my trydjango directory where I start the virtual environment (source bin/activate)
After the terminal has the automatic (trydjango) at the furthest left of (trydjango) lyons-MacBook-Air:trydjango lyons$ I know that I have the virtual environment enabled.
Then, I go to the src directory through the terminal where I enter python manage.py runserver
I am running into consistent errors (e.g. AttributeError) whenever I enter the runserver step (step 3). Am I doing something wrong?
The following is the full Traceback of the error
(trydjango) lyons-MacBook-Air:src lyons$ python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d5a6b90>
Traceback (most recent call last):
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/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 "/Users/lyons/Dev/trydjango/src/trydjango/urls.py", line 22, in <module>
path('', home_view, name='home'),
NameError: name 'home_view' is not defined
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d4a3b90>
Traceback (most recent call last):
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/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 "/Users/lyons/Dev/trydjango/src/trydjango/urls.py", line 22, in <module>
path('', views.home_view, name='home'),
AttributeError: module 'pages.views' has no attribute 'home_view'
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home_view(*args, **kwargs): # args, kwargs
return HttpResponse("<h1>Hello World</h1>") # string of HTML code
urls.py
"""trydjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from pages import views #from pages import views
urlpatterns = [
path('', views.home_view, name='home'),
path('admin/', admin.site.urls),
]
It's not easy to tell you what exact steps to start a project.
django-admin startproject new_project_name
Create a new project
manage.py runserver IP:Port
Run server when start, http://127.0.0.1:8000/, or http://localhost:8000/
manage.py startapp new_APP_name
in new_project_name to create a new APP
manage.py makemigrations
in new_project_name to tell django project changed, then build a migration file.
manage.py migrate
in new_project_name to update.
in new_project_name/new_project_name/settings.py
INSTALLED_APPS add item with new_APP_name.apps.new Appconfig subclass name in new_APP_name\apps.py
in new_project_name/new_project_name/urls.py add you URL parser, for example
from . import views
urlpatterns = [
path('', views.Topic, name='index')
]
in new_project_name/new_APP_name/admin.py register model for example,
from .models import Topic
admin.site.register(Topic)

application call . TypeError: view must be a callable or a list/tuple in the case of include()

I am taking a course where this error does not occur
my error is cod is:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bo
otstrap_inner
self.run()
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\auto
reload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\manag
ement\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\manag
ement\base.py", line 392, in check
all_issues = self._run_checks(
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\manag
ement\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\check
s\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\check
s\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\check
s\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resol
vers.py", line 407, in check
for pattern in self.url_patterns:
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\func
tional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resol
vers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\func
tional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\resol
vers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 12
7, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\KursDjango\skcms\skcms\urls.py", line 22, in <module>
url(r'article/',include('articles.urls'))
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\conf.
py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 12
7, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\KursDjango\skcms\articles\urls.py", line 4, in <module>
url(r'^show_all/$', 'articles.views.articles'),
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\urls\
__init__.py", line 13, in url
return re_path(regex, view, kwargs, name)
File "C:\Users\wuoel\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\urls\conf.
py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
urls.py
from django.conf.urls import include, url from django.urls import
include
urlpatterns =('',
url(r'article/',include('articles.urls')) )
articles/urls.py
from django.conf.urls import *
urlpatterns =('',
url(r'^show_all/$', 'articles.views.articles'),
url(r'^(?P\d+)/$', 'articles.views.article'), )
It looks like you are passing the string representation of the name to include() rather than the actual articles.urls variable. Instead, it should look something like this:
from articles import urls as article_urls
...
urlpatterns =[
url(r'article/',include(article_urls, namespace='article'))
]
the problem is explained in this way:
skcms/urls.py
from django.urls import include, path
urlpatterns =[
path('article/',include('articles.urls')),
]
articles/urls.py
from django.urls import path
from . import views
urlpatterns =[
path('', views.articles, name='articles'),
path('', views.article, name='article'),
]

Django Newbie error: - TypeError: view must be a callable or a list/tuple in the case of include()

I am a total newbie to python Django and would like to know why are views not being created.
here's my urls.py which is under myproject.
from django.contrib import admin
from django.urls import path,include
from django.conf.urls import url
admin.autodiscover()
urlpatterns = ['',
path('admin/', admin.site.urls),
url('myapp/', include('myapp.url'))
]
here's my view.py which is in myapp folder
from django.shortcuts import render
# Create your views here.
def hello(request):
return render(request,"myapp/templates/hello.html", {})
here's my url.py in myapp folder
from django.conf.urls import include, url
urlpatterns = ['', url('hello/', 'views.hello', name='hello'),]
and here's the complete traceback
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/management/base.py", line 395, in check
include_deployment_checks=include_deployment_checks,
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/management/base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/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 "/home/mayureshkadam/PycharmProjects/Learning-Django/myproject/myproject/urls.py", line 23, in <module>
url('myapp/', include('myapp.url'))
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/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 "/home/mayureshkadam/PycharmProjects/Learning-Django/myproject/myapp/url.py", line 3, in <module>
urlpatterns = ['', url('hello/', 'views.hello', name='hello'),]
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/conf/urls/__init__.py", line 13, in url
return re_path(regex, view, kwargs, name)
File "/home/mayureshkadam/Learning-Django/lib/python3.7/site-packages/django/urls/conf.py", line 73, in _path
raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
i've read a few tutorials and the code is being referred from https://www.tutorialspoint.com/django/django_url_mapping.htm but not sure why i am getting an error.
Just replace the url with path:
# main urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.url'))
]
# app urls.py
from django.urls import path
from .view import hello
urlpatterns = [
path('hello/', hello, name='hello'),
]
As you can see in documetation, path has been introduced(from Django>=2.0), so you don't have to use url here. The tutorial you are following is for older versions, so it is using url with regex to generate URLs.

AttributeError: module 'django.contrib.auth.views' has no attribute 'LoginView'

I am using an older version of django.After i run my code i get this error -"AttributeError: module 'django.contrib.auth.views' has no attribute 'LoginView'".I should get error if I use login instead of LoginView. Even after using login I get the same attribute error.
this is my urls.py-
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r"login/$",auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
url(r"logout/$", auth_views.LogoutView.as_view(), name="logout"),
url(r"signup/$", views.SignUp.as_view(), name="signup"),
]
this is my app's urls.py-
from django.conf.urls import url,include
from django.contrib import admin
from .import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.HomePage.as_view(),name='home'),
url(r'^accounts/',include('accounts.urls',namespace='accounts')),
url(r'^accounts',include('django.contrib.auth.urls')),
url(r'^test/$',views.TestPage.as_view(),name='test'),
url(r'^thanks/$',views.ThanksPage.as_view(),name='thanks')
]
and views.py-
from django.shortcuts import render
from django.contrib.auth import login, logout
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView
from . import forms
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("login")
template_name = "accounts/signup.html"
I got this result after making migrations.My app's name is accounts:-
(myDjangoEnv) C:\Users\saini computers\Desktop\simple_social_clone\simplesocial>python manage.py makemigrations accounts
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 342, in execute
self.check()
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\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
Git
GitHub
Initialize a new project directory with a Git repository
Create repository
simplesocial\accounts\views.py14:1(13, 372)
LFUTF-8PythonGitHubGit (0)
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 "C:\Users\saini computers\Desktop\simple_social_clone\simplesocial\simplesocial\urls.py", line 23, in <module>
url(r'^accounts/',include('accounts.urls',namespace='accounts')),
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\saini computers\Anaconda3\envs\myDjangoEnv\lib\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 "C:\Users\saini computers\Desktop\simple_social_clone\simplesocial\accounts\urls.py", line 8, in <module>
url(r"login/$", auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
AttributeError: module 'django.contrib.auth.views' has no attribute 'LoginView'
LoginView was added in Django 1.11. If the import fails, you must be using an older version of Django.
Django 1.11 LTS is the oldest supported version of Django, so you really should upgrade, ideally to the latest version 2.2.
It is always a good idea to say which version you are using. Anyway, the class "LoginView" was implemented in Django 1.11 (https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.views.LoginView)
If your version is compatible with that, try to change the url part to something like:
path('accounts/login/',auth_views.LoginView.as_view(template_name='myapp/login.html'))
Hope it helps

Categories