0

视图.py

settings=Settings.objects.get(user=2)
    if user.date_format == '0':
        date=datetime.datetime.strptime('%m/%d/%Y').strftime('%d/%m/%Y')
    else:
            date=datetime.datetime.strptime('%d/%m/%Y').strftime('%m/%d/%Y')  
    if request.method == 'POST':            
        reportform = ReportForm(instance=report,data=request.POST)
        if reportform.is_valid():
            report = reportform.save(commit=False) 
            report.user = request.user
            report.save()
            return redirect('/member/media/')

模型.py

class Settings(models.Model):
    user = models.ForeignKey(User, null=True)
    date_format = models.CharField('Date format', max_length=100)
    time_format = models.CharField('Time format', max_length=100)

class Report(models.Model):
    manual_date = models.DateField('Another date', null=True, blank=True)
    manual_time = models.TimeField('Another time', null=True, blank=True)

模板是

 {{ form.manual_date }}{{ form.manual_date.errors }}
                            </td>
                            <td>
                                {{ form.manual_time }}{{ form.manual_time.errors }}
                            </td>

要转换视图中的日期和时间格式,假设 date_format 值为“0”,则日期应为 dd/mm/yy 格式,如果值为“1”,则其格式为 (mm/dd/yyyy)。我知道错误在views.py的第二行,需要帮助解决。

4

1 回答 1

0
    settings=Settings.objects.get(user=2)
    if user.date_format == '0':
        date=datetime.datetime.strptime('%m/%d/%Y').strftime('%d/%m/%Y')
    else:
            date=datetime.datetime.strptime('%d/%m/%Y').strftime('%m/%d/%Y')

您可能想要使用settings.date_format而不是user.date_format.

于 2013-05-09T06:04:40.223 回答