使用redirect_to后如何停止控制器的执行?(How do I stop controller execution after using redirect_to? (Using Rails))
我有一个具有多个操作的控制器,以:year和:month作为URL的属性。 我已经做了一个私人方法check_date来检查日期是否有效,并检查日期不在将来。
def check_date(year, month) if month < 1 || month > 12 || year < 2000 flash[:notice] = I18n.t 'archive.invalid_date' redirect_to :action => 'index' elsif year > Date.today.year || (year == Date.today.year && month > Date.today.month) flash[:notice] = I18n.t 'archive.no_future' redirect_to :action => 'month_index', :year => Date.today.year, :month => Date.today.month, :type => params[:type] end end
在redirect_to之后是否有一种导轨控制器执行的方式?
我可以想到的方法是在redirect_to之后抛出异常,或者从check_date返回一个值,并在调用它的每个动作中检查它 - 类似于
def month_index year = params[:year].to_i month = params[:month].to_i if !check_date(year, month) return ... end
但我想知道是否有一些很好的导轨方式来做到这一点。 我一半希望调用redirect_to rails可以识别我想停止,但是似乎并没有发生。
I have a controller with multiple actions that take :year and :month as attributes from the URL. I have made a private method check_date to check the date is valid and check the date is not in the future.
def check_date(year, month) if month < 1 || month > 12 || year < 2000 flash[:notice] = I18n.t 'archive.invalid_date' redirect_to :action => 'index' elsif year > Date.today.year || (year == Date.today.year && month > Date.today.month) flash[:notice] = I18n.t 'archive.no_future' redirect_to :action => 'month_index', :year => Date.today.year, :month => Date.today.month, :type => params[:type] end end
Is there a rails way of ending controller execution after the redirect_to?
Ways I can think of are either to throw an exception after the redirect_to or to return a value from check_date and check it in each action that calls it - something like
def month_index year = params[:year].to_i month = params[:month].to_i if !check_date(year, month) return ... end
But I wonder if there is some nice rails way of doing this. I was half hoping that having called redirect_to rails would recognise I wanted to stop, but that doesn't seem to happen.
原文:https://stackoverflow.com/questions/820781
最满意答案
改变这一行:
self.ends.replace(tzinfo=timezone.utc)
至
timezone.make_aware(self.ends, timezone.get_current_timezone())
解决了这个问题!
Changing this line:
self.ends.replace(tzinfo=timezone.utc)
to
timezone.make_aware(self.ends, timezone.get_current_timezone())
Solved the issue!
相关问答
更多-
您似乎错过了将天真日期时间对象分配回value datetime对象。 它应该是 value = value.replace(tzinfo=None) # assign 'naive' datetime object back to 'value' 而不仅仅是 value.replace(tzinfo=None) # will just return 'naive' datetime object 代码现在变成: import pytz import datetime .... for i, row i ...
-
Django在内部使用UTC时间。 TIME_ZONE将被用于“您的观点和模型”( https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TIME_ZONE ) 你从2016-01-11 00:00 CET开始,即2016-01-10 23:00 UTC ! 您的日期时间已正确保存到数据库并在以后进行了恢复,因此所有内容都按预期工作。 Django uses UTC time internally. TIME_ZONE will be ...
-
改变这一行: self.ends.replace(tzinfo=timezone.utc) 至 timezone.make_aware(self.ends, timezone.get_current_timezone()) 解决了这个问题! Changing this line: self.ends.replace(tzinfo=timezone.utc) to timezone.make_aware(self.ends, timezone.get_current_timezone()) Solve ...
-
RuntimeWarning:DateTimeField接收到一个天真的datetime(RuntimeWarning: DateTimeField received a naive datetime)[2023-08-21]
问题不在Django设置中,而是在传递给模型的日期 。 以下是时区感知对象如何: >>> from django.utils import timezone >>> timezone.now() datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=) 这是一个天真的对象: >>> from datetime import datetime >>> datetime.now() datetime.datetime(2013, 11, 2 ... -
数据库的结果取整为秒,因此15:49.21.93894 + 1.00变为15:49:21 + 02:00 这是完全错误的。 在将datetime对象转换为字符串now.strftime('%m.%d.%Y %H:%M:%S')您已经放弃了now.strftime('%m.%d.%Y %H:%M:%S')微秒的部分。 而且,+02 +02:00是日期时间对象的时区utc偏移量。 这不是一个圆整的秒或什么。 真正的问题在于你的strftime字符串格式。 今天,你的时区可能有夏令时。 但是,当您将错误的格式化日 ...
-
Django,DateTimeField(auto_now_add = True)不起作用(Django, DateTimeField(auto_now_add=True) not working)[2022-08-14]
如错误所示,您无法设置auto_now_add=True并同时指定默认值。 问题是Django需要知道数据库中现有条目的值。 您可以设置null=True ,然后该值将保留为None 。 created_time = models.DateTimeField('Created Time', auto_now_add=True, null=True) 或者,只需删除默认值,然后再次运行makemigrations。 created_time = models.DateTimeField('Created T ... -
Django - 查询(月/日)过滤器仅在USE_TZ = False时有效(Django - query (month/day) filter only works when USE_TZ = False)[2022-07-12]
正如Gocht在上述评论中提到的那样: 我的MySQL服务器时区需要在UTC时区。 此外,对于那些希望更改服务器时区的用户 - 请务必重新启动服务器,否则可能无法进行更改。 干杯 As Gocht helped mention in his comment above: My MySQL server timezone needed to be in UTC time zone. In addition, for those looking to change server time zone - be s ... -
升级到1.6.1或更高版本可以解决问题。 得到了django用户谷歌组的答案。 Upgrading to version 1.6.1 or later solves the problem. Got the answer from django users google group.
-
好的,实际上,因为我的数据库中的某个日期被手动插入为一个字符串,并且SQLite没有什么不同,所以使用Django ORM擦除数据库并重建它,解决了问题。 Ok, it's actually, because some date in my database were manually inserted as a string and SQLite didn't make the difference, wiping the db and rebuilding it using Django ORM sol ...
-
发生这种情况是因为您在数据forms.DateTimeField start_date声明为CharField ,您应该使用forms.DateTimeField 。 您的字段不是必填字段,因此允许用户将其留空。 当你在表单中发布数据时,这就是Django内部发生的事情: 表单字段尝试将原始数据转换为Python类型。 为此,首先检查是否可以在empty_values数组中找到原始数据(值为“空”的值列表,硬编码到字段的源代码中)。 如果它在empty_values找到值(在您的情况下就是这种情况),它将返 ...