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.
Related
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 have a written a short python script which takes a text and does a few things with it. For example it has a function which counts the words in the text and returns the number.
How can I run this script within django?
I want to take that text from the view (textfield or something) and return a result back to the view.
I want to use django only to give the script a webinterface. And it is only for me, maybe for a few people, not for a big audience. No deployment.
Edit: When I first thought the solution would be "Django", I asked for it explicitly. That was of course a mistake because of my ignorance of WSGI. Unfortunately nobody advised me of this mistake.
First off, is your heart really set on it being Django? If not I'd advise that Django, whilst an awesome framework, is a bit much for your needs. You don't really need full stack.
You might want to look at Flask instead, which is a Python micro-framework (and dead easy to use)
However, since you asked about Django...
You can create a custom Django command (docs here) that calls your script,
that can be called from a view as described in this question.
This has the added benefit of allowing you to run your script via the Django management.py script too. Which means you can keep any future scripts related to this project nice and uniform.
For getting the results of your script running, you can get them from the same bit of code that calls the command (the part described in the last link), or you can write large result sets to a file and process that file. Which you choose would really depend on the size of your result set and if you want to do anything else with it afterwards.
What nobody told me here, since I asked about Django:
What I really needed was a simple solution called WSGI. In order to make your python script accessible from the webbrowser you don't need Django, nor Flask. Much easier is a solution like Werkzeug or CherryPy.
After following the django tutorial, as suggested in a comment above, you'll want to create a view that has a text field and a submit button. On submission of the form, your view can run the script that you wrote (either imported from another file or copy and pasted; importing is probably preferable if it's complicated, but yours sounds like it's just a few lines), then return the number that you calculated. If you want to get really fancy, you could do this with some javascript and an ajax request, but if you're just starting, you should do it with a simple form first.
Does anyone has idea how to display django stream actions on templates.
Does I need to use Comet to fetch values to display on my template. Since, When I am doing action.send, it is storing verb and description in table actstream_action. But how should I display the values on template ?
P.S. Please note that this is first time I am using django-activity stream.
I've checked django-activity-stream, seems it has no magic for rendering activity updating dynamically.
Check django-socketio for websockets way. Or simply polling tech would satisfy your requirement if the expected access loading is not that high.
I'm not quite sure what you mean about "fetch actions directly to template using various templatetags" because template rendering is accomplished during request-response proceduret.
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.
EDIT: Figured it out. For whatever reason the field in the index was called strWord instead of wordStr. I didn't notice because of the similarities. The file was auto generated, so I must have called the field that in a previous development version.
I've got an app with around half a million 'records', each of which only stores three fields. I'd like to look up records by a string field with a query, but I'm running into problems. If I visit the console page, manually view a record and save it (without making changes) it shows up in a query:
SELECT * FROM wordEntry WHERE wordStr = 'SomeString'
If I don't do this, I get 'no results'. Does appengine need time to update? If so, how much?
(I was also having trouble batch deleting and modifying data, but I was able to break the problem up into smaller chunks.)
When this has happened to me it's because I've been using a TextField, which cannot be queried (but confusingly just gets ignored). Try switching to StringField.