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)
Related
I am pretty new to Django and I was just following along witht the tutorial.
I was making a project from https://github.com/justdjango/getting-started-with-django, and after I added users and a superuser, I started to get a Traceback like this:
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\__init__.py", line 160, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\apps\registry.py", line 204, in get_model
app_label, model_name = app_label.split('.')
ValueError: not enough values to unpack (expected 2, got 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Dell\Desktop\prog\manage.py", line 22, in <module>
main()
File "C:\Users\Dell\Desktop\prog\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\apps\registry.py", line 122, in populate
app_config.ready()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
self.module.autodiscover()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\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 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\admin.py", line 6, in <module>
from django.contrib.auth.forms import (
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\forms.py", line 21, in <module>
UserModel = get_user_model()
File "C:\Users\Dell\Desktop\prog\programa\lib\site-packages\django\contrib\auth\__init__.py", line 162, in get_user_model
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL must be of the form 'app_label.model_name'
You forgot to specify the model name in the AUTH_USER_MODEL setting. If you defined a model User in the leads app, you thus set this with:
# settings.py
# app name ↓
AUTH_USER_MODEL = 'leads.User'
# model name ↑
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.
I am new python also Django. I know the same issue is posted in this forum. but still, can't figure it out. created a model and need to import in my views.py file so I can render my data.
this is my views.py
from first_app.models import Topic, webpage, AccessRecord
from django.shortcuts import render
from django.http import HttpResponse
# from django.apps import AppConfig
# Create your views here.
def index(request):
webpage_list = AccessRecord.objects.order_by('date')
date_dict = {'acess_records': webpage_list}
return render(request, 'first_app/index.html')
and it showed me this error
PS E:\python\firstp\first_project> python manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000024518AB58C8>
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 112, in inner_run
autoreload.raise_last_exception()
File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 327, in execute
autoreload.check_errors(django.setup)()
File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\apps\registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\ProgramData\Anaconda3\lib\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 "E:\python\firstp\first_project\first_app\models.py", line 4, in <module>
class Topic(models.Model):
File "C:\ProgramData\Anaconda3\lib\site-packages\django\db\models\base.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "C:\ProgramData\Anaconda3\lib\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.
I tried many things from previous post but still no progress.
github repository
YOu actually forget to register your app, go to settings.py, you will see an list named INSTALLED_APPS, add the name of your app first_app.
More info can be found here
INSTALLED_APPS = [
#names,
'first_app',
]
So I'm trying to add some code to my Django 1.10 app that will only run when the server starts. I took the instructions on how to do this from here and here. Every time I try running the python console I get this:
C:\Users\username\Envs\projectName [master ≡ +0 ~2 -0 !]> python manage.py shell
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 341, in execute
django.setup()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\username\AppData\Local\Programs\Python\Python3\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 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
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\username\Envs\projectName\appName\apps.py", line 2, in <module>
import appName.osm as osm
File "C:\Users\username\Envs\projectName\appName\osm.py", line 4, in <module>
from .models import Country, Embassy
File "C:\Users\username\Envs\projectName\appName\models.py", line 38, in <module>
class Country(models.Model):
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I tried looking this up but it seems that everyone who gets this exception is because it relates to an update issue with Django, however I've been using 1.10 for a long time now (not sure if I ever touched anything older). Any idea what's going on?
projectName/appName/apps.py:
from django.apps import AppConfig
import finder.osm as osm # this seems to cause the issue
class FinderConfig(AppConfig):
name = 'finder'
init = True
def ready(self):
pass
projectName/appName/__init__.py:
default_app_config = 'appName.apps.AppNameConfig'
projectName/appName/models.py:
from django.db import models
class Country(models.Model):
#...
class Embassy(models.Model):
#...
As #Andrey Shipilov has commented, it was a case of bad imports as you cannot import on the same level as the AppConfig class. A fix to my solution was to move the import into AppConfig.ready()
projectName/appName/apps.py:
from django.apps import AppConfig
class FinderConfig(AppConfig):
import finder.osm as osm
name = 'finder'
init = True
def ready(self):
pass
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.