Add custom button near Save button Django-admin - python

I have added a custom button in django admin, however its below the Save and Save and Close buttons, how can I make this custom button on the same line with the 2 buttons above, below is the image:
Then, how I overridden the button with the templates:
{% extends 'admin/custominlines/change_form.html' %}
{% load i18n %}
{% block submit_buttons_bottom %}
{{ block.super }}
{% if request.GET.edit %}
<div class="submit-row">
{% for obj in transitions %}
<input type="submit" value="{{ obj }}" name="{{ obj }}">
{% endfor %}
</div>
{% endif %}
{% endblock %}

Django has a <div class="submit-row"> for each row. You are adding a new row. What you need to do is to put your buttons in the same div with Django. Here is Django's code modified to have your buttons.
{% load i18n admin_urls %}
<div class="submit-row">
{% block submit-row %}
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link and original %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box">{% trans "Delete" %}</p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% if can_change %}{% trans 'Save and continue editing' %}{% else %}{% trans 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_close %}{% trans 'Close' %}{% endif %}
{% endblock %}
// Django code above
// Your buttons below
{% for obj in transitions %}
<input type="submit" value="{{ obj }}" name="{{ obj }}">
{% endfor %}
</div>
Your code at the end will look like this.
{% extends 'admin/custominlines/change_form.html' %}
{% load i18n %}
{% block submit_buttons_bottom %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link and original %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box">{% trans "Delete" %}</p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% if can_change %}{% trans 'Save and continue editing' %}{% else %}{% trans 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_close %}{% trans 'Close' %}{% endif %}
// Django code above
// Your buttons below
{% for obj in transitions %}
<input type="submit" value="{{ obj }}" name="{{ obj }}">
{% endfor %}
</div>
{% endblock %}

You don't have to override change_form.html. Instead, you can just override submit_line.html
{% extends 'admin/submit_line.html' %}
{% load i18n admin_urls %}
{% block submit-row %}
{{ block.super }}
{% for obj in transitions %}
<input type="submit" value="{{ obj }}" name="{{ obj }}">
{% endfor %}
{% endblock %}

You can add a custom button in the same line of "SAVE" button on "Add" form and "Change" form for a specifc admin.
To do that, by creating "templates/admin/custom_change_form.html" and "templates/admin/submit_line.html" in the root django project directory, you need to override "change_form.html" under django library whose path is "django/contrib/admin/templates/admin/change_form.html" and "submit_line.html" under django library whose path is "django/contrib/admin/templates/admin/submit_line.html".
Then, you need to add the code of a custom button to "submit_line.html" as shown below:
# "submit_line.html"
{% load i18n admin_urls %}
<div class="submit-row">
{% block submit-row %}
{% if custom_button %}<input type="submit" style="float: right;margin-left: 8px;" value="{% translate 'Custom button' %}" name="_custom_button">{% endif %}
{% if show_save %}<input type="submit" value="{% translate 'Save' %}" class="default" name="_save">{% endif %}
{% if show_delete_link and original %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box">{% translate "Delete" %}</p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% translate 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% translate 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% if can_change %}{% translate 'Save and continue editing' %}{% else %}{% translate 'Save and view' %}{% endif %}" name="_continue">{% endif %}
{% if show_close %}{% translate 'Close' %}{% endif %}
{% endblock %}
</div>
You can find more detail in How to add a custom button right next to "SAVE" button on "Add" form and "Change" form for a specifc admin then you will archive what you want to do.

Related

django widget tweaks form control not action

when Making django board, I met the problem.
django version is 3.
I wanted form like this. but my code can't alert me "This field is required"
But widget tweaks form-control wasn't working.
which part is problem?
My code is like this
{% extends 'base.html' %}
{% load widget_tweaks %}
{% block title %}Start a New Topic{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item">Boards</li>
<li class="breadcrumb-item">{{ board.name }}</li>
<li class="breadcrumb-item active">New topic</li>
{% endblock %}
{% block content %}
<form method="post" novalidate>
{% csrf_token %}
{% for field in form %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">
{{ field.help_text }}
</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-success">Post</button>
</form>
{% endblock %}
Try using bootstrap or django crispy forms in your form.
just change your code to blow with proper indent
{% load widget_tweaks %}
{% block title %}Start a New Topic{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item">Boards</li>
<li class="breadcrumb-item">{{ board.name }}</li>
<li class="breadcrumb-item active">New topic</li>
{% endblock %}
{% block content %}
<form method="post" novalidate>
{% csrf_token %}
{% for field in form %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">
{{ field.help_text }}
</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-success">Post</button>
</form>
{% endblock %}```

Reverse for 'django_markdown_preview' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

I am using django_markdown but it is showing error when I am trying to render form. The error is on 17th line {{ form }} Error. The error is in reverse of 'django_markdown_preview', but I have included
url('^markdown/', include('django_markdown.urls')),
in my urls.py. Can anyone help me to resolve the error.
{% extends 'qaforum/base.html' %}
{% load django_markdown %}
{% block content %}
{% if message %}
<strong>Enter a valid Question!</strong>
{% endif %}
<form method="post" action="{% block action_url %}{% url 'qaforum:qaforum_create_question' %}{% endblock action_url %}">
{% csrf_token %}
{% for field in form.visible_fields %}
<tr>
<th>{{ field.label_tag }}</th>
<td>
{{ field.errors }}
{{ field }}
{{ field.help_text }}
</td>
</tr>
{% endfor %}
<input class="btn pull-right btn-success" type="submit" value="Submit Question" />
</form>
</div>
{% endblock content %}
{% block extra_js %}
{% markdown_editor "#id_description" %}
{% markdown_media %}
{% endblock extra_js %}

Setting up Django Form Wizard

I'm attempting to set up a Django Form Wizard, but I'm having trouble getting it to work. I've followed the example, from the Django website, but can't get anything to work. When I go to the URL that should have my multistep form, only the template html thats extended shows up. What am I doing wrong here?
FORMS.PY
class ScheduleDate(forms.Form):
date = forms.CharField()
class ScheduleTime(forms.Form):
time = forms.CharField()
VIEWS.PY
FORMS =[('date', ScheduleDate),
('time', ScheduleTime)]
TEMPLATES = [('date', 'main/schedule_date2.html'),
('time', 'main/schedule_time.html')]
class ContactWizard(SessionWizardView):
def get_template_name(self):
return TEMPLATES[self.steps.current]
def done(self, form_list, **kwargs):
print 'testing'
return HttpResponseRedirect('main/home.html')
URLS.PY
urlpatterns = patterns('',
url(r'^checkout/$', views.ContactWizard.as_view([ScheduleDate, ScheduleTime]), name='checkout'))
SCHEDULE_DATE2.HTML(From the Django Website)
{% extends "base.html" %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
{% endblock %}
Thanks!

Django-userena showing forms twice when using with Crispy Forms

I am trying to use Crispy forms with Django-userena to make it look better but when ever I put the Crispy form tags in it duplicates the form,
My code is as followed:
{% extends 'userena/base_userena.html' %}
{% load i18n %}
{% load url from future %}
{% load crispy_forms_tags %}
{% block title %}{% trans "Signin" %}{% endblock %}
{% block content %}
<form action="" method="post" class="formholder">
{% csrf_token %}
{{ form|crispy }}
<fieldset>
<legend>{% trans "Signin" %}</legend>
{{ form.non_field_errors }}
{% for field in form %}
{{ field.errors }}
{% comment %} Displaying checkboxes differently {% endcomment %}
{% if field.name == 'remember_me' %}
<p class="checkbox">
<label for="id_{{ field.name }}">{{ field }} {{ field.label }}</label>
</p>
{% else %}
<p>
{{ field.label_tag }}
{{ field }}
</p>
{% endif %}
{% endfor %}
</fieldset>
<input type="submit" value="{% trans "Signin" %}" />
<p class="forgot-password">{% trans "Forgot your password?" %}</p>
{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
</form>
{% endblock %}
Solution by OP.
The problem was that the, {{ form|crispy }} was actually creating the form in the form.py file all I had to do was remove all the other from tags and just keep the {{ form|crispy }}
Here is what it looks like:
{% extends 'userena/base_userena.html' %}
{% load i18n %}
{% load url from future %}
{% block title %}{% trans "Signin" %}{% endblock %}
{% block content %}
{% load crispy_forms_tags %}
<form action="" method="post" class="formholder">
{% csrf_token %}
<fieldset>
<legend>{% trans "Signin" %}</legend>
{{ form|crispy }}
</fieldset>
<input type="submit" value="{% trans "Signin" %}" />
<p class="forgot-password">{% trans "Forgot your password?" %}</p>
{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
</div>
</form>
{% endblock %}

django-cms: define a placeholder in a template without automatically showing it

I want to create a template that has some content
{% placeholder content %}
plus some "teaser" content that I want to display on another page, but not on the page that uses this specific template. Obviously, the page that displays the content does not need a teaser for it.
I'm using cmsplugin_filer and the solution I found seems way too complex:
In my template I'm defining the teaser placeholder as follows:
{% with skipteaser=True %}
{% placeholder teaser %}
{% endwith %}
and then overide the cmsplugin_filer template as follows:
Instead of
<div{% if instance.style %} class="{{ instance.style }}"{% endif %}>
<h2>{{ instance.title }}</h2>
{% if instance.image or instance.image_url %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% endif %}
{% if instance.image_url %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ instance.image_url }}"{% if instance.width or instance.height %} style="{% if instance.width %}width:{{ instance.width }}px;{% endif %}{% if instance.height %}height:{{ instance.height }}px;{% endif %}"{% endif %} />
{% else %}
{% thumbnail instance.image size crop upscale subject_location=opts.subject_location as thumbnail %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ thumbnail.url }}" />
{% endif %}
{% if link %}</a>{% endif %}
{% endif %}
{% if instance.description %}
<p>{{ instance.description }}</p>
{% endif %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% trans "more" %} »</a>{% endif %}
</div>
I use:
{% if skipteaser %}
{% else %}
<div{% if instance.style %} class="{{ instance.style }}"{% endif %}>
<h2>{{ instance.title }}</h2>
{% if instance.image or instance.image_url %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% endif %}
{% if instance.image_url %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ instance.image_url }}"{% if instance.width or instance.height %} style="{% if instance.width %}width:{{ instance.width }}px;{% endif %}{% if instance.height %}height:{{ instance.height }}px;{% endif %}"{% endif %} />
{% else %}
{% thumbnail instance.image size crop upscale subject_location=opts.subject_location as thumbnail %}
<img alt="{% if instance.alt %}{{ instance.alt }}{% endif %}" src="{{ thumbnail.url }}" />
{% endif %}
{% if link %}</a>{% endif %}
{% endif %}
{% if instance.description %}
<p>{{ instance.description }}</p>
{% endif %}
{% if link %}<a href="{{ link }}"{% if obj.target_blank %} target="_blank"{% endif %}>{% trans "more" %} »</a>{% endif %}
</div>
{% endif %}
so that the teaser does not show in the page that uses the template, but I can invoke it in other pages with
{% show_placeholder "teaser" other_page %}
this seems too complex. Isn't there a way for me to define a placeholder in a template without automatically showing it?

Categories

Resources