While trying to load custom templtetags, I got error
'myapp' is not a valid tag library: ImportError raised loading myapp.templatetags.myapp: No module named models
The problem is in templatetags/myapp.py is not been able to load model -> “myapp: No module named models”. Why following line fails in myapp/templatetags/myapp.py?
from myapp.models import Mymodel
To give some details, I have following:
myproject
|
--myapp
| |
| --templatetags
| | |
| | --__init__.py
| | --myapp.py
| --__init__.py
| --models.py
| --views.py
|
--myapp2
| |
| --templatetags
| --__init__.py
| --models.py
| --views.py
In the template/myapp2/showsomething.html, I have following
{% load myapp %}
In the myapp/templatetags/myapp.py, I have following
from myapp.models import Mymodel
register = Library()
@register.inclusion_tag('myapp/myapp.html')
def myapp():
#....somecode ...
return {'some': 'value}
In the myapp/models.py, I have following
from django.db import models
class Mymodel(models):
...some model code
In settings.py, I have
...some other settings code ...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'myapp',
'myapp2',
)
Django is probably confused because of same name. Try to rename the file myapp.py to something else.
Also, You need to add the template tag app to the Installed apps and make sure the template tag has the
__init__.pyfile. More information could be find here