I can't figure out how to fix this problem. I'm trying to insert some data from a html form into a small simple Django database; SQLite if I'm right.
I tried to follow tutorials and did allot of searching online but it seems like I've hit tutorial hell.
my question is: How can I achieve putting data from the text input field on the html file into the Django database?
Here's what I've got so far:
the HTML:
<h1>Create a Post </h1>
<form action="check" method="POST">
{% csrf_token %}
artiest: <input type="text" name="artiest"/><br/>
song: <br/>
<textarea cols="35" rows="8" name="song">
</textarea><br/>
<button type="submit" value="Post"/> </button>
</form>
the views.py
def check(request):
post=Post()
post.artiest= request.POST.get('artiest')
post.song= request.POST.get('song')
post.save()
return render(request, 'spotifylist/check.html')
the models.py
class Post(models.Model):
artiest = models.CharField(max_length=100)
song = models.CharField(max_length=100)
naam = models.CharField(max_length=100)
link = models.CharField(max_length=100)
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.artiest
the urls.py:
urlpatterns= [
re_path('^home/', views.home, name = 'spotifylist-home'),
re_path('help/', views.help, name = 'spotifylist-help'),
re_path('check/', views.check, name = 'spotifylist-check'),
]
so what happens is: when I submit the page refreshes and doesn't add the data. Which is added to the home page with in the views.py:
def home(request):
context = {
'posts' : Post.objects.all()
}
return render(request,'spotifylist/home.html', context)
Thanks Tim! for noting the action="check" error, though it didn't fix my problem!