Reputation: 292
Good day.
I'm trying to create a object based on form input, i tesed out the data, everything is provided but, for some reason the form is not validated.
I've also tried overriding form_valid(self,form)
but the problem with that method was django never went to it as if it didn't exist.
forms.py
class CreatePostForm(forms.ModelForm):
class Meta:
model = Post
fields = '__all__'
views.py
class CreatePost(CreateView):
form_class = CreatePostForm
template_name = 'dashboard/add-product.html'
# success_url = redirect('user_posts:post_detail')
def post(self, request, *args, **kwargs):
form = CreatePostForm(request.POST)
if self.form_valid(form):
post = form.save(commit=False)
post.user = request.user
post.save()
return redirect('user_posts:post_detail', args=post.slug)
print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
code in the template is basic form, not gona import it.
data that has been passed via request.POST
user
'b8a3b0b3-0eef-48ed-b257-a6f9bfdd5cda'
title
'theetitle'
main_description
'agdgdfg'
slug
''
main_image
'Bucee_Lee_smile.jpg'
subtitle1
''
sub_description1
''
sub_image1
''
subtitle2
''
sub_description2
''
sub_image2
''
subtitle3
''
sub_description3
''
sub_image3
''
total_likes
''
traceback points to this line
if self.form_valid(form):
model
class Post(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200, unique=True)
main_description = models.TextField()
slug = models.SlugField(db_index=True, blank=True)
main_image = models.ImageField(upload_to=upload_image)
subtitle1 = models.CharField(max_length=200, blank=True, null=True)
sub_description1 = models.TextField(blank=True, null=True)
sub_image1 = models.ImageField(upload_to=upload_image,
blank=True, null=True)
subtitle2 = models.CharField(max_length=200, blank=True, null=True)
sub_description2 = models.TextField(blank=True, null=True)
sub_image2 = models.ImageField(upload_to=upload_image,
blank=True, null=True)
subtitle3 = models.CharField(max_length=200, blank=True, null=True)
sub_description3 = models.TextField(blank=True, null=True)
sub_image3 = models.ImageField(upload_to=upload_image,
blank=True, null=True)
posts_liked = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='posts_liked',
blank=True, null=True)
total_likes = models.PositiveIntegerField(blank=True, null=True, db_index=True)
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
objects = PostManager()
# TODO: get_absolute_url()
def get_absolute_url(self):
return reverse('user_posts:post_detail', args=[self.slug])
def __str__(self):
return self.title
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
if not self.slug:
self.slug = slugify(self.title)
super(Post, self).save(force_insert, force_update, using, update_fields)
class Meta:
verbose_name_plural = 'Posts'
Upvotes: 1
Views: 236
Reputation: 3392
try the following way
class CreatePost(CreateView):
form_class = CreatePostForm
template_name = 'dashboard/add-product.html'
success_url = ('url_name')
def form_valid(self, form):
valid_data = super(CreatePost, self).form_valid(form)
form.instance.user = self.request.user
return valid_data
Upvotes: 2