Reputation: 2625
I want to change my datetime field but current_user_moneybooks has many objects.
so i want to know is there any method to change the format of datetime field.
I tried link:
for current_user_moneybook in current_user_moneybooks
def start_date(self):
return self.start_date.strftime("%Y-%m-%d")
def end_date(self):
return self.end_date.strftime("%Y-%m-%d")
or
def all_moneybooks(request):
current_user = request.user
if current_user.is_authenticated:
current_user_moneybooks = current_user.owner.all
start_date.strftime("%Y-%m-%d") for current_user_moneybook in current_user_moneybooks
end_date.strftime("%Y-%m-%d") for current_user_moneybook in current_user_moneybooks
return render(request, "home.html", context={"current_user": current_user, "all_moneybooks": all_moneybooks, "current_user_moneybooks": current_user_moneybooks})
else:
return render(request, "home.html", context={"current_user": current_user, "all_moneybooks": all_moneybooks})
and result :
start_date.strftime("%Y-%m-%d") for current_user_moneybook in current_user_moneybooks ^ SyntaxError: invalid syntax
Upvotes: 0
Views: 39
Reputation: 542
First of all, the syntax error you are getting is because of the wrong implementation of list comprehension.
Your iterator has to be within a list and also you need to use the iterator to modify it's attributes.
This
start_date.strftime("%Y-%m-%d") for current_user_moneybook in current_user_moneybooks
has to be like this
modified_dates = [current_user_moneybook.start_date.strftime("%Y-%m-%d") for current_user_moneybook in current_user_moneybooks]
"all_moneybooks" is the definition name and am not sure why you are sending that to the template.
If all you want to do is change the presentation of the date format in the template, you can do that using the django's builtin template filter like so
{{ some_date|date:’D, d M, Y' }} -> Thu, 03 Dec, 2015
Upvotes: 1