Django - django-privacy-mgmt package not displaying html - python

I need help implementing this package into this django project:
When viewing the page source, I see the app's HTML loaded from the render tags. But on the actual page, I am not seeing any of the outputted HTML from those render tags.
Can someone please summarize a step by step set of instructions to get this package to work? The instructions provided can be confusing sometimes.
I did not do the following and am not sure how to do these parts:
1) install the add-on on divio.com or via pypi (would rather not install this since it seems to costs money - unless there is a way to use it for free)
2) update your templates/django_privacy_mgmt to reflect your frontend toolchain situation (not sure where in my project to put these files.
3) Then check what kind of tracking items your website is using (either in the templates or via Google Tag Manager or in any imaginable other way) and add them in the "Django Privacy Mgmt" section of the Django Admin interface to the list of 'Tracking Items'. This is necessary so that we can show a list of tracking items to the user in the 'privacy settings' modal.
4) Then implement conditional logic that enables or disables the tracking items that you identified in the previous step (see next chapter).
Here are the steps I followed:
pip3 install django-privacy-mgmt
pip3 install django-parler
pip3 install django-sekizai
python3 manage.py migrate
​TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,"templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sekizai.context_processors.sekizai',
],
},
},
]
​
SITE_ID = 1
INSTALLED_APPS = [
'django.contrib.sites',
'sekizai',
'django_privacy_mgmt',
'parler',
]
{% load privacy %}
<!DOCTYPE html>
<html lang="en">
<head>
{% load static %}
{% render_privacy_api %}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<body>
{% render_privacy_banner %}
{% render_privacy_settings_modal %}
{% render_privacy_settings_modal_link %}
</body>

I just started using django two weeks ago and stumbled over the django-privacy-mgmt package. I wanted to use it in my project as well and had some trouble in the beginning. That's why I think I might be able to help you out here.
I had a look at your project, checked it out and integrated the package successfully.
I did the following steps:
Installed the package with pip pip install django-privacy-mgmt
Add the installed apps as you did
INSTALLED_APPS = [
...
'django.contrib.sites',
'sekizai',
'django_privacy_mgmt',
'parler',
...
]
And the django.contrib.messages.context_processors.messages to the context-processors
Added the privacy definitions to your base.html in ./courses/templates
{% load privacy %}
{% load sekizai_tags %}
<!DOCTYPE html>
<html>
<head>
<title>Django Video Membership</title>
{% render_privacy_api %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ...
{% render_block 'css' %}
</head>
<body>
{% include 'courses/messages.html' %}
<div class="container">
<ol class='breadcrumb'>
<li>Profile</li>
{% block post_detail_link %}
{% endblock %}
{% if not request.user.is_authenticated %}
<li class='pull-right'><a href='/register'>Register</a></li>
<li class='pull-right'><a href='/login'>Login</a></li>
{% else %}
<li class='pull-right'><a href='/memberships'>Memberships</a></li>
{% endif %}
{% render_privacy_settings_modal_link %}
</ol>
</div>
{% block content %}
{% endblock content %}
<script
src="https://code.jquery.com/jquery-3.3.1.js" ...
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ...
{% render_privacy_banner %}
{% render_privacy_settings_modal %}
{% render_block "js" %}
</body>
</html>
python manage.py migration
python manage.py runserver and login with any user will show the link in the navigation, if you click on it the popup appears
Explanations:
In the first step, we install the package. You don't need to install the django-parler and django-sekizai, they are dependencies of the django-privacy-mgmt and will be automatically installed by pip
After the package is successfully installed, we can use the tags in your base.html file, which is situated in ./courses/templates/courses/base.html. First, we include the load privacy in order to be able to use the tags from the django-privacy-mgmt package. Afterward, we load sekizai_tags. This is not described in the documentation, but it is necessary to add the render_block tags for 'js' and 'css', which are used by the package to add javascript and css to your base.html. The creator of the package plans to remove it in the future, please have a look here. You need to put the render_privacy_settings_modal_link where you want to show the link for the user, probably the footer is the best place. I put it in your navigation.
I added the render_privacy_api, render_privacy_banner and render_privacy_settings_modal according to the documentation into your base.html. Please note that the banner is optional.
In step number 5, I migrate the SQL scripts, which will create the tables necessary to create the TrackingItem's.
After you started your local server and you logged in with any user, you should be able to see the 'privacy-setting'-link in the navigation.
1) install the add-on on divio.com or via pypi
I didn't use it. As described here installing with pip works
2) update your templates/django_privacy_mgmt to reflect
What the creator of the package means by that is that you can override his templates. He has for templates, these are the render-tags you are including in you application. I needed to do this in my project, because I use Django 3 and staticfiles are not supported anymore. You don't need to worry about that, everything works fine in your project, but if you want to change the layout of the banner, or the link or the popup, than you have to override the template. you can do this by creating a folder called django_privacy_mgmt in your ./courses/templates and create HTML-files with the names as you find here. Copy the content from the repository and adjust it to your needs.
3) + 4) Then check what kind of tracking items your website is using
If you log into the admin-area by using localhost:8000/admin you should see the model TrackingItem, where you can create items for the django-privacy-mgmt.
In order to exclude scripts depending on the privacy-setting of a user, you have to follow the explanation here. As you can see in the example, it does not load the googletagamanager if the user denied the statistics in the cookie-settings.
Hope this helps!

Related

Customized Login site not showing in Django

I have created a Django App and want to provide a custom Login page with only the possibility to use a Google login.
I have implemented the Google login based on this post: https://www.section.io/engineering-education/django-google-oauth/
It actually works fine and when I hit localhost/account/login I am getting a login page:
I actually do not want a sign up and a "local" sign in option, I only need a Google login.
To achieve this I have created the following folder structure to overwrite the login template (based on https://learndjango.com/tutorials/django-login-and-logout-tutorial):
MyApp/templaltes/registration/login.html
login.html has the following content:
{% extends "base.html" %}
{% load i18n %}
{% load socialaccount %}
{% block content %}
<div class="row">
<center><h1>{% trans 'LoginHeading' %}</h1></center>
</div>
{% if user.is_authenticated %}
<p>Welcome, You are logged in as {{ user.username }}</p>
{% else %}
Login With Google
{% endif %}
{% endblock %}
I have also added 'DIRS': [str(BASE_DIR.joinpath('templates'))] to the TEMPLATES section in settings.py.
Unfortunately I am still seeing the Django default login page. What am I missing?
(I have also restarted the dev server to make sure it is not an issue there with the same result)
I have figured it out. After reading the documentation of Allauth throughly I found this:
For instance, the view corresponding to the account_login URL uses the
template account/login.html. If you create a file with this name in
your code layout, it can override the one shipped with allauth.
So I changed the folder structure to account/login.html and it works.
To do this thing you can simply create an account folder under the template and add login.html inside it.
This will override your login page.
Make sure that folder name must be account.
In your case, you can rename your folder from "registration" to "account".
Like following:
I am sure this will work well.
Simple solution is to add
SOCIALACCOUNT_LOGIN_ON_GET=True
to your settings.py and it should skip/bypass the sign up form.
Credits: https://stackoverflow.com/a/70680165/11605100
Still does not work?
Stop the server and then;
python manage.py migrate
python manage.py makemigrations
python manage.py runserver

Integrate Django blog project in HTML website

I have a regular website where HTML5, CSS3, JQUERY and static images have been used.
I also have a Blog written in Django and I would like to integrate it in the website.
I am really new to Django so I was wondering which is the best approach to use.
Should I integrate the website code as part of the Django project or there are some other solutions?
thanks!
You have 2 ways of integrating your current site with Django.
1) You can write API with DjangoRestFramework and make requests with jQuery AJAX in order to get content from Django.
2) You can use your current HTML files as your Django project templates for rendering content from Django.
You can use a Django template. The template defines placeholders and various bits of basic logic (template tags) that regulate how the document should be displayed. Usually, templates are used for producing HTML, but Django templates are equally capable of generating any text-based format.
If you've used a templating engine like ''. They look somehow similar.
<html>
<head>
<title>Ordering notice</title>
</head>
<body>
<h1>Ordering notice</h1>
<p>Dear {{ person_name }},</p>
<p>Thanks for placing an order from {{ company }}. It's scheduled to ship on {{ s\
hip_date|date:"F j, Y" }}.</p>
<p>Here are the items you've ordered:</p>
<ul>
{% for item in item_list %}
<li>{{ item }}</li>{% end for %}
</ul>
{% if ordered_warranty %}
<p>Your warranty information will be included in the packaging.</p>
{% else %}
<p>
You didn't order a warranty, so you're on your own when the products inevitably stop working.
</p>
{% endif %}
<p>Sincerely,<br />{{ company }}</p>
</body>
</html>
check here for more details https://djangobook.com/django-templates/

Django with Twitter Bootstrap3 and Themes

I've been researching this a long time and experimenting and I can't seem to figure out what the best way to go about accomplishing this. I know I'm doing it wrong so some clarification as well as some quick examples would help a lot.
I was wondering about using a bootstrap3 theme from https://wrapbootstrap.com/ with Django. I researched into the django-bootstrap3 and django-bootstrap-themes packages. On the github page of django-bootstrap-themes it says it's compatible with bootswatch. I don't like their themes so I was wondering if it was compatible with wrapbootstrap.com themes. The themes I would select from wrapbootstrap.com are Responsive HTML themes. I wanted to know if out of personal experience anyone knows which of these packages are best for using themes from wrapbootstrap.com with django.
I understand it is possible to just take from the theme the stylesheets, scripts, place them into their respective folders in static, and turn the HTML base into a base template as well as other pages. Then install bootstrap from a CDN. I know this is also possible with django-bootstrap3. But I can't find the answer anywhere as to what is currently the best way to go about integrating one of those themes and twitter bootstrap3 into a Django website, and some quick examples of doing this.
Thanks, and any advice for doing so would help a ton.
The package django-bootstrap3 is a nice utility app which allows you to produce less HTML markup in your templates that would be otherwise required for bootstrap to work. It uses some additional template tags for this purpose.
I find this package a nice one and sometimes I use it, but it is usually after you have got involved well into bootstrap that you appreciate it.
The package django-bootstrap-themes seems to be an app which it too offers some template tags like the former package, but apparently less. But it also allows you to easily integrate themes from Bootswatch into your Django templates, which is nice if you find those templates appealing. Other than that I personally find no other reason to use it. But again, both packages could be used together if it fits you.
Some examples:
In order to get bootstrap going in your templates without any other package, you would have to include the following in your base html file:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
{# HTML5 shiv and Respond.js for IE8 support of HTML5 elements and media queries #}
{# WARNING: Respond.js doesn't work if you view the page via file:// #}
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
And at the bottom:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
With django-bootstrap3 this becomes:
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
But some other markup gets very much simplified. For instance a bootstrap form (see the official docs example) would become easy as:
<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</form>
To load bootstrap with django-bootstrap-themes:
{% load bootstrap_themes %}
{% bootstrap_script use_min=True %}
And apparently this is how you would use one of the themes from bootswatch:
{% bootstrap_styles theme='cosmo' type='min.css' %}
To sum up, if you wish to use any other ready-made bootstrap theme in your templates, you would still need to go with the standard approach that you describe in your question, possibly using some of the above tools on top.

HTML changes not reflected in django-cms

I was working on an old django-cms project and was trying to edit base.html file and no changes are reflected when I reload the page.
But if I delete all the lines in that file the django runserver refuses to start showing error
django.core.exceptions.ImproperlyConfigured: The 'js' and 'css' sekizai namespaces must be present in each template, - or a template it inherits from - defined in CMS_TEMPLATES. I can't find the namespaces in 'project/cms/home.html'.
They why isn't other changes like adding a new class not reflected in the page reload or server restart.
NOTE:
The project is working good as it is. I was trying to modify it a little bit. Changes I made in the css pages are getting reflected when I reload the page. Issue is only when I try to edit HTML pages
For base.html, you need to have {% load cms_tags sekizai_tags %} in the file. Add {% render_block "css" %} to <head></head> and {% render_block "js" %} somewhere between <body></body>. Depending on the template files that inherit from base.html, certain portions may have been overwritten. For example, if you had:
{# base.html #}
{% block content %}
<div class="example-class"></div>
{% endblock %}
But in another file say:
{# layout.html #}
{% extends "base.html" %}
{% block content %}
{% endblock %}
The div would not appear.
If however, you are talking about missing CSS files, you still need to include them in <head> for it to be displayed. render_block "css" is for django-cms css files that are included in plugins etc. I usually use a LESS or SCSS compiler to include CSS into my projects.
Hope that helps. Post more details for a better diagnosis.

Django Custom Include Template

I know this isn't exactly Django templating philosophy, but i'd like to be able to include different templates depending on a list of plugins that I've specified in the http response context. For example, if I have the following plugins configured:
context['plugins'] = ['weather']
I attempt to include each template in the base template file:
{% for custom_plugin in custom_plugins %}
{% include "map/plugins/custom/{{ plugin }}/includes.html" %}
{% endfor %}
I've also tried:
{% for plugin in plugins %}
#register.inclusion_tag("map/plugins/custom/{{ plugin }}/includes.html", takes_context=True)
{% endfor %}
For now the each plugin will only contain script references and css classes in their includes.html file:
<script type="text/javascript" src="{{ MEDIA_URL }}map/js/plugins/custom/weather/weatherStation.js?ver={{ version }}"></script>
Any suggestions?
Your first way seems the best, and this answer might provide some pointers as to how you'd go about it: How to concatenate strings in django templates?
You basically want to build a string of the template to include in a variable with the with tag, then include it.

Categories