Django missing labels fromset - python

My Django learning has brought me to Forms. I've been able to create a simple form, using the information from the book I'm reading. I've also create a form based on the Model I have created. The issue I am having is that I am trying to create my own formatting within the template and for some reason the label information isn't held within the formset. I'm a little confused at how using the default way of displaying this i.e. {{ form }} has this information.
What I have;
adminforms.py
class NewsForm(ModelForm):
class Meta:
model = News_Article
exclude = ('news_datetime_submitted', 'news_yearmonth', )
labels = {
'news_title': _('Enter News Title'),
}
help_texts = {
'news_title': _('Enter a title to give a short description of what the news is.'),
}
error_messages = {
'news_title': {
'max_length': _("News title is too long."),
},
}
view.py
def create(request, dataset):
if dataset not in ['news', 'announcement']:
# change this to the siteadmin page if authenticated and have permissions, otherwise go to home
return HttpResponseRedirect(reverse('pages'))
rDict = {}
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
if dataset == "news":
form = NewsForm(request.POST)
elif dataset == "announcement":
form = AnnouncementForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/home/')
else:
pass
# if a GET (or any other method) we'll create a blank form
else:
announcement = get_announcement()
if not announcement == None:
rDict['announcement'] = announcement
if dataset == "news":
rDict['formset'] = NewsForm()
rDict['branding'] = {'heading': 'Create News Item', 'breadcrumb': 'Create News', 'dataset': 'create/' + dataset + '/'}
elif dataset == "announcement":
rDict['form'] = AnnouncementForm()
rDict['branding'] = {'heading': 'Create Announcement', 'breadcrumb': 'Create Announcement', 'dataset': 'create/' + dataset + '/'}
rDict['sitenav'] = clean_url(request.path, ['"', "'"])
rDict['menu'] = Menu.objects.all().order_by('menu_position')
pdb.set_trace()
return render(request, 'en/public/admin/admin_create.html', rDict)
template
<form action="/siteadmin/{{ branding.dataset }}" method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{{ field.label_tag }} {{ field }}
{% endfor %}
{% endfor %}
<input type="submit" value="Submit" />
</form>
For some reason jut get the form fields and no label information. N.b. The template text I've gottom from the Django documentation on formsets.
I've taken a look at the data thats returned and no label information is in it, yet it works with just {{ form }} ? Bit confused.
(Pdb) rDict['formset']
<NewsForm bound=False, valid=Unknown, fields=(news_title;news_text;news_active)>
Thanks in advance guys.
Wayne

You're adding a context variable named "formset" which is not a formset, it's a form: rDict['formset'] = NewsForm().
So, when this context variable is passed to the template, iterating with {% for form in formset %} has the misleading effect of creating a variable named form which is actually a form field. Try naming things properly (if you actually want a formset, create one as described here) and see if things start making sense.

Related

form.as_hidden doesn't pass values to POST

My form has initial values in it. I use form.as_hidden to hide the values and pass those values through a POST request. However, the hidden values are not passing through. Is there a way through this?
views.py
def car_detail_view(request, id):
if request.method == "POST":
form = CarForm(request.POST)
print(form.is_valid())
if form.is_valid():
car_save = form.instance
get_car = Car.objects.get(number_plate=car_save.number_plate)
get_car.available = False
get_car.save()
return redirect('/')
else:
print(form.errors)
else:
car = Car.objects.get(id=id)
form = CarForm(initial={'brand':car.brand, 'number_plate':car.number_plate, 'price':car.price,
'available':car.available})
args = {
'car':car,
'form':form
}
return render(request, 'map/confirmation.html', args)
confirmation.html
<h1>Confirmation of Booking</h1>
{% block content %}
<p>Brand: {{ car.brand }}</p>
<p>Number Plate: {{ car.number_plate }}</p>
<p>Price: {{ car.price }}</p>
<p> Are you sure you want to book? <p>
<form class="" method="post">
{% csrf_token %}
{{ form.as_hidden }}
<input type="submit" value="Book {{ car.brand }}">
</form>
{% endblock %}
Error
<ul class="errorlist"><li>brand<ul class="errorlist"><li>This field is required.</li></ul></li><li>number_plate<ul class="errorlist"><li>This field is required.</li></ul></li><li>price<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
Django doesn't have a form.as_hidden method. Therefore {{ form.as_hidden }} will render as the empty string '' in your template.
You can use the as_hidden method for individual form fields.
{{ form.number_plate.as_hidden }}
If you use values from hidden fields, you might need to add code to prevent the user altering the field values (e.g. with their browser's developer tools). However, in your case you don't need to get the values from the form, you can fetch them from the database.
def car_detail_view(request, id):
if request.method == "POST":
car = Car.objects.get(id=id)
car.available = False
car.save()
return redirect('/')
else:
car = Car.objects.get(id=id)
args = {
'car':car,
}
return render(request, 'map/confirmation.html', args)
Once you've got this working, you might want to think about what happens if two users try to book the same car at once.

Flask-Admin batch action with form

I have a Flask application with Flask-SQLAlchemy and Flask-Admin.
I would like to perform a batch action, but with form. For example I would like to set a same text value to the form attributed, but this value is entered by the user.
I saw documentation for batch actions, but it has example only for predefined data.
Is it possible, or maybe there's some workaround for this?
The way I achieve this is to do an internal POST in the #action method.
class AddressView(AdminView):
# ... ....
#action('merge', 'Merge', 'Are you sure you want to merge selected addresses?')
def action_merge(self, ids):
if len(ids) < 2:
flash("Two or more addresses need to be selected before merging")
return
return_url = request.referrer
return redirect(url_for('mergeaddresses.index', return_url=return_url), code=307)
Then define two routes, one for the internal post and then one to receive the submit POST from the form that receives the user's input data (MergeAddressForm in the case below).
In the example below I happen to be using a Flask-Admin BaseView to handle the routes. Note how the original checked IDs in the flask-admin list view are retrieved and then stored in the form as an encoded comma delimited list hidden field and likewise the return_url back to the flask-admin list view.
class MergeAddressesView(BaseView):
form_base_class = BaseForm
def __init__(self, name=None, category=None,
endpoint=None, url=None,
template='admin/index.html',
menu_class_name=None,
menu_icon_type=None,
menu_icon_value=None):
super(MergeAddressesView, self).__init__(name,
category,
endpoint,
url or '/admin',
'static',
menu_class_name=menu_class_name,
menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value)
self._template = template
def is_visible(self):
return False
#expose('/', methods=['POST'])
def index(self):
if request.method == 'POST':
# get the original checked id's
ids = request.form.getlist('rowid')
merge_form = MergeAddressForm()
merge_form.process()
joined_ids = ','.join(ids)
encoded = base64.b64encode(joined_ids)
merge_form.ids.data = encoded
_return_url = request.args['return_url']
merge_form.return_url.data = _return_url
self._template_args['form'] = merge_form
self._template_args['cancel_url'] = _return_url
return self.render(self._template)
#expose('/merge', methods=['POST'])
def merge(self):
if request.method == 'POST':
merge_form = MergeAddressForm(selection_choices=[])
decoded = base64.b64decode(merge_form.ids.data)
ids = decoded.split(',')
# do the merging
address_merger = AddressMerger(ids=ids, primary_id=merge_form.primary_address.data)
address_merger.merge()
# return to the original flask-admin list view
return redirect(merge_form.return_url.data)
My template for the user input form looks something like below. Note the action url.
{% extends 'admin/base.html' %}
{% import "bootstrap/wtf.html" as wtf %}
{% block body %}
<h3>{{ admin_view.name|title }}</h3>
<form role="form" action="{{ url_for('mergeaddresses.merge') }}" method="post" name="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form) }}
{{ wtf.form_field(form.primary_address) }}
<button type="submit" class="btn btn-primary">Merge</button>
Cancel
</form>
{% endblock %}

Django form is not valid without showing any errors

The form, that is described below, is not valid. And expression {{ search_id_form.errors }} doesn't show anything.
Django template:
<form method="POST">
{% csrf_token %}
{{ search_id_form.idtype.label_tag }}
{{ search_id_form.idtype }}
{{ search_id_form.index.label_tag }}
{{ search_id_form.index }}<br>
<input type="submit" name="id_search_button" value="Submit">
</form>
Python class:
class IDSearchForm(forms.Form):
idtype = forms.ChoiceField(
choices=[('idx', 'Our Database ID'), ('uprot', 'UniProt'), ('ncbi', 'NCBI')],
initial='idx',
widget=forms.RadioSelect,
label="Which identifier to use:"
)
index = forms.CharField(label="Identifier:")
View:
def search(request):
if request.method == 'POST':
# handling other forms ...
# find a toxin by id
if 'id_search_button' in request.POST:
search_id_form = IDSearchForm()
if search_id_form.is_valid():
idtype = search_id_form.cleaned_data['idtype']
index = search_id_form.cleaned_data['index']
return render(request, 'ctxdb/result_ctx.html', {
# here I try to use predefined object to pass to renderer (for debugging)
'ctx': get_object_or_404(CTX, idx='21')
})
# handling other forms ...
# other forms
search_id_form = IDSearchForm()
# other forms
return render(request, 'ctxdb/search.html', {
# other forms
'search_id_form': search_id_form,
# other forms
})
In the view function I handle four different forms on single page. Other forms work correctly. What is the problem here?
When calling .is_valid, you need to pass the data to search_id_form which you are not doing.
Change
search_id_form = IDSearchForm()
to
search_id_form = IDSearchForm(request.POST)

Validate Django forms for user specified dynamically generated fields

How do I perform a Django form validation for an unknown number of fields specified by the user? In my case, form allows a user to create a music album with any number of tracks and associate it to an artist. The view asks for how many tracks there are and then generates a form with that many input fields.
Form:
class NumberOfTracks(forms.Form):
track_no = forms.IntegerField()
class CustomAlbumAdmin(forms.Form):
artist = forms.CharField(max_length=150)
album = forms.CharField(max_length=150)
track_no = forms.IntegerField()
track = forms.CharField(max_length=150)
View:
def album_admin(request):
if request.GET.get('track_no'):
number_of_tracks = request.GET.get('track_no')
artists = Artist.objects.all()
return render(request, 'customadmin/album_admin1.html', {
'number_of_tracks': number_of_tracks,
'tracks': range(1, int(number_of_tracks) + 1),
'artists': artists,
})
elif request.method == 'POST':
form = CustomAlbumAdmin(request.POST)
print form
artist = request.POST['artist']
album = request.POST['album']
all_tracks = request.POST.getlist('track')
create_album = CreateAlbum(artist=artist, album=album, tracks=all_tracks)
create_album.save_album()
create_album.save_tracks()
form = NumberOfTracks()
return render(request, 'customadmin/no_tracks1.html', {
'form': form,
})
else:
form = NumberOfTracks()
return render(request, 'customadmin/no_tracks1.html', {
'form': form,
})
(Just so it's clear, I used if form.is_valid() and form.cleaned_data but to get this to work thus far, I've had to bypass that in favor of getting the raw POST data)
Part of what's confusing me is that I've customized my form template to add a number input fields with name="track" depending on user input (EX: create an album with 13 tracks). When I go into my view to print form = CustomAlbumAdmin(request.POST) it gives a very simple table based on my form: one artist, one album, one track, and one track_no so validating against this will of course return False unless I have an album with just one track.
Here's the template:
{% extends 'base.html' %}
{% block content %}
<form action="/customadmin/album1/" method="POST">{% csrf_token %}
<select name="artist">
{% for entry in artists %}
<option value="{{ entry.name }}">{{ entry.name }}</option>
{% endfor %}
</select>
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.album.errors }}
<label for="id_album">Album:</label>
<input id="id_album" maxlength="150" name="album" type="text" />
</div>
<div class="fieldWrapper">
{{ form.track.errors }}
<input type="hidden" name="number_of_tracks" value="{{ number_of_tracks }}">
{% for e in tracks %}
<label for="id_track">Track No. {{ forloop.counter }}</label>
<input id="id_track_{{ forloop.counter }}" maxlength="150" name="track" type="text" /></br>
{% endfor %}
</div>
<p><input type="submit" value="Save album" /></p>
</form>
{% endblock %}
The one way I was thinking of approaching this was to create a custom clean_track method that takes a list of all the tracks entered as I've done in the view with all_tracks = request.POST.getlist('track') but not sure how to do that.
A related question I have is if I can customize validation based on POST data. The first way I approached this was to generate incremented inputs with name="track_1", name="track_2", etc.,. and then trying to validate based on that. However, I wouldn't be able to use request.POST.getlist('track') in that case.
It might be a better approach to use formsets instead.
class AlbumForm(forms.Form):
artist = forms.CharField(max_length=150)
name = forms.CharField(max_length=150)
class TrackForm(forms.Form):
track_no = forms.IntegerField()
name = forms.CharField(max_length=150)
# In view
from django.forms.formsets import formset_factory
TrackFormSet = formset_factory(TrackForm)
if request.method == 'POST':
track_formset = TrackFormSet(request.POST)
album_form = AlbumForm(request.POST)
if track_formset.is_valid() and album_form.is_valid():
save_album(album_form, track_formset)
else:
track_formset = TrackFormSet()
album_form = AlbumForm()
And in save_album you can just iterate through track_formset.ordered_forms to get each form in the formset:
for form in track_formset.ordered_forms:
data = form.cleaned_data
# Do what you want with the data
This can be even more powerful if you use model formsets because you can set a foreign key in the track model that points to the album model, and Django can save them automatically for you.

Django forms.MultipleChoiceField only selects one value

I have a form with only one field which is a MultipleChoiceField. In the template it is being printed with two other forms that are ModelForms inside the same HTML form (as described here).
When reading all of the POST data in the view, everything is there and working correctly except the values from this MultipleChoiceField, which is shown only the last selected value from the form if selecting it straight from request.POST['field'] -- but interesting enough, if I print request.POST, everything selected is there. How is this possible? This is really puzzling my mind.
This is the form:
class EstadosAtendidosForm(forms.Form):
estadosSelecionados = forms.MultipleChoiceField(choices = choices.UF.list)
This is the view:
#login_required
#csrf_protect
def cadastro_transportadora(request):
if request.method == 'POST':
print request.POST
print len(request.POST['estadosSelecionados'])
print request.POST
estadosSelecionados = request.POST['estadosSelecionados']
for estado in estadosSelecionados:
print estado
form_end = EnderecoForm(request.POST)
form_transp = TransportadoraForm(request.POST)
else:
transportadora_form = TransportadoraForm()
endereco_form = EnderecoForm()
estados_form = EstadosAtendidosForm()
return render(request, 'transporte/transportadora/cadastro.html', {'transportadora_form': transportadora_form, 'endereco_form': endereco_form, 'estados_form': estados_form})
And this is the template:
{% extends "transporte/base.html" %}
{% block main %}
<h1>Cadastro de Transportadora</h1>
<form enctype="multipart/form-data" action="" method="POST">
{% csrf_token %}
<h4>Dados da transportadora</h4>
{{ transportadora_form.as_p }}
<h4>Endereço</h4>
{{ endereco_form.as_p }}
<h4>Estados atendidos</h4>
{{ estados_form.as_p }}
<input type="submit" />
</form>
{% endblock %}
The output from the prints in the view, since line 5 to 10, is as follows:
<QueryDict: {u'nome': [u'Test name'], u'bairro': [u'Estrela'], u'logradouro': [u'R. SHudhsu'], u'numero': [u'234'], u'estadosSelecionados': [u'AM', u'RJ', u'SP'], u'telefone': [u'+559965321232'], u'cep': [u'88088888'], u'csrfmiddlewaretoken': [u'mQhxZlbosISw4acZOmTWw6FpaQPwg2lJ'], u'estado': [u'AM'], u'email': [u'test#email.com']}>
2
<QueryDict: {u'nome': [u'Test name'], u'bairro': [u'Estrela'], u'logradouro': [u'R. SHudhsu'], u'numero': [u'234'], u'estadosSelecionados': [u'AM', u'RJ', u'SP'], u'telefone': [u'+559965321232'], u'cep': [u'88088888'], u'csrfmiddlewaretoken': [u'mQhxZlbosISw4acZOmTWw6FpaQPwg2lJ'], u'estado': [u'AM'], u'email': [u'test#email.com']}>
S
P
See that the variable estadosSelecionados really contains the 3 values that I selected from the form, correctly, as a list, when I print the whole request.POST data, but when I print just request.POST['estadosSelecionados'], it doesn't.
Why?
POST is a QueryDict object, which has special behavior when multiple values are submitted in the HTTP POST for the same key. To get all of them, use the getlist method. Alternatively, just use your form - the form field will collect the multiple values for you.
You shouldn't be looking in request.POST. The whole point of using a form is that it takes care of things like type conversion. Look in form.cleaned_data['estadosSelecionados'] after checking form.is_valid().

Categories