Django模型未保存到数据库

时间:2020-03-30 22:19:14

标签: python django django-models django-views django-database

我有一个非常简单的模型,其中仅包含四个字段:meeting_datemeeting_hourparticipant_nameparticipant_email。我正在向参与者发送有关所选数据(小时)的电子邮件,它可以正常工作。但是此时间表未写入数据库。这是我的代码:

views.py:

def index(request):
    context = {
        'schedules': Schedule.objects.all()
    }
    participant_name = request.POST.get('name')
    participant_email = request.POST.get('email')
    meeting_date = request.POST.get('date')
    meeting_hour = request.POST.get('hour')
    subject = 'Görüş'
    message = 'Hello ' + str(participant_name) + '. \nDate: ' + str(meeting_date) + ' hour ' + str(meeting_hour).
    from_email = settings.SERVER_EMAIL
    recipient_list = [participant_email]
    send_mail(subject, message, from_email, recipient_list)
    if request.POST.get('participant_email'):
        Schedule.objects.create(
            participant_name = request.POST.get('name'),
            participant_email = request.POST.get('email'),
            meeting_date = request.POST.get('date'),
            meeting_hour = request.POST.get('hour')
        )
    return render(request, 'index.html', context)

admin.py

class ScheduleAdmin(admin.ModelAdmin):
    list_display = ['participant_name', 'participant_email', 'is_scheduled', 'meeting_date', 'meeting_hour']

admin.site.register(Schedule, ScheduleAdmin)

models.py

class Schedule(models.Model):
    participiant_name = models.CharField(max_length=100)
    participiant_email = models.CharField(max_length=100)
    meeting_date = models.DateField()
    meeting_hour = models.TimeField()
    is_scheduled = models.BooleanField(default=False)

    def __str__(self):
        return self.meeting_date

html

<form action="{% url 'index' %}" method="POST">
                {% csrf_token %}
                <div class="form-group">
                    <label for="name">Name</label><br/>
                    <input type="text" name="name" id="name" placeholder="Name">
                </div>
                <div class="form-group">
                    <label for="email">Email</label><br/>
                    <input type="email" name="email" id="email" placeholder="Email"> 
                </div>
                <div class="form-group">
                    <label for="date">Date</label><br/>
                    <input type="date" name="date">
                </div>
                <div class="form-group">
                    <label for="hour">Saat</label><br/>
                    <select name="hour" id="hour">
                        <option value="1">Hour</option>
                        <option value="14:00">14:00</option>
                        <option value="15:00">15:00</option>
                        <option value="16:00">16:00</option>
                        <option value="17:00">17:00</option>
                        <option value="18:00">18:00</option>
                    </select>
                </div>
                <div class="form-group">
                    <button type="submit">Save</button>
                </div>
            </form>

1 个答案:

答案 0 :(得分:0)

request.POST.get('date')request.POST.get('hour')是字符串,因此您将像这样将它们转换回datehour

from datetime import datetime

converted_meeting_date = datetime.strptime(request.POST.get('date'), "%Y-%m-%d")
converted_meeting_hour = datetime.strptime(request.POST.get('hour'), "%H:%M")

请注意,日期和小时格式必须与POST数据中的格式保持一致,转换后,您应该能够创建Schedule对象:

 Schedule.objects.create(
            participant_name = request.POST.get('name'),
            participant_email = request.POST.get('email'),
            meeting_date = converted_meeting_date,
            meeting_hour = converted_meeting_hour 
        )