adding form styling to django forms - python

I am currently in the process of adding a theme to the admin of django one issue I have found it adding styling to the forms is that it is very difficult and I can't find much useful documentation on it. The only real thing I need to do is add classes to the form elements so that they match the theme I am using is this possible and if so how would you go about doing it the code I am currently using is very basic and the basic code included in the standard theme does anybody know how to add classes to these standard bits of code bellow is what I have.
{% if is_popup %}
<input type="hidden" name="_popup" value="1" />
{% endif %}
{% if save_on_top %}
{% block submit_buttons_top %}
{% submit_row %}
{% endblock %}
{% endif %}
{% if errors %}
<p class="errornote">
{% blocktrans count counter=errors|length %}
Please correct the error below.
{% plural %}
Please correct the errors below.
{% endblocktrans %}
</p>
{{ adminform.form.non_field_errors }}
{% endif %}
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/includes/fieldset.html" %}
{% endfor %}
{% endblock %}
{% block after_field_sets %}{% endblock %}
{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
{% endblock %}
{% block after_related_objects %}{% endblock %}
{% block submit_buttons_bottom %}
{% submit_row %}
{% endblock %}

There are a lot of ways to do this, but certainly one way would be to overwrite all the widgets in your ModelAdmin rather than in the template. That could look something like this:
from django.db import models
from django.contrib import admin
from django.forms.extras.widgets import TextInput
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': TextInput(attrs={'class':'my-widget-class'},)},
}
You'd have to go through and do that for each widget, but then they'd have the appropriate classes -- at least for that modelAdmin.

Related

How to extend multiple bases or conditional {% extends %} in one html page in django?

{% if request.user.profile.emp_desi in qa_list %}
{% extends "qa_base.html" %}
{% elif request.user.profile.emp_desi in mgr_list %}
{% extends "manager_base.html" %}
{% else %}
{% extends "common_base.html" %}
{% endif %}
How can I solve this problem??
based on designation I want to extend different different bases.
You can use include
You need to create a directory and template layout in such a way that
you will have one base and multiple includes.
https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#include
{% if request.user.profile.emp_desi in qa_list %}
{% include "qa_base.html" %}
{% elif request.user.profile.emp_desi in mgr_list %}
{% include "manager_base.html" %}
{% else %}
{% include "common_base.html" %}
{% endif %}

Django Template Aren't Loading

I'm trying to load different html files into the base.html and they're not showing. Any ideas?
<body class="bg">
<main class='text'>
{% block carousel %}
{% endblock %}
{% block info%}
{% endblock %}
{% block content %}
{% endblock %}
{% block mobile %}
{% endblock %}
</main>
</body>
I think you may be confusing template inheritance with template composition.
In template inheritance, you have a base page like base.html:
<body class="bg">
<main class='text'>
{% block carousel %}
{% endblock %}
{% block info%}
{% endblock %}
{% block content %}
{% endblock %}
{% block mobile %}
{% endblock %}
</main>
</body>
Then, you have a second template shoes.html that extends base.html. It inherits all the HTML from base.html, but fills in some custom content inside the blocks:
{% extends "base.html" %}
{% block carousel %}
<p>Carousel</p>
{% endblock %}
{% block info%}
<p>Info</p>
{% endblock %}
{% block content %}
<p>Content</p>
{% endblock %}
{% block mobile %}
<p>Mobile</p>
{% endblock %}
Therefore, when you render it inside your view:
views.py
from django.shortcuts import render
def index(request):
return render(request, 'polls/shoes.html', {})
you get this result:
<body class="bg">
<main class='text'>
<p>Carousel</p>
<p>Info</p>
<p>Content</p>
<p>Mobile</p>
</main>
</body>
I suspect that you have different HTML files named after each block (e.g. carousel.html, info.html). That's not how Django works (but it is how Ruby on Rails works). Is that what you were trying to do? If not, please update your question to clarify.

Django CMS – Show different content for users and guests in same template

I would like to have different content for users and guests in my home page's template using Django 1.9 and Django CMS 3.3.1.
It could be acomplished by making subpages and showing the corresponding content in the ancestor based on authentication conditional, but that makes the page structure overly complicated.
Is there an easy way of adding these placeholders straight to the template?
I have tried this:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
{% if not user.is_authenticated %}
{% placeholder "guests" %}
{% endif %}
{% if user.is_authenticated %}
{% placeholder "authenticated" %}
{% endif %}
{% placeholder "content" %}
{% endblock content %}
But as I am authenticated when I'm editing the content, I cannot access the guests placeholder.
Try this:
{% block content %}
{% if request.toolbar.build_mode or request.toolbar.edit_mode %}
{% placeholder "guests" %}
{% placeholder "authenticated" %}
{% else %}
{% if not user.is_authenticated %}
{% placeholder "guests" %}
{% endif %}
{% if user.is_authenticated %}
{% placeholder "authenticated" %}
{% endif %}
{% endif %}
{% placeholder "content" %}
{% endblock content %}
I have some experience with Django CMS but don't know if this will work. The idea is to check if we're in edit mode by inspecting corresponding request variables. See this answer.
Update by #V-Kopio:
The answer given above works fine in practice but Django warns about dublicate placeholders. This can be avoided by combining the if and else blocks:
{% block content %}
{% if not user.is_authenticated or request.toolbar.build_mode or request.toolbar.edit_mode %}
{% placeholder "guests" %}
{% endif %}
{% if user.is_authenticated %}
{% placeholder "authenticated" %}
{% endif %}
{% placeholder "content" %}
{% endblock content %}

Simplifying the compound extends

On my homepage, I would like to use other pages I have defined as sections. After my navigation, I have a portion for news and team members that run as stand alone pages on my site. It looks something like this:
Header
Nav
<div class="content">
{% include 'news.html' %}
{% include 'officers.html' %}
</div>
Footer
So my news has some basic html but in order not to clone my headers and nav I have to add this line:
{% if page_data.current_page == 'news' %} {% extends "base.html" %} {% endif %}
Is there a way to simplify this statement?
An alternative way to deal with the issue is to move the body of news.html into a partial, for example partials/news.html and then include it in both your home page and in the news page itself:
{# home.html #}
Header
Nav
<div class="content">
{% include 'partials/news.html' %}
{% include 'officers.html' %}
</div>
Footer
and then in news.html:
{% extends "base.html" %}
{% block where_news_belongs %}
{{ super() }} {# if we need to include the contents of the block #}
{% include 'partials/news.html %}
{% endblock where_news_belongs %}

Jinja2: How to use named blocks inside included templates, inside extendable template

I am having the Issue with Jinja2 Extend and Import.
base_admin.html
<html>
<body>
<div class="outerbody">
<somehtml code>
{% include "base_admin_nav.html" %}
{% include "base_admin_sidebar.html" %}
{% include "base_admin_content.html" %}
</div>
</body>
</html>
base_admin_content.html
<div class="innerbody">
{% block body_content %}
{% endblock %}
</div>
admin.html
{% extends 'base_admin.html' %}
{% block body_content %}
<div>BodyContent</div>
{% endblock %}
The code inside body_content is not passed to base_admin_content.html. Any workarounds?
Note
This is not duplicate of this one
jinja2: blocks in included files..
The include is done in different files here
Defining {% macro admin_content() %} insdide base_admin_content.html and importing it inside base_admin.html using
{% from "base_admin_content.html" import admin_content with context %}
{{ admin_content() }}.
also has no effect.
Edited - to reflect changes in original question
Ok, now that I know you definitely need the includes, here's how I would do it: instead of including the base_admin_content.html file, you should include the admin.html file directly into base_admin.html. The admin.html file will extend base_admin_content.html and everything should work just fine:
base_admin.html
<html>
<body>
<div class="outerbody">
<somehtml code>
{% include 'admin.html' %}
</div>
</body>
</html>
admin.html
{% extends 'base_admin_content.html' %}
{% block body_content %}
<div>BodyContent</div>
{% endblock %}
base_admin_content.html
{% block innerbody %}
<div class="innerbody">
{% block body_content %}
{% endblock %}
</div>
{% endblock %}
Why does this work but your original code does not?
In your base_admin.html file you have
{% include 'base_admin_content.html' %}
Where we have no reference to admin.html, which is why nothing from the admin.html file shows up!. Therefore, we should do this:
{% include 'admin.html' %}
Because that does contain a reference to base_admin_content in the extends line:
{% extends 'base_admin_content.html' %}
Hopefully that makes sense...
You could inherit from base_admin and base_admin_content separately:
base_admin.html:
<html>
<body>
<div class="outerbody">
...
{% block admin_content %}
{% include "default_admin_content.html" %}
{% endblock %}
</div>
</body>
</html>
base_admin_content.html: (unchanged)
<div class="innerbody">
{% block body_content %}{% endblock %}
</div>
admin.html:
{% extends 'base_admin.html' %}
...
{% block admin_content %}
{% include "admin_content.html" %}
{% endblock %}
admin_content.html:
{% extends 'base_admin_content.html' %}
{% block body_content %}
<div>BodyContent</div>
{% endblock %}
This way base_admin doesn't need to know about base_admin_content's blocks,
it's flexible and simple.

Categories

Resources