Django block content - django

I am trying out block content in my django project but when i type in my code in a html file like so
<!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>Document</title>
</head>
<body>
<div class="a">
{% block content %}
{% endblock %}
</div>
</body>
</html>
{% block content %}
{% endblock %}
these two codes won't work and they don't turn yellow as they should.
could somebody help me?

If it doesn't turn yellow,
You can check if you are using the "django" high light template in your vscode
or you are just using "HTML" template
However, i don't suggest you to turn to "django" highlight. Because most of your code are written in "HTML"
And if your code isn't work, you can check the file which is based on this file.
I guess the file you show is a basic file
May you please show me the file that is based on it? or it's hard to tell your mistake

Related

Stylesheets to use in django-ckeditor

I'm using django-ckeditor which is a TextArea WYSIWYG editor for creating and editing posts in a future blog. The editor produces beautiful posts, but when I save to the database and go to view it, it appears without the style I initially created in the editor.
See the following code:
{%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.0">
<title>MyDomain | {{post.title}}</title>
<link href="{% static 'ckeditor/ckeditor/contents.css'%}" rel="stylesheet">
</head>
<body>
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|safe|linebreaks }}
</body>
</html>
My question is: which styles do I import to present the same view as when I created the post?

Django Flash Message shows up without implementing it in template

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?

Jinja macro doesn't render string + variable

I'm trying to use this macro but doesn't render the "Dear user_name" into the macro, but if I pass only the variable it works.
Why does this happen?
layout.hmtl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
{% block content %}{% endblock content %}
</div>
</body>
</html>
macro.html
{% macro paragraph(text="") %}
<p>{{ text }}</p>
{% endmacro %}
content.html
{% extends 'layout.html' %}
{% from 'macros/macro.html' import paragraph %}
{% block content %}
{{ paragraph(text="Dear user_name,") }}
{% endblock %}
If you have a variable named user_name and you'd like to use that in your template, you would need to move user_name outside of the literal string. Instead of:
{{ paragraph(text="Dear user_name,") }}
You would use:
{{ paragraph(text="Dear " ~ user_name ~ ",") }}
Or:
{{ paragraph(text="Dear {},".format(user_name)) }}
Using either of the above examples, this code:
import jinja2
e = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
t = e.get_template('content.html')
print(t.render(user_name='alice'))
When used with your templates, produces this output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<p>Dear alice,</p>
</div>
</body>
</html>

how to load bootstrap in Django?

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

How to use generic "analytical.*" tags in django-analytical

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>