Pelican i18n subsites menu items override - python

I am building a static site with Pelican and the i18n subsites plugin.
The way I understand it, you can override settings in pelicanconf.py with this plugin, but I don’t think the way I did it is working.
Pelicanconf.py:
I18N_SUBSITES = {
'nl': {
'SITENAME': 'Robin Berghuijs Design',
'INDEX_SAVE_AS': 'nieuws.html',
'MENUITEMS': [
('Nieuws','nieuws.html'),
],
},
'en': {
'SITENAME': 'Robin Berghuijs Design',
'INDEX_SAVE_AS': 'news.html',
'MENUITEMS': [
('News','news.html'),
],
}
}
Index.html output:
<nav id="menu"><ul>
<li>Contact</li>
</ul></nav><!-- /#menu -->
base.html template:
{% for title, link in MENUITEMS %}
<li>{{ title }}</li>
{% endfor %}
I get no errors upon site generation. More detail here.
Running pelican with --debug gives this.

As it turns out, the i18n subsites plugin was creating two new sites, with the old one left in the output folder. So there was a site in output/, one in output/nl/, and one in output/en/. Adding DELETE_OUTPUT_DIRECTORY = True and 'OUTPUT_PATH': '', to the Dutch i18n subsites settings solved the issue.

Related

how can I throw all the categories into the header [duplicate]

I'm putting together a project management website for my team using django. My base template includes a sidebar menu which contains a list of all projects and users, linking to a DetailView for that user or project, respectively.
My problem is that I need to provide the User and Project models to every view so that I can render that sidebar. I know how to add extra context; the problem is that I feel like I'm violating DRY by modifying the context at each level. Is it possible to simply redefine the base TemplateClass so that all child classes—ListView, DetailView, etc.—contain the modified context?
On a related note, if this is a terrible way to set up the project, let me know that as well.
You could use the template context processor:
myapp/context_processors.py:
from django.contrib.auth.models import User
from myapp.models import Project
def users_and_projects(request):
return {'all_users': User.objects.all(),
'all_projects': Project.objects.all()}
And then add this processor to the TEMPLATE_CONTEXT_PROCESSORS setting for Django version < 1.8:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'myapp.context_processors.users_and_projects',
)
And for Django version >= 1.8 add it to the context_processors list in the OPTIONS of the TEMPLATES setting:
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'myapp.context_processors.users_and_projects',
],
},
},
]
Context processor will run for ALL your requests. If your want to run these queries only for views which use the base.html rendering then the other possible solution is the custom assignment tag:
#register.assignment_tag
def get_all_users():
return User.objects.all()
#register.assignment_tag
def get_all_projects():
return Project.objects.all()
And the in your base.html template:
{% load mytags %}
{% get_all_users as all_users %}
<ul>
{% for u in all_users %}
<li>{{ u }}</li>
{% endfor %}
</ul>
{% get_all_projects as all_projects %}
<ul>
{% for p in all_projects %}
<li>{{ p }}</li>
{% endfor %}
</ul>

Using Vue.js inside Django template

What I would like to accomplish is to set a django block content inside a vue app. I used vue cli to create a vue app but I am not able to do the following.
views.py
def login(request):
template_name = 'login.html'
return render(request, template_name)
base.html
{% load static %}
<html>
<body>
<div id="app">
{% block component %}{% endblock %}
</div>
</body>
</html>
<script src="{% static 'assets/js/app.js' %}"></script>
login.html
{% extends 'base.html' %}
{% block component %}
<myComponent />
{% endblock %}
What kind of setup do I need to build app.js in way that I can put whatever component I want in the templates?
below is the NOT working vue files
main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
App.vue
<template>
<div id="app">
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
<style>
</style>
Currently if I add tags within the component block in login.html nothing changes.
If I add a tag inside App.vue it will show up in login.html but I want to be able to choose which components will be displayed.
EDIT:
I have made some progress and kind of accomplished what I want but not 100%
I copied files over from a laravel project that uses vue in the way I have described.
package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --disable-host-check --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.19",
"bootstrap": "^4.0.0",
"cross-env": "^7.0",
"jquery": "^3.2",
"laravel-mix": "^5.0.1",
"lodash": "^4.17.19",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.20.1",
"sass-loader": "^8.0.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
}
}
webpack.mix.js
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
This configuration accomplishes what I want but I do not want to use the laravel-mix. I tried removing it from the package.json and webpack.mix.js and I receive build fails. The laravel-mix is described as a configuration layer on top of webpack so I'm assuming if I can get pointed in the right direction with how to set that up properly, I will be good to go.

Django - django-privacy-mgmt package not displaying html

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!

django: how do I actually override admin site template

I know this is asked and answered several times but I basically went over all the post on stack overflow and still couldn't get this to work. Right now I am just trying simply change the admin site title. I have the following:
#base_site.html
{% extends "admin/base_site.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('NEW TITLE') }}{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('NEW TITLE') }}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
And I tried to put this in
my_site/templates/admin/base_site.html,
my_site/templates/admin/my_app/base_site.html, and
my_site/my_app/templates/admin/base_site.html,
but none of these work.
settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
I also tried just directly changing django\contrib\admin\templates\admin\base_site.html but still nothing happens.
I am really frustrated now and definitely could use some help, thanks
Updates:
Actually I found out that the local template does have effect.
Like here, the topmost white bar displays "#base_site.html!!##" which is what I put in my_site/templates/admin/base_site.html as a comment by chance. So it kinda working, but I still don't understand why I can't change the site title.
Add your Django app above 'django.contrib.admin' in settings -> INSTALLED_APPS. The order of apps in INSTALLED_APPS matters.
# settings.py
...
INSTALLED_APPS = [
"your_app.yourAppConfig", # add your app here
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
...
use my_site/my_app/templates/admin/base_site.html
put your app where you define this template before
'django.contrib.admin', in INSTALLED_APPS
source link
First open your settings.py and add a template folder:
TEMPLATES = [
{
'DIRS': [
'/path/to/your/django-project/templates', # Absolute Path
# os.path.join(BASE_DIR, 'templates') # Relative Path
],
...
Then move the file base_site.html to the following directory.
It's important to keep the structure on the file system.
/path/to/your/django-project/templates/admin/base_site.html
Content of base_site.html:
{% extends "admin/base_site.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('NEW TITLE') }}{% endblock %}
{% block branding %}
<h1 id="site-name">Your new Title</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
Django Documentation - Overriding Admin Site
Hope that helps.
So you should delete including the curly braces, and just write the text you want.
Remove: `{{ site_title|default:_('NEW TITLE') }}` Write: NEW TITLE
Remove: `{{ site_header|default:_('NEW TITLE' }}` Write: NEW TITLE
Also be sure that you placed the folders (templates/admin) in the root directory of your project. root directory is the container for all your app folders and also the manage.py file.
In other words, "templates" folder you want to create should be at the same level with manage.py file.
Your folder structure should look like this:
mysite/
templates/
admin/
base_site.html
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
...
...
I am not sure if I understand right. but you can easy to change the title by doing this:
in the "urls.py" file
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
.......
]
admin.site.site_header = 'My New Title'
admin.site.site_title = 'My New Title'
Then it should get the job done for you.
To change Django Admin site title, you can add these two lines in the admin.py of your Django app:
from django.contrib import admin
# change Django admin title
admin.site.site_title = 'My Blog Admin'
# change Django admin site header
admin.site.site_header = 'My Blog Admin'

Django crispy-forms cannot find CSS

I am using Django and Crispy Forms. I can get the form to render correctly, but no CSS formatting appears. What do I need to do?
I have added
CRISPY_TEMPLATE_PACK = 'bootstrap'
to my settings.py file.
The html file is as simple as it gets:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}
What else is necessary to make it work? I understand that since the bootstrap files come bundled with crispy_forms, I don't need to copy and reference them specifically in my project's CSS path. Is this correct?
It sounds as if your django instance can't find the static files for crispy forms. If you are running the development server, have you included 'crispy_forms' in INSTALLED_APPS?
If you're running a production server you'll probably need to ensure that your STATIC_FILES paths are correct and you've run collectstatic recently.
The authors have documented the installation process here - have you followed that completely?
Its supposed to be {{ form|crispy }}, not {{ crispy form }}. You are putting the form through a filter (which is what | does in Django templates).
Also, you forgot the {% csrf_token %}.

Categories