Am trying to add another custom button, and at the same time I want it to trigger certain processes in Django-admin with templates, below is how I have implemented the template :
{% extends 'admin/custominlines/change_form.html' %}
{% load i18n %}
{% block submit_buttons_bottom %}
{{ block.super }}
{% if request.GET.edit %}
<div class="submit-row">
<input type="submit" value="Custom button" name="_transition-states">
</div>
{% endif %}
{% endblock %}
The button does appear however, when I click it , it doesn't show the print I have inserted in the response_change() function in the admin.py, what am I missing here :
def response_change(self, request, obj):
if '_transition-states' in request.POST:
print("am working")
return super().response_change(request, obj)
When I click the button it just routes back to the previous page.
Related
I am running Django 2.2 and have written a simple shopping cart. I wish to validate two fields at the same time in such a way that both cannot be empty at the same time. In my forms.py,
from django import forms
class CartAddProductForm(forms.Form):
cc_handle = forms.CharField(required=False, label='CC Handle', empty_value='')
lc_handle = forms.CharField(required=False, label='LC Handle', empty_value='')
def clean(self):
cleaned_data = super().clean()
if cleaned_data.get('cc_handle') == '' and cleaned_data.get('lc_handle') == '':
print("issue detected")
raise forms.ValidationError('Either cc or lc handle is required.')
return cleaned_data
This is following the official Django docs on cleaning and validating fields that depend on each other. The print() statement above lets me know that the issue has been detected, i.e. both fields are empty. Running the Django server, I see that the issue was indeed detected but no validation error message was displayed on top of the originating page. The originating page is the product page that contains the product and a link to add the product to the shopping cart. Normally the validation error message is displayed at the top of the page.
According to the docs, the validation is done when is_valid() is called. So I put a diagnostic print of my views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from shop.models import Product
from .cart import Cart
from .forms import CartAddProductForm
#require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product,
cc_handle=cd['cc_handle'],
lc_handle=cd['lc_handle'])
else:
print('invalid form')
return redirect('cart:cart_detail')
And indeed the words 'invalid form' popped up. The code then takes me to the shopping cart. Instead, what I want is to be at the product page and show the validation error informing the reader that both fields cannot be empty. Is there a simple way of doing it?
For required=True fields in the forms, if I leave it blank, there will be a message popping up saying that I need to fill it in. So I want to do something similar except the validation requires that both fields cannot be empty.
This is different from this Stackoverflow answer because that is a registration form. You can redirect it to the same form whereas for this case, the CartAddProductForm is embedded in all the products page on the site. If possible, I want the validation to occur at the same stage as the field with required=True option.
The product/detail.html template looks like the following.
{% extends "shop/base.html" %}
{% load static %}
{% block title %}
{{ product.name }}
{% endblock %}
{% block content %}
<div class="product-detail">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
<h1>{{ product.name }}</h1>
<h2>{{ product.category }}</h2>
<p class="price">${{ product.price }}</p>
<form action="{% url "cart:cart_add" product.id %}" method="post">
{{ cart_product_form }}
{% csrf_token %}
<input type="submit" value="Add to cart">
</form>
{{ product.description|linebreaks }}
</div>
{% endblock %}
Adding this line in form template has cleared your issue.
{{ cart_product_form.non_field_errors }}
product/detail.html:
{% extends "shop/base.html" %}
{% load static %}
{% block title %}
{{ product.name }}
{% endblock %}
{% block content %}
<div class="product-detail">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
<h1>{{ product.name }}</h1>
<h2>{{ product.category }}</h2>
<p class="price">${{ product.price }}</p>
<form action="{% url "cart:cart_add" product.id %}" method="post">
{{ cart_product_form }}
{% csrf_token %}
{{ cart_product_form.non_field_errors }} // This line will raise validation errors
<input type="submit" value="Add to cart">
</form>
{{ product.description|linebreaks }}
</div>
{% endblock %}
Doc:(Copied from official documentation)
Note that any errors raised by your Form.clean() override will not be
associated with any field in particular. They go into a special
“field” (called all), which you can access via the
non_field_errors() method if you need to. If you want to attach errors
to a specific field in the form, you need to call add_error().
Your view is inconditionnally redirecting to cart_details so no surprise you don't see the validation errors - you'd have to render the invalid form for this. You should only redirect when the post succeeded.
I would like to disable all buttons that they are present into the submit-row (save, save and edits, delete, etc...) after the click on one of they.
I started to try to override the change_form for admin model. Something
like this:
class MyAdmin(admin.ModelAdmin):
change_form_template = 'admin/reports/models/change_form.html'
into the admin/reports/models/change_form.html file I added this code:
{% extends "admin/change_form.html" %}
{% 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 %}
{% 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 class="myclass" type="submit" value="{% trans 'Save as new' %}" name="_saveasnew">{% endif %}
{% if show_save_and_add_another %}<input class="myclass" type="submit" value="{% trans 'Save and add another' %}" name="_addanother">{% endif %}
{% if show_save_and_continue %}<input class="myclass" 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 %}
</div>
But, I don't see any changes (class="myclass" there aren't).
My next changes will be the js code to disable all buttons at on click... but now I'm blocked on this first problem.
I use python 3 and Django 2
If you are trying to add classes to the inputs just so you can use JavaScript to disable them upon click, that's not necessary. You should be able to select the buttons pretty easily without adding classes to them. For example, using jQuery included in the Django admin:
django.jQuery(".submit-row :submit").attr("disabled", "disabled")
If you truly need to override the rendering of the button HTML, you should look at overriding the blocks submit_buttons_bottom and submit_buttons_top:
{% extends "admin/change_form.html" %}
{% if save_on_top %}your submit buttons here{% booking_submit_row %}{% endblock %}{% endif %}
{% block submit_buttons_bottom %}your submit buttons here{% endblock %}
If you want to avoid repeating yourself with this approach, consider using a custom submit-line.html and building a custom templatetag to output your submit buttons in the above template, like so:
from django.contrib.admin.templatetags.admin_modify import submit_row
from django.template import Library
register = Library()
#register.inclusion_tag("admin/<your app>/<your model>/submit_line.html", takes_context=True)
def custom_submit_row(context):
ctx = submit_row(context)
original = context["original"] if "original" in context else None
# adjust as you need based on your context
return ctx
Now in your custom change_form.html you can use:
{% extends "admin/change_form.html" %}
{% load custom_admin_modify %}
{% if save_on_top %}{% custom_submit_row %}{% booking_submit_row %}{% endblock %}{% endif %}
{% block submit_buttons_bottom %}{% custom_submit_row %}{% endblock %}
Hope that helps - good luck!
I could remove "SAVE" button, "Save and continue editing" button, "Save and add another" button and "Delete" button from "Change person" page with this code below for "admin.py". But, when I removed "SAVE" button, "Close" button appeared then I don't know how to remove "Close" button:
# "admin.py"
from django.contrib import admin
from .models import Person
#admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['show_save'] = False
extra_context['show_save_and_continue'] = False
return super().changeform_view(request, object_id, form_url, extra_context)
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None): # Here
return False
Then, "Delete" button is removed from "Change person" page as shown below:
Using Django, I am making a site that displays tickets. On the login page, I want it to where there is a preview area of existing tickets to the left of the login form. I am able to make both pages seperately, but I can not figure out how to make them co-exist side by side. I am also using jinja2 templating. I've been trying at this for 2 days, and I have read the tutorials over and over. Here is how I have it:
url.py:
urlpatterns = patterns('',
url(r'', include('ticket_app.urls')),
url(r'^accounts/login/$', login, {'authentication_form': forms.LoginForm}, name='login'),
)
views.py Note: Request is the database table object:
class PreviewList(ListView):
model = Request
form.py:
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-8'
self.helper.form_tag = False
self.helper.layout = Layout(
Field('username', placeholder="Username", css_class='input-xlarge'),
Field('password', placeholder="Password", css_class='input-xlarge'),
FormActions(
Submit('login', 'Login', css_class="btn-primary"),
)
base.html(short version):
<h1>{% block page_title %}Preview of tickets{% endblock page_title %}</h1>
{% block preview %}
{% endblock preview %}
{% block login %}
{% endblock login %}
preview.html:
{% extends 'base.html'%}
{% block preview %}
<div class="span6">
<ul class="list-group">
{% if object_list %}
{% for item in object_list %}
<li class="list-group-item">{{item.date_due}} - {{item.desctription}}
<span class="badge">
{% if item.user_assigned %}
<span class="badge"style="color:green"> assigned </span>
{% else %}<span class="badge" style="color:red">unassigned</span>
{% endif %}
</li>
{% endfor %}
{% else %}
<p>Yay! No ticket requests found!</p>
{% endif %}
</div>
{% endblock preview %}
login.html:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block login %}
<div class="container" style="padding-bottom: 70px;">
<div class='row'>
<div class='col-md-6 col-md-offset-3'>
<div class="well">
<legend>Sign in to Site</legend>
<form method="post" action="{% url 'django.contrib.auth.views.login' %}" class="form-horizontal">
{% crispy form %}
<input type="hidden" name="next" value="{{ next }}"/>
</form>
</div>
</div>
</div>
</div>
{% endblock login %}
There are multiple ways to accomplish this. For example, you could create your own login view that 'extends' the one from django.contrib.auth:
from django.contrib.auth.views import login
def login_with_preview(request, extra_context=None, *args, **kwargs):
if extra_context is None:
extra_context = {}
extra_context['object_list'] = Request.objects.all()
return login(request, authentication_form=LoginForm, extra_context=extra_context, *args, **kwargs)
And then use login_with_preview in your url conf and merge your two templates.
Another possibility is to create a custom template tag. This is especially nice if you want to show this preview list in different places:
#register.inclusion_tag("yourapp/preview_list.html")
def ticket_preview_list():
return {'object_list': Request.objects.all()}
preview_list.html should only contain the template code that, right now, is in your preview template block. You need to make sure that template doesn't extend base.html; that would mess things up. The usual template tag setup steps apply.
You could create one view that renders the login form and the preview data
i'm developing a small app with Python and Google app engine. I'm using boilerplate (https://github.com/coto/gae-boilerplate) as front-end which follows gae direction and python templates, so nothing diffrent than plain stuff.
Now, what i would like to have is this.
When a user logged in, if the field of name and last name are not filled in i would like to have, in the home page, the profile editing.
The page for editing the profile is a template (which extend the base.html), called edit_profile.html which works well.
The homepage is a template as well (extend the base.html) called home.html.
Now, can i include the edit_profile.html in home.html? how can i do it?
this is what i've, i don't know what to put instead of ???? i tried with
{% block edit_profile.html %} {% endblock %}
but does not work
{% if user_info.name and user_info.last_name %}
..
{% else %}
????
{% endif %}
thanks.
So you want to include only some block of given template. There are two solutions:
1) Create template just for profile editing form and include it into edit_profile.html. Then include it also into home.html to if condition branch:
profile_form.html:
<form action="{% url some-action %}">
{{ form }}
<input type="submit" value="save"/>
</form
profile_edit.html
{% extends "base.html" %}
{% block main %}
{% include "profile_form.html" %}
{% endblock %}
home.html
{% if user_info.name and user_info.last_name %}
{% include "profile_form.html" %}
{% endif %}
2) use variable for extended template:
profile_form.html
{% extend BASE_TEMPLATE %}
and set it into context w/ different value as needed:
in home.html (let's say included_form.html is some basic template)
{% if user_info.name and user_info.last_name %}
{% with "included_form.html" as BASE_TEMPLATE %}
{% include "edit_profile.html" %}
{% endwith %}
{% endif %}
and if you want show form as a standalone page, set BASE_TEMPLATE to base.html
I'm using django 1.2.4.
I have a template for login in registration/login.html (wich action is django.contrib.auth.views.login) and I want to include it on everypage. I created a block on my base.html as I do for every template. The thing is the browser doesn't recognize this login block and I think it is because I only render a template for each view, I am not rendering this login template.
Here is my folder structure:
/templates/
base.html
/myapp/
object_list.html
...
/registration/
login.html
...and here is my login.html:
{% extends "base.html" %}
{% block mylogin %}
<div class="horizontal">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form action="{% url django.contrib.auth.views.login %}" method="post">
{% csrf_token %}
<div class="login_box">
<div class="login_text">{{ form.username.label_tag }}</div><div class="login_input">{{ form.username }}</div>
<div class="password_text">{{ form.password.label_tag }}</div><div class="password_input">{{ form.password }}</div>
<input id="button_login" type="submit" value="" />
</div>
</form>
</div>
{% endblock %}
...and in my base.html I have:
<div id="some_div">
{% block mylogin %} {% endblock %}
</div>
I have a basestyle.css included in base.html and the other templates inherit correctly too... it seems to be a block problem...
So.. how can I render this template for every view??
Thank you
I think you are looking for the include tag. This way you can include your login html snippet in your base.html :
{% include "/registration/login.html" %}
What I really need was to create a templatetag in templatetags/mytags.py, where I define a function called get_login wich looks like this:
#register.inclusion_tag('registration/login.html', takes_context=True)
def get_login(context):
...
return {'formLogin': mark_safe(AuthenticationForm())}
...and in base.html:
{% load mytags %}{% get_login %}
The problem now is that the template (registration/login.html) doesnt recognize '{{ formLogin.username }}','{{ formLogin.password }}' and so on.
What am I missing?
Update 1:
mark_safe returns an instance of django.utils.safestring.SafeString, not a form.
Use (AuthenticationForm() instead of mark_safe(AuthenticationForm()) and it works!