how to create same name submit in flask-wtf? - flask

i want to create two same name submit in html like this
<input type="submit" name="key" value="up">
<input type="submit" name="key" value="down">
but i want to use flask-wtf to do it, i don't know how to create Class?is like this?
class NameForm(FlaskForm):
submit = SubmitField('up')
submit = SubmitField('down')

No. Doing it like that will simply overwrite the class attribute submit. Do it like this:
class NameForm(FlaskForm):
key = SubmitField('not_used_string')
Then in your html after return render_template('page.html', form=form) you render it like this:
{{ form.key(value='up', id="A1") }} # -> will render <input id="A1" name="key" type="submit" value="up">
{{ form.key(value='down', id="A2") }} # -> will render <input id="A2" name="key" type="submit" value="down">
You don't have to supply id's but if you don't they will both be key.
Note that in order to have the same name you can only have one class attribute with that name.

Related

Django formset - custom input HTML

I need to expand a formset's field, so instead of
{{ form.name }}
I'm using something like
<input type="text" name="{{ form.name }}" ... >
My custom implementation does not print the formset prefix, so the HTML I get is
<input type="text" name="name" ... >
But what I would need to have the form working properly with formset is
<input type="text" name="attachments-3-name" ... >
where attachments-x is automatically added.
How can I get that?
I noted there's an helper for ID (auto_id) which prints something similar: id_attachments-3-name; is there something similar for names?
Looks like {{ form.name.html_name }} is what I've been looking for the last 2 hrs.

Django passing parameters to views and templates

I got a navigation tree that tells me where i am on my website to build that navigation tree i always need to pass all variables from template to the view as <input type="hidden">. Then i need to pass it from the view to the next template and it goes on and on feels like a bad solution to pass the variables from every template to every view. so my question is if there is a better solution to my problem here is a screen of the navigation tree.
template:
<form action="{% url 'aktentabelle' %}" method="post" style="display:inline-block">
{% csrf_token %}
<input type="hidden" name="mitglied" value="{{Container.containernr}}" />
<input type="hidden" name="contpk" value="{{Container.pk}}" />
<input type="hidden" name="projectnr" value="{{projectnr}}" />
<input type="hidden" name="status" value="{{Container.status}}" />
<input type="hidden" name="chargepk" value="{{chargepk}}" />
<input type="hidden" name="chargenr" value="{{chargenr}}" />
<input class="btn btn-primary" type="submit" value="anzeigen" />
</form>
so in my templates i always need to pass alot of variables as hidden and in my view i need to convert them back to read them:
views.py:
def aktentabelle(request):
assert isinstance(request, HttpRequest)
container = request.POST['mitglied']
z = AkteForm
projectnr = request.POST['projectnr']
chargepk = request.POST['chargepk']
chargenr = request.POST['chargenr']
contpk = request.POST['contpk']
closecontainerform = CloseContainerForm
akte_list = Akte.objects.filter(container__containernr=container)
Anzahl_Akten =Akte.objects.filter(container__containernr=container).count
status = request.POST['status']
return render(
request,
'app/aktentabelle.html',
{
'title':'About',
'akte_list':akte_list,
'anzahl':Anzahl_Akten,
'container':container,
'aktenform':z,
'status':status,
'closecontainerform': closecontainerform,
'date':datetime.now().date,
'contpk':contpk,
'chargepk':chargepk,
'chargenr':chargenr,
'projectnr':projectnr,
}
)
as you can see i use so many lines to just pass all the variables from one template to a view and back to the template again just to build that navigation tree.
One way to do this would be to use filters. Something like below.
from django.template import Library
register = Library()
def get_fields(requested_key):
my_dict={
'title':'About',
'akte_list':akte_list,
'anzahl':Anzahl_Akten,
'container':container,
'aktenform':z,
'status':status,
'closecontainerform': closecontainerform,
'date':datetime.now().date,
'contpk':contpk,
'chargepk':chargepk,
'chargenr':chargenr,
'projectnr':projectnr,
}
return mydict.get("requested_key","")
register.filter('get_fields', get_fields)
Store this in template tags directory with a filename and in your template load this at the top using
{% load filename %}.
Then you can do something like below in your template.
{% load templatefilename %}
<form action="{% url 'aktentabelle' %}" method="post" style="display:inline-block">
{% csrf_token %}
<input type="hidden" name="mitglied" value="{{get_fields|containernr}}"
</form>

Display empty default field in Django form

I would like to initialize my form with a default empty value.
In my views.py i have one like so:
form = BookingForm(initial={'name':''})
and in the template like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value={{ form.name }}/></td></tr>
The output however, in the html input field shows '/' instead of the placeholder.
Any ideas how to do this?
Try adding quotes around your {{ form.name }} like so:
<tr><td><input class="input" type="text" name="name" id="id_name" placeholder="Enter your name" value="{{ form.name }}"/></td></tr>
and if that doesn't change anything you can try in your forms.py:
name = forms.CharField(initial='')
rather than trying to set the default input value upon instantiation.

Angular JS Form management

I am using Django, and would like to use Angular on my forms. Is there anyway to bind to form elements without having to explicitly write out each input element and add a ng-model?
I would like to be able to do this:
<form name="myForm" ng-submit="submitForm()">
<input type="text" name="username" />
<input type="text" name="password" />
</form>
And access the username/password in $scope as myForm.username and myForm.password, without having to do this:
<form name="myForm" ng-submit="submitForm()">
<input type="text" name="username" ng-model="myForm.username" />
<input type="text" name="password" ng-model="myForm.password" />
</form>
This would be useful when using Django's form builder, which automatically outputs forms based on the model they are based on, and saves having to write out and modify each form when changes are made.
I had to do this yesterday... I would do it like this:
forms.py
form_name = 'myForm' #or some other logic to get form class name
username = forms.CharField(widget=forms.TextInput(attrs={'ng-model': '%s.username' % form_name}))
or use https://github.com/jrief/django-angular
from django import forms
from djangular.forms.angular_model import NgModelFormMixin
class ContactForm(NgModelFormMixin, forms.Form):
subject = forms.CharField()
# more fields ...
This will output:
<input id="id_subject" type="text" name="subject" ng-model="subject" />

How can I access data sent in a post request in Django?

I have a form that is supposed to create a new 'Quote' record in Django. A 'Quote' requires a BookID for a foreign key.
This is my form
<form method="POST" action="{% url 'quotes:createQuote' %}">
{% csrf_token %}
<section>
<label for="q_text">Quote Text</label>
<input type="text" name="text" id="q_text" placeholder="Enter a Quote" style="padding-left:3px"> <br>
<label for="q_book">Book ID</label>
<input type="text" name="bookID" id="q_book" placeholder="Enter Book ID" style="padding-left:3px"> <br>
<label for="q_disp">Display Quote Now?</label>
<input type="radio" name="display" id="q_disp" value="True"> True
<input type="radio" name="display" value ="False">False <br>
<button value="submit">Submit</button>
</section>
</form>
And this is the method that it is targeting
def createQuote(request):
#b = get_object_or_404(Book, pk=request.bookID)
return HttpResponseRedirect(reverse('quotes:index'))
Somewhere in that request argument I assume there is some sort of field that contains the bookID the user will pass in on the form. How do I get at that information?
Bonus points for anyone who can tell me some way I can visualise data like I might with console.log(some.collection) in Javascript
if request.method == "POST":
book_id = request.POST['book_id']
Assuming you're sure it's in there. Otherwise you'll need to verify/provide a default value like you would for a normal python dictionary.
As for visualising the data, do you mean printing it to the console? In which case if you're running the django runserver you can just do print some_data. If you want it formatted a little nicer, you can use pretty print:
import pprint
pp = pprint.PrettyPrinter()
pp.pprint(some_data)