I use WTForms FieldList field for lists of fields. It gets the values of these input fields sorted by their names in form.data. But I want to get the values in the order they appear in the form. Overriding process function is the only solution that I know of. Any ideas on simpler solutions?
So the HTTP spec says the following.
The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream.
So that takes care of part one. Part two is what can you do. WTForms will process anything in the form of a Multidict specifically it needs to have a getlist method.
The rest depends on what libs you are using. The Werkzeug project supplies an ImmutableOrderedMultiDict. implementation and there are several that can found be searching the PyPi repository or on github(https://github.com/gruns/orderedmultidict).
The last remaining piece is does your web server implementation preserve the forms ordering(i.e. is it strictly compliant with the W3C spec?) This part I don't know because I don't know what you are using.
Related
I have a little issue with Django's template not really rendering the time format as I'd like it to, following the documentations here proved to be a bit troublesome as it didn't work.
Original Code:
<p>Obtained this item on {{date_variable|date:"M j, Y"}} at {{time_variable|time}}</p>
Tried these two time filter formats as shown below which are given by the Django's documentation.
{{time_variable|time}}
{{time_variable|time:"TIME_FORMAT"}}
They represent the same thing as stated by the documentations. I even went as far as to try different formats i.e. {{time_variable|time:"h:i A"}}, {{time_variable|time:"P"}} etc. but none of it rendered in the web application.
One solution I miraculously found was by removing the time filter {{time_variable}} and it showed the time entered from the form except the AM/PM. However, I need to display the AM/PM, looking to try for other solutions.
Django version used is 2.2.2.
Note: I have tried other solutions that are on stackoverflow but it seemed none of them worked, I don't really know why either as the information is entered through Django's form template, not processed or changed.
It turns out that in the Django template (well at least for my project), the user inserts the time e.g.1:23 AM which is accepted and stored as a string due to AM so therefore it needs to be processed, that is, use the strptime() in python to convert the string back into a datetime object which can be then displayed using the following {{time_variable|time:'%h:%i A'}} or {{time_variable|time}} in the Django template.
I have some text fields in my Django model that are filled by a script, with values in English (the list of values is known).
But the app is actually made for Russian clients only. I'd like to translate those fields into Russian, and here comes a little question. These values are taken from an API response, which means I should check the value to translate it. What's faster: to check and translate fields in template or to make extra fields and translate strings in the Python script?
The problem is overhead of compiling Templates when rendering. So the more complicated the template gets (method calls etc), the performance tends to get slow (like py files are converted to pyc). Django has template caching but that also is limited (I don't know how much). I have faced performance issue because of lot of logic in templates. Plus its always good to have a dumb client (template). I will prefer the Python approach because of the idea to keep client thin and not because of the performance gap. Plus if tomorrow you need to add one more language then changing templates is always going to be difficult then server.
I'm creating a Django website for game developers to share code, which means I need to be able to save properly formatted code to a database, as well as being able to retrieve and display it.
Is there anything built into Django for this, or do I need to use a third party library?
I've played with a Django pagedown widget, which is modeled after StackOverflow's markdown editor, but I've had some trouble with it, and I'm not positive if that's is going to accomplish everything I want by itself.
Has anyone actually used the pagedown widget, implemented functionality similar to this, or know of another library that would work?
For storing the code, you can use any database that supports text types. That is, you would use a TextField in your model.
Now you need to decide how that raw text will be displayed/formatted.
This depends on what you mean by displaying properly formatted code, but I'm going to go ahead and just say you should check out Pygments. It is a syntax highlighting library that can be easily used w/Django.
Pygments works by your specifying a lexer (read: language) for it to apply styling to the target text. Whitespace will be preserved by your database. Everything else formatting-related is purely subjective. You'd presumably specify a fixed width font, a size, etc in the view and pass it to the context as you would any other DB-backed object.
Django Rest Framework's tutorial actually makes something very similar to what you are describing and uses Pygments, so I'd recommend checking that out for concrete usage examples.
For formatting code exclusively, I would avoid any markdown flavors like the one you listed, as code could contain characters that have special meaning in markdown, thus you would need to escape the text, and that defeats the purpose of markdown.
Introduction
In Django, when the data you want to display on a template is included in one object, It's f**** easy. To sum up the steps (that everyone knows actually):
You Write the right method to get your object in your model class
You Call this method in your view, passing the result to the template
You Iterate on the result in the template with a for loop, to display your objects in a table, for example.
Now, let's take a more complex situation
Let's say that the data you want to display is widely spread over different objects of different classes. You need to call many methods to get these data.
Once you call these different methods, you got different variables (unsimilar objects, integers, list of strings, etc.)
Nevertheless, you still want to pass everything to a template and display a pretty table in the end.
The problem is:
If you're passing all the raw objects containing the data you need to your template, it is completely unorganised and you can't iterate on variables in a clean way to get what you need to display your table.
The question is:
How (which structure) and where (models? views?) should I organize my complex data before passing it to a template?
My idea on this (which can be totally wrong):
For each view that need "spread data" to pass to a template, I could create a method (like viewXXX_organize_data()) in views.py, that would take the raws objects and would return a data structure with organized data that would help me to display a table by iterating on it.
About the data structure to choose, I compared lists with dictionaries
dictionaries have key so it's cleaner to call {{dict.a-key-name}} rather than {{ tabl.3}} in the template.
lists can be sorted, so when you need to sort by date the elements you want to display, dictionary is not helpful, arghh, stuck again!
What do you think about all that? Thanks for reading until there, and sharing on this!
With your question you are entering in a conceptual/architectural domain rather than in a "this particular view of the data in my project is hard to represent in the template layer of django". So I will try to give you the birds view (when flying and not on the ground) of the problem and you can decide for yourself.
From the first philosophy box in the django template language documentation it's clearly stated that templates should have as little program logic as possible. This indicates that the representation of the data used in the template should be simple and totally adapted to the template you are trying to build (this is my interpretation of it). This approach indicates that you should have a layer responsible for intermediating the representation of your data (models or other sources) and the data that your template needs to achieve the final representation you want you users to see.
This layer can simple stay in your view, in viewXXX_organize_data, or in some other form respecting to a more complex/elaborated architecture (see DCI or Hexagonal).
In your case I would start by doing something like viewXXX_organize_data() where I would use the most appropriate data structures for the template you are trying to build, while keeping some independence from the way you obtain your data (through models other services etc).
You can even think of not using you model objects directly in the template and creating template specific objects to represent a certain view of the data.
Hope this helps you make a decision. It's not a concrete answer but will help you for sure make a decision and then staying coherent all trough your app.
I need to store anonymous form data (string, checkbox, FileUpload,...) for a Conference registration site, but ATContentTypes seems to me a little bit oversized.
Is there a lightweight alternative to save the inputs -
SQL and PloneFormGen are not an option
I need to list, view and edit the data inputs in the backend...
Plone 3.3.6
python 2.4
Thanks
You could use souper
The description of the packages matches exact your requirement:
ZODB Storage for lots of (light weight) data.
There's a plone integration package plone.souper
There's an also an implementation example, see collective.pfg.soup
I guess this could fit your requirement.
I remember a talk at the ploneconf 2013, as example for the performance of souper, someone imported wikipedia articles: some slides
btw: Well, I'm not sure about Plone 3.x / Python 2.4 support.
Use uwosh.pfg.d2c (https://pypi.python.org/pypi/uwosh.pfg.d2c/)
It's an adapter for PloneFormGen (I know you said you don't want to use it, but keep reading). It transform your data in real Archetype content and you can enable an optiona that make it works with anonymous users.
And it will work on Plone 3.3. also.
Another approach is our unreleased collective.signupsheet (https://github.com/RedTurtle/collective.signupsheet) that is based on uwosh.pfg.d2c, but it's focused on event subscription. However we never released it (use at your own risk).
One approach is to create a browser view that accepts and retrieves JSON data and then just do all of the form handling in custom HTML. The JSON could be stored in an annotation against the site root, or you could create a simple content type with a single field for holding the JSON and create one per record. You'll need to produce your own list and item view templates, which would be easier with the item-per-JSON-record approach, but that's not a large task.
If you don't want to store it in the ZODB, then pick whatever file store you want - like shelf - and dump it there instead.