Importing Static files to template with Django - python

I'm having trouble loading my CSS file into my HTML template. Below are the invovled files I'm working with. Can anyone see why the CSS wouldn't be loading?
settings.py
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
words.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'hello/words.css' %}" />
urls.py
urlpatterns = patterns('',
url(r'^
url(r'^Words', hello.views.index, name='index'),
url(r'^db', hello.views.db, name='db'),
url(r'^Add', hello.views.create, name='create'),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += staticfiles_urlpatterns()
words.css
.pri {
color: blue;
}, hello.views.index, name='index'),
Project Structure
murmurwall/
├── Procfile
├── README.md
├── hello
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── admin.cpython-34.pyc
│   │   ├── forms.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── management
│   │   └── commands
│   │   ├── CSV
│   │   │   └── Pretty\ Little\ Liars.csv
│   │   ├── __pycache__
│   │   │   └── update_words.cpython-34.pyc
│   │   └── update_words.py
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   │   └── hello
│   │   └── words.css
│   ├── templates
│   │   ├── add_word.html
│   │   ├── base.html
│   │   ├── db.html
│   │   └── words.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── manage.py
├── murmurwall
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── static
│   │   └── humans.txt
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── requirements.txt
├── runtime.txt
└── staticfiles
├── admin
│   ├── css
│   │   ├── base.css
│   │   ├── changelists.css
│   │   ├── dashboard.css
│   │   ├── forms.css
│   │   ├── ie.css
│   │   ├── login.css
│   │   ├── rtl.css
│   │   └── widgets.css
│   ├── img
│   │   ├── changelist-bg.gif
│   │   ├── changelist-bg_rtl.gif
│   │   ├── default-bg-reverse.gif
│   │   ├── default-bg.gif
│   │   ├── deleted-overlay.gif
│   │   ├── gis
│   │   │   ├── move_vertex_off.png
│   │   │   └── move_vertex_on.png
│   │   ├── icon-no.gif
│   │   ├── icon-unknown.gif
│   │   ├── icon-yes.gif
│   │   ├── icon_addlink.gif
│   │   ├── icon_alert.gif
│   │   ├── icon_calendar.gif
│   │   ├── icon_changelink.gif
│   │   ├── icon_clock.gif
│   │   ├── icon_deletelink.gif
│   │   ├── icon_error.gif
│   │   ├── icon_searchbox.png
│   │   ├── icon_success.gif
│   │   ├── inline-delete-8bit.png
│   │   ├── inline-delete.png
│   │   ├── inline-restore-8bit.png
│   │   ├── inline-restore.png
│   │   ├── inline-splitter-bg.gif
│   │   ├── nav-bg-grabber.gif
│   │   ├── nav-bg-reverse.gif
│   │   ├── nav-bg-selected.gif
│   │   ├── nav-bg.gif
│   │   ├── selector-icons.gif
│   │   ├── selector-search.gif
│   │   ├── sorting-icons.gif
│   │   ├── tooltag-add.png
│   │   └── tooltag-arrowright.png
│   └── js
│   ├── LICENSE-JQUERY.txt
│   ├── SelectBox.js
│   ├── SelectFilter2.js
│   ├── actions.js
│   ├── actions.min.js
│   ├── admin
│   │   ├── DateTimeShortcuts.js
│   │   └── RelatedObjectLookups.js
│   ├── calendar.js
│   ├── collapse.js
│   ├── collapse.min.js
│   ├── core.js
│   ├── inlines.js
│   ├── inlines.min.js
│   ├── jquery.init.js
│   ├── jquery.js
│   ├── jquery.min.js
│   ├── prepopulate.js
│   ├── prepopulate.min.js
│   ├── timeparse.js
│   └── urlify.js
├── hello
│   └── words.css
└── humans.txt

Your STATIC_ROOT = 'staticfiles' need to be the absolute path to the static files. For example STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/")). This might have to be changed for deployment. An example i use for openshift:
if 'OPENSHIFT_REPO_DIR' in os.environ:
STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'), 'wsgi', 'static')
else:
STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))
See also django static static url static root

Related

Problem locating media file using Django's static function in template

I am using Django 3.2
I am trying to integrate a blog app that has a slightly convoluted directory structure for its static assets. Here is the relevant part of the tree:
Blog app static assets directory structure
blog/static/blog/
├── css
│   ├── bona
│   │   ├── comment.css
│   │   ├── detail-page.css
│   │   ├── prism.css
│   │   ├── responsive.css
│   │   ├── styles
│   │   └── styles.css
│   ├── common-css
│   │   ├── bootstrap.css
│   │   ├── fontawesome-free
│   │   ├── ionicons.css
│   │   ├── ionicons.min.css
│   │   └── swiper.css
│   └── tinymce
│   ├── github.css
│   └── tomorrow-night-blue.css
├── font
│   ├── fontello.eot
│   ├── fontello.svg
│   ├── fontello.ttf
│   ├── fontello.woff
│   └── fontello.woff2
├── fonts
│   ├── Aileron
│   │   ├── Aileron-Black.otf
│   │   ├── Aileron-Bold.otf
│   │   ├── Aileron-Heavy.otf
│   │   ├── Aileron-Italic.otf
│   │   ├── Aileron-Light.otf
│   │   ├── Aileron-Regular.otf
│   │   ├── Aileron-SemiBold.otf
│   │   ├── Aileron-Thin.otf
│   │   ├── Aileron-UltraLight.otf
│   │   └── Gidole-Regular.ttf
│   ├── Colaborate
│   │   ├── ColabBol.otf
│   │   ├── ColabLig.otf
│   │   ├── ColabMed.otf
│   │   ├── ColabReg.otf
│   │   └── ColabThi.otf
│   ├── ionicons.eot
│   ├── ionicons.svg
│   ├── ionicons.ttf
│   ├── ionicons.woff
│   ├── linea-basic-10.eot
│   ├── linea-basic-10.svg
│   ├── linea-basic-10.ttf
│   ├── linea-basic-10.woff
│   ├── Material-Design-Iconic-Font.eot
│   ├── Material-Design-Iconic-Font.svg
│   ├── Material-Design-Iconic-Font.ttf
│   ├── Material-Design-Iconic-Font.woff
│   ├── Material-Design-Iconic-Font.woff2
│   ├── Roboto
│   │   ├── LICENSE.txt
│   │   ├── Roboto-BlackItalic.ttf
│   │   ├── Roboto-Black.ttf
│   │   ├── Roboto-BoldItalic.ttf
│   │   ├── Roboto-Bold.ttf
│   │   ├── Roboto-Italic.ttf
│   │   ├── Roboto-LightItalic.ttf
│   │   ├── Roboto-Light.ttf
│   │   ├── Roboto-MediumItalic.ttf
│   │   ├── Roboto-Medium.ttf
│   │   ├── Roboto-Regular.ttf
│   │   ├── Roboto-ThinItalic.ttf
│   │   └── Roboto-Thin.ttf
│   ├── Roboto_Condensed
│   │   ├── LICENSE.txt
│   │   ├── RobotoCondensed-BoldItalic.ttf
│   │   ├── RobotoCondensed-Bold.ttf
│   │   ├── RobotoCondensed-Italic.ttf
│   │   ├── RobotoCondensed-LightItalic.ttf
│   │   ├── RobotoCondensed-Light.ttf
│   │   └── RobotoCondensed-Regular.ttf
│   └── Spirequal-Light
│   └── Spirequal-Light.TTF
├── images
│   ├── authors_banner2.png
│   ├── authors_banner.png
│   ├── banner.jpeg
│   ├── blog-1-1000x600.jpg
│   ├── category-1-400x250.jpg
│   ├── category-3-400x250.jpg
│   ├── favicon.png
│   ├── logo.png
│   ├── marion-michele-330691.jpg
│   ├── media
│   │   ├── article-default.jpg
│   │   ├── banner
│   │   ├── category-default.jpg
│   │   ├── profile-pic-default.jpg
│   │   └── slider-1.jpg
│   ├── pexels-photo-370474.jpeg
│   ├── slider-1-1600x900.jpg
│   └── slider-1.jpg
└── js
├── blog
│   ├── blog.js
│   ├── highlight.pack.js
│   └── prism.js
├── bootstrap
│   └── bootstrap.bundle.min.js
└── common-js
├── bootstrap.js
├── jquery-3.1.1.min.js
├── scripts.js
├── swiper.js
└── tether.min.js
Snippet of template
<img alt="author-profile-image" src="{% static author_profile_details.profile.image.url %}" class="rounded-circle border border-dark shadow-sm">
The image src above is resolved to the path: /static/media/profile-pic-default.jpg
But (as can be seen from the directory path above), the correct path should be:
static/media/blog/images/media/profile-pic-default.jpg (I think - I haven't quite got my heard around static file deployment).
My question is - given the directory structure of the blog app's static assets, how do I use static to correctly locate the profile-pic-default.jpg asset?
Without specifics of the model, I'm going to assume that this is an ImageField for which the app provides a static default.
In this case, the correct way to code it in a template is:
{% if author_profile_details.profile.image %}
<img
alt="author-profile-image"
src="{{ author_profile_details.profile.image.url }}"
class="rounded-circle border border-dark shadow-sm">
>
{% else %}
<img
alt="author-profile-image"
src="{% static 'blog/images/media/profile-pic-default.jpg' %}"
class="rounded-circle border border-dark shadow-sm">
>
{% endif %}

django - cannot import name settings

I'm working on a site in django that is actually in production. I'm trying to run it in local but i get the error: cannot import name settings when i try to access the main page.
This page use the bbcode apps that's correctly installed but that I suspect to be the source of my problem (pages without bbcode work well).
First, as asked here is the hierarchy of my project. Files with a ~ at the end are temporary files created by emacs, so don't pay attention to them. The correct settings.py is the one in /jdrpoly/, The other one was a test to see if it was helping
├── bbcode
│   ├── bbtags
│   │   ├── advanced.py
│   │   ├── advanced.pyc
│   │   ├── brainfuck.py
│   │   ├── brainfuck.pyc
│   │   ├── functional.py
│   │   ├── functional.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── lists.py
│   │   ├── lists.pyc
│   │   ├── smilies.py
│   │   ├── smilies.pyc
│   │   ├── table.py
│   │   ├── table.pyc
│   │   ├── text_formatting.py
│   │   ├── text_formatting.pyc
│   │   └── web.py
│   ├── cli.py
│   ├── fields.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── mypygments.py
│   ├── templates
│   │   └── bbcode
│   │   ├── bbhelp.html
│   │   └── bbtag.html
│   ├── templatetags
│   │   ├── bbcode.py
│   │   ├── bbcode.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   └── views.py
├── events
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── jdrpoly
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings~
│   ├── settings.py
│   ├── settings.py~
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── utils
│   │   └── text.py
│   ├── utils.py
│   ├── utils.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── LICENSE
├── main
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0003_contest.py
│   │   ├── 0003_contest.py~
│   │   ├── 0003_contest.pyc
│   │   ├── 0004_comitymember_mainpagesection_news.py
│   │   ├── 0004_comitymember_mainpagesection_news.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── manage.py
├── members
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_code_member.py
│   │   ├── 0002_code_member.py~
│   │   ├── 0002_code_member.pyc
│   │   ├── 0003_member.py
│   │   ├── 0003_member.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── base.html
│   │   ├── comity.html
│   │   ├── concours.djhtml
│   │   ├── contact.html
│   │   ├── contact_success.html
│   │   ├── events
│   │   │   ├── attending.djhtml
│   │   │   ├── campaign_delete.djhtml
│   │   │   ├── campaign_detail.djhtml
│   │   │   ├── campaign_list.djhtml
│   │   │   ├── create.djhtml
│   │   │   ├── edition_view.djhtml
│   │   │   ├── list.djhtml
│   │   │   ├── menu_event.djhtml
│   │   │   ├── new_campaign.djhtml
│   │   │   ├── propose.djhtml
│   │   │   └── view.djhtml
│   │   ├── gallery
│   │   │   ├── list.html
│   │   │   └── view.html
│   │   ├── mainpage.html
│   │   ├── members
│   │   │   ├── code.html
│   │   │   ├── code_mail.txt
│   │   │   ├── code_use.html
│   │   │   ├── create.html
│   │   │   ├── edit.html
│   │   │   ├── main.html
│   │   │   ├── password_change.html
│   │   │   ├── password_change_ok.html
│   │   │   ├── password_reset.html
│   │   │   └── view.html
│   │   ├── news
│   │   │   ├── letter.html
│   │   │   ├── letter_ok.html
│   │   │   └── view.html
│   │   ├── registration
│   │   │   └── login.html
│   │   └── svz
│   │   ├── admin.html
│   │   ├── generic.html
│   │   └── index.html
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── pictures
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_auto_20150831_1452.py
│   │   ├── 0002_auto_20150831_1452.pyc
│   │   ├── 0003_auto_20161213_2212.py
│   │   ├── 0003_auto_20161213_2212.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── settings.py
├── static
│   ├── assets
│   │   ├── css
│   │   │   ├── font-awesome.min.css
│   │   │   ├── ie8.css
│   │   │   ├── images
│   │   │   │   └── bracket.svg
│   │   │   └── main.css
│   │   ├── fonts
│   │   │   ├── FontAwesome.otf
│   │   │   ├── fontawesome-webfont.eot
│   │   │   ├── fontawesome-webfont.svg
│   │   │   ├── fontawesome-webfont.ttf
│   │   │   ├── fontawesome-webfont.woff
│   │   │   └── fontawesome-webfont.woff2
│   │   ├── js
│   │   │   ├── ie
│   │   │   │   ├── backgroundsize.min.htc
│   │   │   │   ├── html5shiv.js
│   │   │   │   ├── PIE.htc
│   │   │   │   └── respond.min.js
│   │   │   ├── jquery.dropotron.min.js
│   │   │   ├── jquery.min.js
│   │   │   ├── main.js
│   │   │   ├── skel.min.js
│   │   │   ├── skel-viewport.min.js
│   │   │   └── util.js
│   │   └── sass
│   │   ├── ie8.scss
│   │   ├── libs
│   │   │   ├── _functions.scss
│   │   │   ├── _mixins.scss
│   │   │   ├── _skel.scss
│   │   │   └── _vars.scss
│   │   └── main.scss
│   ├── images
│   │   ├── banner.jpg
│   │   ├── banner.png
│   │   ├── default.jpg
│   │   ├── no-image.png
│   │   ├── pic01.jpg
│   │   ├── pic02.jpg
│   │   ├── pic03.jpg
│   │   ├── pic04.jpg
│   │   ├── pic05.jpg
│   │   ├── pic06.jpg
│   │   └── pic07.jpg
│   └── svz
│   ├── assets
│   │   ├── css
│   │   │   ├── font-awesome.min.css
│   │   │   ├── ie8.css
│   │   │   ├── ie9.css
│   │   │   ├── images
│   │   │   │   └── intro.svg
│   │   │   └── main.css
│   │   ├── fonts
│   │   │   ├── FontAwesome.otf
│   │   │   ├── fontawesome-webfont.eot
│   │   │   ├── fontawesome-webfont.svg
│   │   │   ├── fontawesome-webfont.ttf
│   │   │   ├── fontawesome-webfont.woff
│   │   │   └── fontawesome-webfont.woff2
│   │   ├── js
│   │   │   ├── admin.js
│   │   │   ├── ie
│   │   │   │   ├── html5shiv.js
│   │   │   │   └── respond.min.js
│   │   │   ├── jquery.min.js
│   │   │   ├── jquery.scrollex.min.js
│   │   │   ├── jquery.scrolly.min.js
│   │   │   ├── main.js
│   │   │   ├── skel.min.js
│   │   │   └── util.js
│   │   └── sass
│   │   ├── base
│   │   │   ├── _page.scss
│   │   │   └── _typography.scss
│   │   ├── components
│   │   │   ├── _box.scss
│   │   │   ├── _button.scss
│   │   │   ├── _features.scss
│   │   │   ├── _form.scss
│   │   │   ├── _icon.scss
│   │   │   ├── _image.scss
│   │   │   ├── _list.scss
│   │   │   ├── _section.scss
│   │   │   ├── _split.scss
│   │   │   ├── _spotlights.scss
│   │   │   ├── _table.scss
│   │   │   └── _wrapper.scss
│   │   ├── ie8.scss
│   │   ├── ie9.scss
│   │   ├── layout
│   │   │   ├── _footer.scss
│   │   │   ├── _header.scss
│   │   │   ├── _intro.scss
│   │   │   ├── _sidebar.scss
│   │   │   └── _wrapper.scss
│   │   ├── libs
│   │   │   ├── _functions.scss
│   │   │   ├── _mixins.scss
│   │   │   ├── _skel.scss
│   │   │   └── _vars.scss
│   │   └── main.scss
│   └── images
│   ├── pic01.jpg
│   ├── pic02.jpg
│   ├── pic03.jpg
│   ├── pic04.jpg
│   ├── pic05.jpg
│   └── pic06.jpg
└── svz
├── admin.py
├── admin.pyc
├── apps.py
├── __init__.py
├── __init__.pyc
├── migrations
│   ├── 0001_initial.py
│   ├── 0001_initial.pyc
│   ├── 0002_auto_20161213_2212.py
│   ├── 0002_auto_20161213_2212.pyc
│   ├── 0003_auto_20170209_1516.py
│   ├── 0003_auto_20170209_1516.pyc
│   ├── 0004_auto_20170209_1612.py
│   ├── 0004_auto_20170209_1612.pyc
│   ├── 0005_auto_20170209_1621.py
│   ├── 0005_auto_20170209_1621.pyc
│   ├── 0006_auto_20170224_1248.py
│   ├── 0006_auto_20170224_1248.pyc
│   ├── 0007_player_faction.py
│   ├── 0007_player_faction.pyc
│   ├── 0008_auto_20170313_1033.py
│   ├── 0008_auto_20170313_1033.pyc
│   ├── __init__.py
│   └── __init__.pyc
├── models.py
├── models.pyc
├── templates
│   └── svz
│   └── concours_affiche.html
├── tests.py
├── tests.pyc
├── urls.py
├── urls.pyc
├── views.py
└── views.pyc
When i run manage.py runserver everything goes well
Then here is the StackTrace when i'm trying to access the main page:
Traceback (most recent call last):
File "/usr/lib64/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/usr/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/usr/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 170, in __call__
response = self.get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 44, in inner
response = response_for_exception(request, exc)
File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 94, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/usr/lib/python2.7/site-packages/django/template/response.py", line 109, in render
self.content = self.rendered_content
File "/usr/lib/python2.7/site-packages/django/template/response.py", line 84, in rendered_content
template = self.resolve_template(self.template_name)
File "/usr/lib/python2.7/site-packages/django/template/response.py", line 66, in resolve_template
return select_template(template, using=self.using)
File "/usr/lib/python2.7/site-packages/django/template/loader.py", line 48, in select_template
return engine.get_template(template_name)
File "/usr/lib/python2.7/site-packages/django/template/backends/django.py", line 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "/usr/lib/python2.7/site-packages/django/template/engine.py", line 160, in get_template
template, origin = self.find_template(template_name)
File "/usr/lib/python2.7/site-packages/django/template/engine.py", line 134, in find_template
name, template_dirs=dirs, skip=skip,
File "/usr/lib/python2.7/site-packages/django/template/loaders/base.py", line 44, in get_template
contents, origin, origin.template_name, self.engine,
File "/usr/lib/python2.7/site-packages/django/template/base.py", line 191, in __init__
self.nodelist = self.compile_nodelist()
File "/usr/lib/python2.7/site-packages/django/template/base.py", line 233, in compile_nodelist
return parser.parse()
File "/usr/lib/python2.7/site-packages/django/template/base.py", line 518, in parse
raise self.error(token, e)
ImportError: cannot import name settings
[02/Nov/2017 19:10:31] "GET / HTTP/1.1" 500 59
I checked all the imports and they are correct, tried to specify the settings directly when running manage.py with --settings=..., tried also to specify the PYTHONPATH.
I've run out of ideas.
One last thing, this website, with exactly the same files is currently running on a server and do not have any problems of this kind.
Try this:
from django.conf import settings

Django 1.11 don't load static image and show error in main page

I have a new problem:
I don't know what is the problem, because I check it move the folder media to eventus folder, but it no working, and I change the path in MEDIA_URL, but it no working too.
I think the problem will be in that I follow a tutorial of the version 1.6 of Django and some things are deferents in this new version and I don't know how to do it.
my schema of the project is the next
.
├── eventus
│   ├── eventus
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   ├── urls.cpython-36.pyc
│   │   │   └── wsgi.cpython-36.pyc
│   │   ├── db.sqlite3
│   │   ├── settings
│   │   │   ├── __init__.py
│   │   │   ├── __pycache__
│   │   │   │   ├── __init__.cpython-36.pyc
│   │   │   │   ├── base.cpython-36.pyc
│   │   │   │   └── local.cpython-36.pyc
│   │   │   ├── base.py
│   │   │   ├── local.py
│   │   │   ├── prod.py
│   │   │   └── staging.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── manage.py
│   └── myapps
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-36.pyc
│   ├── events
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   ├── admin.cpython-36.pyc
│   │   │   ├── forms.cpython-36.pyc
│   │   │   ├── models.cpython-36.pyc
│   │   │   ├── urls.cpython-36.pyc
│   │   │   └── views.cpython-36.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── forms.py
│   │   ├── media
│   │   │   └── events
│   │   │   ├── evento.png
│   │   │   ├── evento2.png
│   │   │   ├── evento2_a0yEovu.png
│   │   │   ├── evento3.png
│   │   │   ├── evento3_IufcnS5.png
│   │   │   └── evento_HFKQ1lo.png
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0002_auto_20170924_2140.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │   ├── 0001_initial.cpython-36.pyc
│   │   │   ├── 0002_auto_20170924_2115.cpython-36.pyc
│   │   │   ├── 0002_auto_20170924_2140.cpython-36.pyc
│   │   │   └── __init__.cpython-36.pyc
│   │   ├── models.py
│   │   ├── static
│   │   │   ├── css
│   │   │   │   ├── bootstrap-theme.css
│   │   │   │   ├── bootstrap-theme.css.map
│   │   │   │   ├── bootstrap-theme.min.css
│   │   │   │   ├── bootstrap-theme.min.css.map
│   │   │   │   ├── bootstrap.css
│   │   │   │   ├── bootstrap.css.map
│   │   │   │   ├── bootstrap.min.css
│   │   │   │   ├── bootstrap.min.css.map
│   │   │   │   └── estilos.css
│   │   │   ├── fonts
│   │   │   │   ├── glyphicons-halflings-regular.eot
│   │   │   │   ├── glyphicons-halflings-regular.svg
│   │   │   │   ├── glyphicons-halflings-regular.ttf
│   │   │   │   ├── glyphicons-halflings-regular.woff
│   │   │   │   └── glyphicons-halflings-regular.woff2
│   │   │   └── js
│   │   │   ├── bootstrap.js
│   │   │   ├── bootstrap.min.js
│   │   │   └── npm.js
│   │   ├── templates
│   │   │   ├── base.html
│   │   │   └── events
│   │   │   ├── base_events.html
│   │   │   ├── index.html
│   │   │   └── panel
│   │   │   ├── crear_evento.html
│   │   │   ├── detalle_evento.html
│   │   │   ├── editar_evento.html
│   │   │   ├── eliminar_evento.html
│   │   │   ├── navbar.html
│   │   │   └── panel.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   └── users
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── actions.cpython-36.pyc
│   │   ├── admin.cpython-36.pyc
│   │   ├── models.cpython-36.pyc
│   │   └── urls.cpython-36.pyc
│   ├── actions.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   ├── 0001_initial.cpython-36.pyc
│   │   └── __init__.cpython-36.pyc
│   ├── models.py
│   ├── templates
│   │   └── users
│   │   └── login.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── requirements
├── base.txt
├── local.txt
├── prod.txt
└── staging.txt
27 directories, 94 files
And I change the configuration of this archives:
my file urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^', include('myapps.events.urls', namespace="events_app")),
url(r'^', include('myapps.users.urls', namespace="users_app")),
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns = [
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my file users/urls.py:
from django.conf.urls import url, include
urlpatterns = [
url(r'^login/$', 'myapps.users.views.userlogin', name="login"),
]
my file users/views.py:
from django.shortcuts import render
from .forms import UserRegisterForm
# Create your views here.
def userlogin(request):
if request.method == "POST":
pass
else:
user_register = UserRegisterForm()
return render(request, 'users/login.html', {'user_register' : user_register})
my file users/forms.py:
from django import forms
from .models import User
class UserRegisterForm(forms.ModelForm):
class Meta:
model = User
fields = ('username', 'email', 'password')
widgets = {
'username' : forms.TextInput(attrs =
{
'class' : 'form-control',
'placeholder' : 'Ingresa un nombre de usuario'
})
'email' : forms.TextInput(attrs =
{
'type' : 'email',
'class' : 'form-control',
'placeholder' : 'Ingresa un email'
})
'password' : forms.TextInput(attrs =
{
'type' : 'password',
'class' : 'form-control',
'placeholder' : 'Ingresa un password'
})
}
my local.py is :
from .base import *
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'eventusdb',
'USER': 'eventususer',
'PASSWORD': '12345',
'HOST': 'localhost',
'PORT': '5432'
}
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR.child('media')
Could you help me?
Remove this if settings.DEBUG: block completely. It is replacing your urlpatterns with a single URL pattern for media files.
if settings.DEBUG:
urlpatterns = [
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Then add static(...) to the end of your urlpatterns:
urlpatterns = [
url(r'^', include('myapps.events.urls', namespace="events_app")),
...
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Django template not found in main project directory error

I am getting a 'template not found' error, although I've set up a correct template hierarchy (or so I thought)
.
├── manage.py
├── twinja
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   └── views.py
└── twinjasite
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.py~
├── settings.pyc
├── static
│   └── twinja
│   ├── fonts
│   │   └── bootstrap
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   ├── glyphicons-halflings-regular.woff
│   │   └── glyphicons-halflings-regular.woff2
│   ├── images
│   ├── javascripts
│   │   ├── bootstrap
│   │   │   ├── affix.js
│   │   │   ├── alert.js
│   │   │   ├── button.js
│   │   │   ├── carousel.js
│   │   │   ├── collapse.js
│   │   │   ├── dropdown.js
│   │   │   ├── modal.js
│   │   │   ├── popover.js
│   │   │   ├── scrollspy.js
│   │   │   ├── tab.js
│   │   │   ├── tooltip.js
│   │   │   └── transition.js
│   │   ├── bootstrap.js
│   │   ├── bootstrap.min.js
│   │   └── bootstrap-sprockets.js
│   └── stylesheets
│   ├── framework.css
│   └── styles.css
├── templates
│   └── twinja
│   ├── base.html
│   └── menu.html
├── urls.py
├── urls.py~
├── urls.pyc
├── views.py
├── views.py~
├── views.pyc
├── wsgi.py
└── wsgi.pyc
At first I set up templates/base but that did not work. So I made it as you see here: templates/twinja/base
Ideally I'm setting up the MAIN template files which are independent of apps, which I thought was meant to go in the main folder (where settings.py is) but perhaps I am mistake.
Yes, I have 'twinja' set up in installed apps as well.
What appears to be wrong here?
TemplateDoesNotExist at /
twinja/base.html
If your template is independent of apps, it shouldn't be in your app folder - namespace it accordingly.
How do you load your template? Did you setup your templates/staticfolders in your settings.py?
Updated.
For the fresh project you need to configure your settings.py file. Read here, ask away if you still have a question.
Updated.
After you get familiar with static files and how to manage those, spend some time here.

Django: Application labels aren't unique

I have been working on the issue of duplicate labels in Django and from this answer I have added the following files to my "jobs" project folder:
jobs/apps.py
# jobs/apps.py
from django.apps import AppConfig
class JobsConfig(AppConfig):
name = 'jobs'
verbose_name = "jobs2"
jobs/init.py
# jobs/__init__.py
default_app_config = 'jobs.apps.JobsConfig'
This hasn't really helped much and I still get the error when trying syncdb:
"duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: jobs
Also, changing from "name = 'jobs'" to "name = 'jobs2'" just gives me the error:
ImportError: No module named jobs2
File Structure
/opt/Webapp
├── userfiles
├── templates
│   └── admin
│   └── base.html
├── static
│   ├── admin_tools
│   │   ├── images
│   │   │   └── apto.gif
│   │   └── css
│   │   └── theming.css
│   └── admin
│   └── css
│   └── base.css
├── smartrecruitment
│   ├── wsgi.py
│   ├── urls.py
│   ├── settings.pyc
│   ├── settings.py
│   ├── __init__.pyc
│   └── __init__.py
├── requirements.txt
├── manage.py
├── jobs
│   ├── views.py
│   ├── urls.py
│   ├── tests.py
│   ├── testhelpers.py
│   ├── templates
│   │   └── jobs
│   │   ├── test.html
│   │   ├── success.html
│   │   ├── registration.html
│   │   ├── registrationcomplete.html
│   │   └── application.html
│   ├── tables.py
│   ├── static
│   │   └── jobs
│   │   ├── styles
│   │   │   ├── index.css
│   │   │   ├── hide_admin_original.css
│   │   │   └── application.css
│   │   ├── style.css
│   │   └── images
│   │   └── apto.gif
│   ├── models.py
│   ├── migrations
│   │   ├── __init__.py
│   │   ├── 0002_auto__del_field_registrant_name__add_field_registrant_first_name__add_.py
│   │   └── 0001_initial.py
│   ├── lists.py
│   ├── __init__.pyc
│   ├── __init__.py
│   ├── forms.py
│   ├── apps.pyc
│   ├── apps.py
│   └── admin.py
├── fileuploads
│   ├── tests.py
│   ├── templates
│   │   └── fileuploads
│   │   ├── index.html
│   │   ├── details.html
│   │   ├── base.html
│   │   └── add.html
│   ├── models.pyc
│   ├── models.py
│   ├── __init__.pyc
│   ├── __init__.py
│   ├── forms.pyc
│   ├── forms.py
│   ├── context_processors.py
│   └── admin.pyc
├── dashboard.pyc
└── dashboard.py
You have a mix of old-style (south: 0002_auto_del...) and new-style (django: 0001_initial) migrations in your jobs app. Easiest fix would be to delete all numbered migrations rm jobs/migrations/0???_*.py* and recreate migrations by running manage.py makemigrations

Categories