У меня очень простая модель, содержащая всего четыре поля: meeting_date
, meeting_hour
, participant_name
и participant_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>