Custom Button in django - django

I am trying to create custom Button in Django Admin
admin.py
class HeroAdmin(admin.ModelAdmin):
change_list_template="hellogym/hellogymapp/templates/change_list.html"
change_list.html
{% extends 'admin/change_list.html' %}
{% block object-tools %}
<div>
<form action="immortal/" method="POST">
{% csrf_token %}
<button type="submit">Make Immortal</button>
</form>
<form action="mortal/" method="POST">
{% csrf_token %}
<button type="submit">Make Mortal</button>
</form>
</div>
<br />
{{ block.super }}
{% endblock %}
I have created superuser Admin and I want a custom button inside it

Related

How to solve 'This field is required' for django forms

I created a blog like app. Whenever I update an entry and press the submit button, I want it to take me to view page for that blog entry. However it just redirects me to the update page. When I check form.errors it says that 'This field is required' for all of the fields. But all of the fields are filled with data.
{% extends 'base.html' %}
{% block content %}
{{ form.as_p }}
<form method="POST" action="">
{% csrf_token %}
<input type="submit" value="Update">
</form>
{% endblock content %}
You have your form declared in the wrong block:
{% extends 'base.html' %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
Notice that the {{form.as_p}} has to be declared into the form block.
If you fill all data but your form is out of the form tag then the data will not be send.

Image does not upload using createview from template django

CompanyCreateView is a generic view. The file uploads from django administration but does not upload from template.
company_logo is stored in mysql database
class CompanyCreateView(CreateView):
model = Company
fields = ['company_name', 'company_description', 'company_email',
'company_website', 'company_address', 'company_phone', 'company_status',
'company_monthly_payment', 'company_logo']
company_form.html
{% extends "super_admin/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form action="" method="POST">
{% csrf_token %}
<fieldset class="from-group">
{% if object.company_name.strip %}
<legend class="border-bottom mb-4">Update Company</legend>
{% else %}
<legend class="border-bottom mb-4">Enter New Company</legend>
{% endif %}
{{ form | crispy}}
</fieldset>
<div class="form-group">
{% if object.company_name.strip %}
<button class="btn btn-outline-info" type="submit">Update</button>
<a class="btn btn-primary" href="{% url 'super-company-delete' object.id %}" role="button">Delete</a>
{% else %}
<button class="btn btn-outline-info" type="submit">Save</button>
{% endif %}
</div>
</form>
</div>
{% endblock content %}
I have found the answer i was missing enctype in form. Just replace the form tag
<form action="" method="POST" enctype="multipart/form-data">

Invalid filter add_class using django-widget-tweaks

I am creating form view using django-widget-tweaks following this tutorial.
When I tried to implement add_class filter, I got following error.
Invalid filter: 'add_class'
Does anyone know how to solve this error?
html page
{% extends "base.html" %}
(% load widget_tweaks %}
{% block content %}
<form method="post" enctype="multipart/form-data">
<h4 style="margin-top: 0">Project Upload</h4>
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{hidden}}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group">
<label for="{{field.id_for_label}}">{{field.label}}</label>
{{field|add_class:"form-control"}}
</div>
{% endfor %}
<button type="submit">Upload</button>
</form>
{% endblock %}
form.py
class DocumentForm(forms.ModelForm):
class Meta:
model=html
fields=['project','version','diff','program','location']
You can check this-out here
{% load widget_tweaks %}
<!-- add 2 extra css classes to field element -->
{{ form.title|add_class:"css_class_1 css_class_2" }}
Try this :
{% render_field field|add_class:"form-group"%}

Django Registration Registration_form

I'm using Django Registration_redux templates for user registrations.
I am wondering how does Django know where to go when user submits his registration when action is action="." ?
{% extends "registration/registration_base.html" %}
{% load i18n %}
{% block title %}{% trans "Register for an account" %}{% endblock %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
{% comment %}
**registration/registration_form.html**
Used to show the form users will fill out to register. By default, has
the following context:
``form``
The registration form. This will be an instance of some subclass
of ``django.forms.Form``; consult `Django's forms documentation
<http://docs.djangoproject.com/en/dev/topics/forms/>`_ for
information on how to display this in a template.
{% endcomment %}
If your action is blank or . then the post goes to the view (URL) which rendered the form.
Here's a simple example using a contact form; https://hellowebapp.com/news/tutorial-setting-up-a-contact-form-with-django/

Why is this django formset not being submitted?

i have a formset as follows:
EduFormSet = formset_factory(forms.CandidateDegreeForm, can_delete=True)
edu_formset = EduFormSet(prefix='candidate_degree')
in the templates i am doing the following:
{% if edu_formset %}
{% for form in edu_formset %}
<div class="formset-form" style="visibility: visible;">
<form id="{{ form.prefix }}" method="POST" action="/degree/add/">
<h4>Some Heading Here</h4>
{% csrf_token %}
{% for field in form %}
{% include "form_field.html" %}
{% endfor %}
</form>
<script type="text/javascript">
jQuery(document).ready ( function(){
jQuery('{{ form.prefix }}').validationEngine();
});
</script>
<div class="clearfix"></div>
</div>
{% endfor %}
{{ edu_formset.management_form }}
<div class="button-container right">
<input class="button" type="submit" value="submit" />
</div>
{% endif %}
I am not sure why but nothing really happens when i hit the submit button.
Your submit button is not within the form, so the action is not triggered by the click!
Here's how the docs show you to render formsets:
<form method="post" action="">
<!-- Notice how the formset (below) and thus its submit button
is INSIDE the form (above) -->
{{ formset.management_form }}
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
</form>
You try to create multiple forms with the form.prefix for id. This could work but each form would have to be rendered with its own submit button. Formsets are designed to combine multiple forms into one and guarantee uniqueness of value names by said prefix. They would be enclosed in a singe form and share any submit triggers.