from django.db import models
from django.utils import timezone
class Note(models.Model):
title = models.CharField(max_length = 50)
body = models.TextField()
image = models.ImageField(null=True, blank=True, upload_to="images/")
is_active = models.BooleanField(default=True)
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return "%s" % self.title
class Meta:
verbose_name = 'Запись'
verbose_name_plural = 'Записи'
from django import forms
from .models import Note
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('title', 'body', 'image')
def admin(request ):
form = NoteForm
if request.method == "POST":
form = NoteForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
#post.author = request.user
post.published_date = timezone.now()
post.save()
#return redirect('blog', pk=note.pk)
else:
form = NoteForm()
return render(request, 'admin.html', locals())
<form method="POST " enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class=" btn btn-default">Save</button>
</form>