Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Why isn't my Django form sending data to the database?

$
0
0

I'm new to Django and Python. I created a blog for my project but when trying to use a form to add blog posts to the database I can't figure out how to make the send button to actually update the model in the database. I managed to make it show in the view but the send button is the problem.

This is the model:

class BlogPost(models.Model):    title = models.CharField(max_length=250)    content = models.TextField()    post_author = models.CharField(max_length=30)    creation_date = models.DateTimeField(auto_now_add=True)    modification_date = models.DateTimeField(auto_now=True)    image = models.ImageField(upload_to='blog/files/blog_images')    categories = models.ManyToManyField('Category', related_name='posts') #Relaciona el atributo con la clase Category y permite usar category.posts    def __str__(self):        return self.title    class Meta:        verbose_name = "blog post"        verbose_name_plural = "blog posts"

This is the form:

class newPostForm(forms.Form):    title = forms.CharField(max_length=250)    content = forms.CharField(        widget=forms.Textarea()    )    image = forms.ImageField()    post_author = forms.CharField()

This is the view:

def new_post(request):    if request.method == "POST":        form = newPostForm(request.POST)        if form.is_valid():            info = form.cleaned_data()            new_post_info = BlogPost(                title=info["title"],                content=info["content"],                image=info["image"],                post_author=info["post_author"],                )            new_post_info.save()                return render(request, "")    else:        form = newPostForm()    return render(request, "blog/new_post.html", {"form":form})

I don't know if you need any more information, let me know. Thanks in advance!


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>