django-crispy-forms have field and button on same row

2024/10/18 14:59:07

I am needing to have a bootstrap PrependedText field with a button on the same row. I can get it on the same row but it shows the button before the textbox and I want it after. What am I doing wrong and how do I get it to display after the box (on the right side of the text field)

serial = forms.CharField(forms.TextInput(attrs={'autocomplete':'off',}))serial.label = ''helper = FormHelper()helper.form_class = 'form-horizontal'helper.form_action = 'checkout'helper.form_method = 'post'helper.layout = Layout(HTML("<p class='alert-info'>Please scan the items barcode or enter the serial # in the field below.</p>"),Div(Div(PrependedText('serial', 'Serial #',css_class='input-xlarge'), css_class='span1'),Div(Submit('submit','Submit', css_class='btn-primary'), css_class='span1'),css_class='row-fluid'))
Answer

First of all, I noticed that in your definition of the serial field, you forgot to put widget= before forms.TextInput(. Also, your p should have the alert class too, not just alert-info.

I was able to kind of fix the problem by changing the first instance of span1 to span6, but I definitely don't recommend that because resizing the window causes layout issues and can even make it appear that there is no submit button at all!

I was going to recommend using PrependedAppendedText, but the template makes the assumption that you're appending text and not a button, so that won't work unless you override the template (e.g. PrependedAppendedText.template = 'custom_appended_prepended_text.html'). If you want to go that route, just copy the original template to your custom one, remove the span that the second instance of {{ crispy_appended_text|safe }} is inside of, and then use code like this:

Div(PrependedAppendedText('serial', 'Serial #', '<button class="btn btn-primary">Submit</button>', css_class='input-xlarge'), css_class='span1'),

I do have another suggestion, though. I like this solution much better, but it means giving up the prepended text in favor of placeholder text. First, in addition to setting the field's autocomplete attribute to off, set its class attribute to input-xlarge and its placeholder attribute to Serial #. Next, use this code:

FieldWithButtons('serial', StrictButton('Submit', type='submit', css_class='btn-primary')),

No need to create a custom template. Also, between the p you have at the top of the form and the field's placeholder text, I don't think your users will be confused by the lack of prepended text.

https://en.xdnf.cn/q/73148.html

Related Q&A

Uploading file in python flask

I am trying to incorporate uploading a basic text/csv file on my web app which runs flask to handle http requests. I tried to follow the baby example in flasks documentation running on localhost here. …

Diagonal stacking in numpy?

So numpy has some convenience functions for combining several arrays into one, e.g. hstack and vstack. Im wondering if theres something similar but for stacking the component arrays diagonally?Say I h…

Rules Engine in C or Python [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Draw arrows between 3 points

I am trying to draw arrows between three points in matplotlib.Lets assume we have 3 arbitrary points (A1,A2,A3) in 2d and we want to draw arrows from A1 to A2 and from A2 to A3.Some code to make it cle…

Make an animated wave with drawPolyline in PySide/PyQt

Im trying to animate a polyline (it have to act like a wave). Ive tried this way:from PySide.QtCore import * from PySide.QtGui import * import sys, timeclass Test(QMainWindow):def __init__(self, parent…

dateutil.tz package apparently missing when using Pandas?

My python 2.7 code is as follows:import pandas as pd from pandas import DataFrameDF_rando = DataFrame([1,2,3])...and then when I execute, I get a strange error regarding dateutil.tz./Library/Frameworks…

Importing SPSS dataset into Python

Is there any way to import SPSS dataset into Python, preferably NumPy recarray format? I have looked around but could not find any answer.Joon

Given a set of points defined in (X, Y, Z) coordinates, interpolate Z-value at arbitrary (X, Y)

Given a set of points in (X, Y, Z) coordinates that are points on a surface, I would like to be able to interpolate Z-values at arbitrary (X, Y) coordinates. Ive found some success using mlab.griddata …

Python multiprocessing speed

I wrote this bit of code to test out Pythons multiprocessing on my computer:from multiprocessing import Poolvar = range(5000000) def test_func(i):return i+1if __name__ == __main__:p = Pool()var = p.map…

Using os.forkpty() to create a pseudo-terminal to ssh to a remote server and communicate with it

Im trying to write a python script that can ssh into remote server and can execute simple commands like ls,cd from the python client. However, Im not able to read the output from the pseudo-terminal af…