'User'对象没有属性'date_format'

时间:2013-05-09 06:02:21

标签: django django-models django-views

views.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/')

models.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的第二行,需要帮助解决。

1 个答案:

答案 0 :(得分: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