NameError: name 'base' is not defined(haystack search) - python

I wanted to include full text search in my django application. I am using whoosh-haystack for this.When I include whoosh and haystack in my installed apps,and execute the command ./manage.py, I am getting an import error. Can anyone sort this out.
settings.py
INSTALLED_APPS = {
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'search',
'whoosh',
'haystack',
}
when I make migration in my model the error which I got is:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python \Python36\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\conf\__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\Samad Talukder\AppData\Local\Programs\Python\Python36\lib\site-packages\django\conf\__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\Samad Talukder\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\Samad Talukder\Desktop\django-env\search\search\settings.py", line 80, in <module>
'PATH': os.path.join(base(), 'whoosh_index')
NameError: name 'base' is not defined
my haystack connection:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(base(), 'whoosh_index')
},
}
my models.py:
from django.contrib.auth.models import User
from django.db import models
class Author(User):
pass
class Book(models.Model):
title = models.CharField(max_length=300)
author = models.ForeignKey(Author)
isbn = models.CharField(max_length=300)
resume = models.TextField()
def __unicode__(self):
return self.title
for more information I install haystack and whoosh in my django project by django pip install method like this:
pip install haystack
pip install whoosh

The traceback is quite clear:
File "C:\Users\Samad Talukder\Desktop\django-env\search\search\settings.py", line 80, in <module>
'PATH': os.path.join(base(), 'whoosh_index')
NameError: name 'base' is not defined
This means that line #80 of your settings.py file uses a name (base - obviously expected to be a function) that is not defined. Your settings file is missing either an import or a function definition. What base is supposed to do etc is beyond our knowledge (it's definitly not a builtin, and nothing standard in a django settings file) but you should now since it's your project.

Related

Django app NameError however the app is installed

I'm trying to modify Django's built-in authetnication system by adding a custom user model. The customized model is defined inside an app named accounts:
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
# Create your models here.
COUNTRIES = settings.COUNTRY_CHOICES
class CustomUser(AbstractUser):
zip_code = models.PositiveSmallIntegerField(blank=True, null=True)
city = models.CharField(blank=True, null=True, max_length=64)
address_street = models.CharField(blank=True, null=True, max_length=64)
address_number = models.CharField(blank=True, null=True, max_length=32)
country = models.CharField(blank=True, null=True, choices=COUNTRIES, max_length=8)
I updated the settings.py file this way:
AUTH_USER_MODEL = accounts.CustomUser
I added accounts as an installed app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'store',
'cart',
]
When I try to run the python manage.py makemigrations command in terminal to apply changes made to the user model Django's throwing this error:
Traceback (most recent call last):
File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 22, in <module>
main()
File "C:\Users\uhlar\Dev\ecommerce\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\core\management\__init__.py", line 386, in execute
settings.INSTALLED_APPS
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 87, in __getattr__
self._setup(name)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 74, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\uhlar\.virtualenvs\ecommerce-vU6zMECh\lib\site-packages\django\conf\__init__.py", line 183, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\uhlar\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\uhlar\Dev\ecommerce\config\settings.py", line 149, in <module>
AUTH_USER_MODEL = accounts.CustomUser
NameError: name 'accounts' is not defined
The error is not only confined to the accounts app, no matter inside which app I try to define the CustomUser model I'm still getting it.
If anyone knows what's causing the problem please help me to resolve it.
The AUTH_USER_MODEL setting [Django-doc] should be a string, not a (qualified) name, so:
# settings.py
# …
# string literal 🖟 🖟
AUTH_USER_MODEL = 'accounts.CustomUser'
As Willem mentioned, you need to update your AUTH_USER_MODEL setting inside settings.py to accounts.CustomerUser model. In addition, you will have to update your schema manually as when you first run migrations, Django will use the default auth.User for multiple foreign keys etc. See the docs here for changing a user mid project.

Problem with Django not finding a module 'learning_logs'

I've been trying to fix this Django issue. I've just started this project and I'm fairly new to this language so I don't know where exactly is the issue.
Error:
Traceback (most recent call last):
File "C:\Users\user\Desktop\Projects Python\Django\manage.py", line 22, in <module>
main()
File "C:\Users\user\Desktop\Projects Python\Django\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\user\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 'learning_logs'
models.py:
from django.db import models
# Create your models here.
class Topic(models.Model):
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.text
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'learning_logs',
]
Any help I would really appreciate it.
In settings.py, the Installed apps should be written as 'app_name.apps.appconfig'.
The 'appconfig' is the same as the class name in app/apps.py
In you case it might be something like learning_logs.apps.LearningLogsConfig

Unable to override both contrib.admin.AdminSite and django.contrib.auth.admin.UserAdmin simultaneously

Using Django 3.1 and Python 3.6, I'm trying to both override the default AdminSite class, and the UserAdmin. I'm trying to combine the examples from two different sections of the documentation:
Overriding the default admin site
Extending the existing User model
Originally I had just the overriden AdminSite class, but then the problem arose when I tried to also override UserAdmin.
Both of these work independently. I can either override AdminSite and get a custom header, or UserAdmin and get my inline showing.
For whatever reason, I run into an import error: ImportError: Module "core.admin" does not define a "CoreCustomAdmin" attribute/class. It looks like a sort of circular dependency, but that's just a guess, as I cannot figure out where/how it's happening.
Stack trace:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 20, in import_string
return getattr(module, class_name)
AttributeError: module 'core.admin' has no attribute 'CoreCustomAdmin'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 44, in autodiscover_modules
if register_to:
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/functional.py", line 240, in inner
self._setup()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 540, in _setup
AdminSiteClass = import_string(apps.get_app_config('admin').default_site)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/infernous/project_name/src/core/admin.py", line 2, in <module>
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/auth/admin.py", line 26, in <module>
class GroupAdmin(admin.ModelAdmin):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/decorators.py", line 21, in _model_admin_wrapper
if not isinstance(admin_site, AdminSite):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/functional.py", line 240, in inner
self._setup()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 540, in _setup
AdminSiteClass = import_string(apps.get_app_config('admin').default_site)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 24, in import_string
) from err
ImportError: Module "core.admin" does not define a "CoreCustomAdmin" attribute/class
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 20, in import_string
return getattr(module, class_name)
AttributeError: module 'core.admin' has no attribute 'CoreCustomAdmin'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 24, in ready
self.module.autodiscover()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 53, in autodiscover_modules
if register_to:
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/functional.py", line 240, in inner
self._setup()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 540, in _setup
AdminSiteClass = import_string(apps.get_app_config('admin').default_site)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
module = import_module(module_path)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/infernous/project_name/src/core/admin.py", line 2, in <module>
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/auth/admin.py", line 26, in <module>
class GroupAdmin(admin.ModelAdmin):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/decorators.py", line 21, in _model_admin_wrapper
if not isinstance(admin_site, AdminSite):
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/functional.py", line 240, in inner
self._setup()
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 540, in _setup
AdminSiteClass = import_string(apps.get_app_config('admin').default_site)
File "/home/infernous/project_name/.venv/lib/python3.6/site-packages/django/utils/module_loading.py", line 24, in import_string
) from err
ImportError: Module "core.admin" does not define a "CoreCustomAdmin" attribute/class
I do, in fact, have the default admin not loaded (that gives an error anyway).
settings.py:
INSTALLED_APPS = [
...
'core.apps.CoreCustomAdmin',
...
# 'django.contrib.admin', # Commented out
The problem file? Changing the ordering of the imports does nothing, which is expected since it is Python:
core/admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User, Group
from allauth.account.models import UserData
class CoreCustomAdmin(admin.AdminSite):
site_header = 'Custom Header'
class UserDataInline(admin.StackedInline):
model = UserData
can_delete = False
class CustomUserAdmin(BaseUserAdmin):
inlines = (UserDataInline,)
admin.site = CoreCustomAdmin()
admin.site.register(User, CustomUserAdmin)
admin.site.register(Group)
The only place where I import core.admin. I have tried change admin.site = CoreCustomAdmin() from core/admin.py to something like customadmin = CoreCustomAdmin() and using that, but it did nothing, naturally.
urls.py
from django.urls import path, include
from core.admin import admin
app_name = 'project_name'
urlpatterns = [
...
path('admin/', admin.site.urls),
]
core/apps.py
from django.apps import AppConfig
from django.contrib.admin.apps import AdminConfig
class CoreConfig(AppConfig):
name = 'core'
class CoreCustomAdmin(AdminConfig):
default_site = 'core.admin.CoreCustomAdmin'
Thanks!
I accidentally fixed it after coming back to the problem 3 weeks later and spending 5 minutes on it. I changed the line in settings.py from
'core.apps.CoreCustomAdmin',
to
'base.apps.AdminConfig',
The reason it's base.apps instead of core.apps is because I'd renamed the app to "base" as per the comment on the question, in an attempt to solve the problem. Had I not done so, it would have been just changing CoreCustomAdmin to AdminConfig.
As far as I can tell, it's achieved the desired effect. It still has both the custom header on the admin site, as well as the inline on the User model edit page.

Set AUTH_USER_MODEL from apps inside folder from settings.py

I'm new to django and I'm trying to extend the user class in one of my apps but I'm having trouble redirecting from setting.py to the model that contains my AbstractUser
My Nproject:
-apps:
--profile:
---__init__.py
---apps.py
---forms.py
---models.py
---urls.py
---views.py
--apps1
-media
-static
-templates
-Nproject:
--__init__.py
--settings.py
--urls.py
--wsgi.py
model in apps.profile.models
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
address = models.CharField(max_length=255)
def __str__(self):
return '%s %s' % (self.email, self.username)
setting.py and here I think this is my mistake
AUTH_USER_MODEL = 'apps.User' #not working
I think the error is in AUTH_USER_MODEL that I do not know how to properly reference the model User
Research some sites and tell me that you should use the init.py file but it is not working
init in apps.profile
from .models import User
Error:
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/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 "/home/projects/Nproject/apps/perfil/__init__.py", line 1, in <module>
from .models import User
File "/home/projects/Nproject/apps/perfil/models.py", line 2, in <module>
from django.contrib.auth.models import User
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/projects/.virtualenvs/Nprojects/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Additional data:
-Django 2.1
-Python 3.6.5
The correct name string for the AUTH_USER_MODEL would be appname.modelname. In your case, I suppose it is 'profile.user' (it could be different if you changed the app name by yourself)

I'm getting problems uploading images in django

This is the models.py file. Although you can see that i have used MEDIA_URL as the directory for the image to be saved, i have also messed around with all other alternatives getting similar error messages. Could this have something to do with permissions? im not sure.
from django.db import models
from taggit.managers import TaggableManager
class Post(models.Model):
title = models.CharField(max_length = 100)
body = models.TextField()
created = models.DateTimeField()
image1 = models.ImageField(upload_to = MEDIA_URL)
def __unicode__(self):
return self.title
This is the settings part i have added.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
And this is the error message i am getting in the cmd(using windows)
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 284, in execute
self.validate()
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "C:\Python34\lib\site-packages\django\core\management\validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Python34\lib\site-packages\django\db\models\loading.py", line 196, in get_app_errors
self._populate()
File "C:\Python34\lib\site-packages\django\db\models\loading.py", line 75, in _populate
self.load_app(app_name, True)
File "C:\Python34\lib\site-packages\django\db\models\loading.py", line 99, in load_app
models = import_module('%s.models' % app_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:\Users\Neema\sonder\blog\models.py", line 4, in <module>
class Post(models.Model):
File "C:\Users\Neema\sonder\blog\models.py", line 8, in Post
image1 = models.ImageField(upload_to = MEDIA_URL)
NameError: name 'MEDIA_URL' is not defined
I think you need to add a import to your models.py:
from django.conf import settings
And then you can use it:
settings.MEDIA_URL
Anyway, I don't think it is a good practice to add the MEDIA_URL to your field. If in the future you decide to change it, you need to update all the DB entries.
Don't mention MEDIA_URL in your models.py at all. Just mention a subdirectory name. For e.g.
image1 = models.ImageField(upload_to="blogimages")
Make sure that the media folder has write access by server (Apache or IIS). This is a common problem in Windows. It can be fixed easily by right clicking the folder and changing the permissions.
If you still get an error, please post that as well.

Categories