Django ModuleNotFoundError: No module named 'views' - python

In my Django Project market I created an app called main. In main I created urls.py
main/urls.py:
from django.urls import path
import views
url_patterns = [
path('home/', views.homepage, name='home'),
]
main/views.py:
from django.shortcuts import render, HttpResponse
# Create your views here.
def homepage(request):
return HttpResponse("<h1> Hello World </h1>" )
market/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls'))
]
But when I try to run:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Python38\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Python38\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Python38\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\Python38\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Python38\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Python38\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Python38\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\Python38\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python38\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python38\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python38\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python38\lib\importlib\__init__.py", line 127, 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:\worksapce\jimDjango\market\market\urls.py", line 21, in <module>
path('', include('main.urls'))
File "C:\Python38\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python38\lib\importlib\__init__.py", line 127, 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:\worksapce\jimDjango\market\main\urls.py", line 2, in <module>
import views
ModuleNotFoundError: No module named 'views'
Why won't this run?
Edit:
I also tried replacing import views in main/urls.py with from market.main import views. Doesn't help.

I found two problems with what I did:
I didn't import views.py correctly. I should've used from . import views instead of import views in main/urls.py. (Thanks to user #bdbd)
In main/urls.py I wrote url_patterns instead of urlpatterns

Related

cannot import name 'Product' from 'products.models'

I am new to django, i am following a tutorial i do same things but got an error
Im trying to make view for products app this is products.models
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.TextField()
description = models.TextField()
price = models.TextField()
products.views
from django.shortcuts import render
from .models import Product
def product_detail_view(request):
obj = Product.objects.get(id=1)
context = {
'title':obj.title,
'description':obj.description
}
return render(request, "product/detail.html", context)
prouducts .urls
from django.urls import path
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('product',views.product_view_detail, name='Home'),
]
urls.py
from new1.settings import MEDIA_ROOT
from django import urls
from django.contrib import admin
from django.urls import path
from pages import views
from django.urls import include, path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('pages.urls'), name='Home'),
path('product',include('products.urls')),
path('admin/', admin.site.urls),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += staticfiles_urlpatterns()
I got this error cannot import name 'Product' from 'products.models'
it works before i add products views
Exception in thread django-main-thread:
Traceback (most recent call last):
File "g:\64-bit\new folder\py38 64bit\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "g:\64-bit\new folder\py38 64bit\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "g:\64-bit\new folder\py38 64bit\lib\importlib\__init__.py", line 127, 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:\Users\aram\Desktop\cfeproj\new1\urls.py", line 33, in <module>
path('product',include('products.urls')),
File "C:\Users\aram\.virtualenvs\cfeproj-ldwjYLGf\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "g:\64-bit\new folder\py38 64bit\lib\importlib\__init__.py", line 127, 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:\Users\aram\Desktop\cfeproj\products\urls.py", line 3, in <module>
from . import views
File "C:\Users\aram\Desktop\cfeproj\products\views.py", line 2, in <module>
from .models import Product
ImportError: cannot import name 'Product' from 'products.models' (C:\Users\aram\Desktop\cfeproj\products\models.py)
At your settings add 'products' at installed app then makemigrations and migrate the
migrations then try again

ModuleNotFoundError: No module named 'api.serlializer'

I've installed Django 3.1.4 and djangorestframework 3.12.2.
I am attempting to run makemigrations for a music website I am building and I keep getting this error in the terminal and haven't been able to figure out what is causing it.
(Django_React) PS C:\Users\BB_SO\desktop\dev\Django_React\music_site> python .\manage.py makemigrations Traceback (most recent call last):
File ".\manage.py", line 22, in <module>
main()
File ".\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\base.py", line 368, in execute
self.check()
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\BB_SO\desktop\dev\Django_React\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\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\BB_SO\anaconda3\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\BB_SO\desktop\dev\Django_React\music_site\music_site\urls.py", line 19, in <module>
urlpatterns = [path("admin/", admin.site.urls), path("", include("api.urls"))]
File "C:\Users\BB_SO\desktop\dev\Django_React\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\BB_SO\anaconda3\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\BB_SO\desktop\dev\Django_React\music_site\api\urls.py", line 2, in <module>
from .views import RoomView
File "C:\Users\BB_SO\desktop\dev\Django_React\music_site\api\views.py", line 3, in <module>
from .serlializer import RoomSerializer
ModuleNotFoundError: No module named 'api.serlializer'
Here my serializer.py file content:
from rest_framework import serializer
from .models import Room
class RoomSeralizer(seriarlizers.ModelSerializer):
class Meta:
model = Room
fields = (
"id",
"code",
"host",
"guest_can_pause",
"vote_to_skip",
"created_at",
)
Does anyone know what might be causing this error? I am still very new to coding and am using vscode if that helps at all. Thanks in advance for your help!
In serializer.py Try:
from rest_framework import serializers
Django-rest framework doesn't have any API called serializer.
More: https://www.django-rest-framework.org/api-guide/serializers/
import should be
from rest_framework import serializers

I'm trying to graphic with django using python but jump IndentationError: expected an indented block

Looks like simple but I'm starting code and I don't know what to do or why this error happened.
What I'm trying is to make an example graphic in django that's all not with success, all the information showed is confusing me.
this is my views.py
from django.shortcuts import render
import matplotlib.pyplot as plt
def home(request):
plt.plot(range(10))
return render(request, 'app1/home.html')
def about(request):
return render(request, 'app1/about.html', {'title': 'About'})
and jump the next error
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 950, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/core/management/base.py", line 392, in check
all_issues = checks.run_checks(
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.9/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 "/home/corbin/ProyectoDB/LabBD/Web_DB/urls.py", line 21, in <module>
path('', include('app1.urls')),
File "/home/corbin/ProyectoDB/venv/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.9/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 "/home/corbin/ProyectoDB/LabBD/app1/urls.py", line 2, in <module>
from . import views
File "/home/corbin/ProyectoDB/LabBD/app1/views.py", line 11
^
IndentationError: expected an indented block
I will apreciate all your help.

How i fix this Unable to import 'django.urls' error?

Errors are:
1. No name 'urls' in module 'django'pylint(no-name-in-module)
Unable to import 'django.urls'
2. No name 'test' in module 'django'pylint(no-name-in-module)
Unable to import 'django.test'pylint(import-error)
3. No name 'http' in module 'django'pylint(no-name-in-module)
Unable to import 'django.http'pylint(import-error)
When I start the server the terminal shows me this -
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "c:\users\aviba\appdata\local\programs\python\python38-32\Lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "c:\users\aviba\appdata\local\programs\python\python38-32\Lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\management\base.py", line 387, in check
all_issues = self._run_checks(
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\aviba\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\aviba\Envs\test\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "C:\Users\aviba\Envs\test\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\aviba\Envs\test\lib\importlib\__init__.py", line 127, 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:\Users\aviba\djangoproject\firsttry\firsttry\urls.py", line 20, in <module>
path('',include('firstapp.urls')),
File "C:\Users\aviba\Envs\test\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\aviba\Envs\test\lib\importlib\__init__.py", line 127, 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:\Users\aviba\djangoproject\firsttry\firstapp\urls.py", line 2, in <module>
from . import views
File "C:\Users\aviba\djangoproject\firsttry\firstapp\views.py", line 2, in <module>
from django.http import httpresponse
ImportError: cannot import name 'httpresponse' from 'django.http' (C:\Users\aviba\Envs\test\lib\site-packages\django\http\__init__.py)
urls.py of the app
from django.urls import path
from . import views
urlpattarns = [
path('',views.home, name='home'),
]
views.py of the app
from django.shortcuts import render
from django.http import httpresponse
def home(request):
return httpresponse("Hello Django")
urls.py of the project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('',include('firstapp.urls')),
path('admin/', admin.site.urls),
]
modules in python are case sensitive, it's HttpResponse not httpresponse
Try this as views.py:
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello Django")
Here httpresponse is case sensitive and hence we use it this way.
Read this it might help you more.
Make sure you are using Python3 as interpreter. I had the same issue (using Django 3.1.2) and after selecting the right interpreter (python 3.7.8) it was fixed.

How to fix attribute error in Django(Python)?

I'm newbie to Python and Django.
I'm watching https://www.youtube.com/watch?v=a48xeeo5Vnk&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p&index=2 this course and following instructions.
But there is and error message keeps coming out like this.
AttributeError: module 'blog.views' has no attribute 'post_list'
C:\Users\Administrator\PycharmProjects\django_project\blog\urls.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 398, in check
for pattern in self.url_patterns:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 579, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\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\Administrator\PycharmProjects\django_project\django_project\urls.py", line 6, in <module>
path('blog/', include('blog.urls'))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\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\Administrator\PycharmProjects\django_project\blog\urls.py", line 5, in <module>
path('', views.home, name='post_list'),
AttributeError: module 'blog.views' has no attribute 'home'
At pylint, they said Module 'blog.views' has no 'home'
So I think there is no home function at apps.py but I already call home function like this.
def home(request):
return HttpResponse('<h1>Blog Home</h1>')
blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
]
django_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls'))
]
blog/apps.py
from django.apps import AppConfig
from django.http import HttpResponse
def home(request):
return HttpResponse('<h1>Blog Home</h1>')
I can't find the problem.
With
path('', views.home, name='post_list')
you're saying the view home is in the module views, but as you already said, it is in apps.py, hence the error: Module 'blog.views' has no 'home'.
Just move your function/view home to the blog.views module.
You might have to register your app in the settings file.
You want to look for a list of apps the add your app blog to that list.

Categories

Resources