I have been going crazy in order to understand what's going on but I can't seem to figure out what is the problem. It only happens in production, not in dev server. Here is my config:
Settings.py
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
base.html (the layout im extending)
<!DOCTYPE html>
<html lang="en">
{% load static %}
{% load bootstrap4 %}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>WHARFY</title>
{% bootstrap_javascript jquery='full' %}
{% bootstrap_css %}
<!-- Custom fonts for this template-->
<link href='{% static "vendor/fontawesome-free/css/all.min.css"%}' rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
<!-- Custom styles for this template-->
<link href='{% static "css/sb-admin-2.min.css" %}' rel="stylesheet">
model_update.html (Im using UpdateView)
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
{{ form.media }}
<form method="post">{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Save">
</form>
{% endblock %}
While in dev I do not have any problems, in production i get this error from the console
Failed to load resource: the server responded with a status of 404 (Not Found)
datepicker-widget.css:1
Failed to load resource: the server responded with a status of 404 (Not Found)
datepicker-widget.css:1
The datetime picker doesn't show up(ONLY IN PRODUCTION)
I properly installed using datetimepicker plus and also bootstrap and also successfully run the following command:
python manage.py collectstatic
I'm new to django, I found similar questions but couldn't figure out how to make it work.
Related
i want to show up my Flash Messages in my template. It works, but i cant tell the template where to show or how to show it. It appears all the time in the same place and the same style.
This is my view:
def exportBefunde(request):
(...)
messages.info(request, "Befund exportiert")
return redirect("reporting")
This is my main.html template
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Das neueste kompilierte und minimierte CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optionales Theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Das neueste kompilierte und minimierte JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="{% static 'index.css' %}">
<title>Message Problem - it drives me crazy</title>
</head>
<body>
{% include 'navbar.html' %}
{% block content %}
<!-- Content from other templates -->
{% endblock content %}
{% if messages %}
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
{% endif %}
</body>
</html>
I want to show my template underneath my content. But i appears above it. Even if i remove this peace of code:
{% if messages %}
{% for message in messages %}
<p id="messages">{{message}}</p>
{% endfor %}
{% endif %}
the message shows up above the code. The styling is always the same. Django ignores my Code in my template. Do anyone has a solution for this issue?
I've done pretty much everything in my knowledge to solve this, but I guess I've gone wrong somewhere as I'm still a beginner, so I'd like someone to kindly help me out with this and also I've added the error message at the bottom, including the code
error
TemplateSyntaxError at /
Invalid block tag on line 9: 'static'. Did you forget to register or load this tag?
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.6
Exception Type: TemplateSyntaxError
Exception Value:
Invalid block tag on line 9: 'static'. Did you forget to register or load this tag?
Exception Location: C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py, line 531, in invalid_block_tag
Python Executable: C:\Users\Admin\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.1
Python Path:
['C:\\Users\\Admin\\Desktop\\Django\\ecommerce',
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Fri, 09 Apr 2021 00:16:08 +0000
setting
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
css/main.css
body {
background-color: blue;
}
store/main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ecommerce</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'css/main.css' %}" type="text/css">
</head>
<body>
<h1>Navbar</h1>
<hr>
<div class="container">
{% block content %}
{% endblock content %}
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
</body>
</html>
store
{% extends 'store/main.html' %}
{% load static %}
{% block content %}
<h3>Store</h3>
{% endblock content %}
store/urls.py -- This is the app
from django.urls import path
from . import views
urlpatterns = [
path('',views.store,name="store"),
path('cart/',views.cart,name="cart"),
path('checkout/',views.checkout,name="checkout"),
]
ecom/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('store.urls')),
]
You forgot to add static template tag {% load static %}
in main.html
In your templates, use the static template tag to build the URL for
the given relative path using the configured STATICFILES_STORAGE.
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ecommerce</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'css/main.css' %}" type="text/css">
</head>
<body>
<h1>Navbar</h1>
<hr>
<div class="container">
{% block content %}
{% endblock content %}
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
</body>
</html>
https://docs.djangoproject.com/en/3.1/howto/static-files/
so I tried loading Bootstrap to Django. But since I wanted to customize the styling with scss, instead of putting the CDN url in header, I replaced it with a separate css file which has all of the Bootstrap stylings. Here's the code.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/style/main.css">
<title>{% block title %}BASE{% endblock %}</title>
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
I have the correct /style/main.css file, I've checked it by ctrl+clicking it. As mentioned, the file has ALL of the Bootstrap stylings.
#charset "UTF-8";
/*!
* Bootstrap v4.4.1 (https://getbootstrap.com/)
* Copyright 2011-2019 The Bootstrap Authors
* Copyright 2011-2019 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
:root {
--blue: #007bff;
--indigo: #6610f2;
--purple: #6f42c1;
--pink: #e83e8c;
### AND SO ON ###
However my Django page wouldn't reflect it. When I hit refresh I don't see any styling.
But when I restore the CDN url, Bootstrap is normally applied. I have no idea what the problem is. I would very much appreciate your help. :)
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% load static %}
<link rel="stylesheet" href="{% static 'style/main.css' %}">
<title>{% block title %}BASE{% endblock %}</title>
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
settings.py
add this in your settings.py
STATIC_URL = '/static/'
if your static folder follow this path Projectname/static or if you have static folder in your app too Projectname/appname/static then you can append it in list like 2nd one
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'),os.path.join(BASE_DIR, 'app_name/static'),]
To load static files in Django
Usually we keep our static files (a.js, b.css, c.png) in a folder named static.
Suppose you have the main.css file in static/css/main.css
Then change your code as
{% load static %}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
<title>{% block title %}BASE{% endblock %}</title>
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Check out How to serve static files django
I have a class based view which shows the data in template having {% extends 'base.html' %}.
My base file folder is in seperate folder called products
My App url
urlpatterns = [
url(r'^contact/', views.contact, name='contact'),
url(r'^product_list/', views.ProductViewList.as_view(), name='ProductViewList'),
]
Template of class based view (product_list.html)
{% extends 'base.html' %}
{% block content %}
{{ object_list }}
{% endblock %}
My base.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="{% static 'img/favicon.ico' %}">
<title>Starter Template for Bootstrap</title>
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
<link href="{% static 'css/starter-template.css' %}" rel="stylesheet">
</head>
<body>
{% include 'navbar.html' %}
<div class="container">
{% block content %}
{% endblock %}
</div><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="{% static 'js/jquery.min.js' %}"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="{% static 'js/ie10-viewport-bug-workaround.js' %}></script>
</body>
</html>
When i visit "http://127.0.0.1:8000/product/product_list/" following error pops up
My directory structure, My base.html is in root directory(templates) and product_list.html is in products folder
I think there could be problem with {% extends 'base.html' %}, But not sure how to sole the problem.......Any help is much appreciated
Here's my first urls...which further redirects to my app url.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^product/', include('products.urls')),
url(r'^accounts/', include('registration.backends.default.urls')),
]
You need to specify a name to the view url that identify when you want to used in the template
from django.conf.urls import url
import myapplication.views
app_urlpatterns = [
url(r'^article/(\d+)/$',views.article_detail,name='article-view-url'),
]
And this is how I use the url name in my html template
{% for article in articles.object_list %}
<article>
<h3>
{{ article.name }}
</h3>
<p>
{{ article.desc }}
</p>
</article>
{% endfor %}
I try to setup Google Analytics with django-analytical for my django project, following this guide: http://pythonhosted.org/django-analytical/services/google_analytics.html#google-analytics-configuration.
In this guide you can find the following statement :
"Next you need to add the Google Analytics template tag to your templates. This step is only needed if you are not using the generic analytical.* tags. If you are, skip to Configuration."
So my question is: Where to put this generic analytical.* tag? Is it somewhere in my settings.py file?
Thanks.
If you are planning to use Google Analytics as your service, you can simply add the following to your templates/base.html:
{% load google_analytics %}
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>My Website: {{ title }}</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
{% block css %}
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css">
<link rel="stylesheet" href="{{ STATIC_URL }}bootstrap/css/bootstrap-tokenfield.css">
<link rel="stylesheet" href="{{ STATIC_URL }}bootstrap/css/bootstrap.css">
<link rel="stylesheet" href="{{ STATIC_URL }}bootstrap/css/font-awesome.min.css">
<link rel="stylesheet" href="{{ STATIC_URL }}css/main.css">
{% endblock css %}
<!-- fix so that IE 9 and less will properly recognize html5 elements -->
<!--[if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
{% google_analytics %}
</head>
See the documentation at: https://pythonhosted.org/django-analytical/services/google_analytics.html
I believe that the generic analytical.* tags that they refer to at the link that you posted have to do with using the following below. If I'm incorrect, others please chime in and provide the correct response. HTH.
{% load analytical %}
<!DOCTYPE ... >
<html>
<head>
{% analytical_head_top %}
…
{% analytical_head_bottom %}
</head>
<body>
{% analytical_body_top %}
…
{% analytical_body_bottom %}
</body>
</html>