Im trying to display the month name of the datetime object on a django template. This is how Im sending the date:
{
..
'today': timezone.localtime(timezone.now())
..
}
On the template Im doing this, I have tried what this question has: (Django) how to get month name?
But still I'm not getting the desired result, maybe there's a difference with timezone.locatime() vs datetime.now()?:
{{ today|date:"F" }}
However Im only getting and empty blank space where whe month name should be. Can anyone help me correct my error?
Related
I am trying to display time in 10:34 PM format but MySQL server has it in 22:34:00 format. I tried to do {{ invoice['time'].strftime("%I:%M %p") }} but I am getting error saying jinja2.exceptions.UndefinedError: 'datetime.timedelta object' has no attribute 'strftime'
I also tried to convert it to string then just get the first 5 character hoping to give at least 22:34 format but it also failed. {{ invoice['time']|string[:5] }}
How to achieve this? The MySQL stores it in time and not datetime.
Im using crispy field on inputs for css speed.
I have my {{from.username|as_crispy_field}}.
When i submit the data i get a CrispyError at /client error.
Exception Type: CrispyError
Exception Value:
|as_crispy_field got passed an invalid or inexistent field
What do i need to do to handle this in views ?
it is supposed to be {{form.username|as_crispy_field}} instead of {{from.username|as_crispy_field}}.
PS. This is about 3 years old but I hope the answer will help someone at some point. Cheers!
I want to get the current date for a specific timezone in my Django app, irrespective of the server's timezone. I save the user's timezone in the database. I'll then use that in the following function:
def current_date(zone):
utc = timezone.now()
tz = pytz.timezone(zone)
return utc.astimezone(tz).date()
print(current_date('Pacific/Auckland')) #prints 2016-05-30
print(current_date('Africa/Accra')) #prints 2016-05-29
It seems to work, but working with timezones seems complex and I'm wondering if something can go wrong with this approach?
It looks fine as long as getting to the value in view itself is what you want. Since the date/time print is an aspect of presentation you probably may not want to do it in the view code rather in the template using something like below:
{% load tz %}
{{ object.datetime_field|timezone:request.user.timezone }}
assuming you are storing the user's timezone selection in the user model.
I'm trying to edit and save changes from my form to PostgreSQL database. I'm able to get the correct ID and can view everything fine. When I try and save it to the database it never saves, the form just does a quick refresh. If I use the shell I can update without any issues.
from turnover.models import *
t = Detail.models.get(id=1)
t.shift = 'Swing'
t.save()
Here is the link to the django code I'm currently working with. it's just snippits to give an ovewview of where I'm having problems.
Python Code: https://gist.github.com/1363921
HTML Update Template: https://gist.github.com/1363940
EDIT:
Issue was resolved, I had a field that was required that I was not filling out, thanks for the help from jro on using {{ form.errors }}
I'm having problems with passing model object values through a URL pattern. The URL:
url(r'^cities/(?P<city>\w+)/$', 'city_firm', name='city_firm'),
In the template (from the index page) I have:
{{ city }}
This is in a for loop.
The related view is:
def city_firm(request, city):
city1 = Cities.objects.get(city=city)
cityf = city1.Firms.all()
return render_to_response('cityfirm.html', {'cityf': cityf})
The two models (Cities, Firms) are in a many to many relationship.
I keep getting TemplateSyntaxError at index (NoReverseMatch while rendering: Reverse for 'city_firm' with arguments '(<Cities: >,)' and keyword arguments '{}' not found). In the template link tag I tried: {% url city_firm city=city %}, {% url city_firm city=cities.city %}. Nothing changed. The urlconf part seems right. The problem seems to be in the template. Maybe there is an issue with the string values of the object as they aren't in English. But I took several precautions to prevent this. There is maybe something wrong with the view but the error says template. Any ideas?
Solution:
Thanks everyone! Finally I figured it out. The problem was simple: I was trying to send object attribute names through the url, that had non-English characters and spaces. To fix it, I had to edit my models.
The issue is that you can't pass an object in a URL, you can only pass characters. So you need to put the part of the city object that contains the text you want to be in the URL - in your case, it appears to be an attribute also called city, which is what you use to in the lookup to get the object in the view. So it should be:
{{ city }}
I don't think name means what you think it does - remove that and read this: http://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns
As far as the error... the NoReverseMatch is telling you that it's not seeing any arguments. Remember that nonexisting template variables expand to "". Make sure city is in context when you're running that code - maybe post the for in the template?