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:
JavaScript
x
10
10
1
<h1>Create a Post </h1>
2
<form action="check" method="POST">
3
{% csrf_token %}
4
artiest: <input type="text" name="artiest"/><br/>
5
song: <br/>
6
<textarea cols="35" rows="8" name="song">
7
</textarea><br/>
8
<button type="submit" value="Post"/> </button>
9
</form>
10
the views.py
JavaScript
1
9
1
def check(request):
2
3
post=Post()
4
post.artiest= request.POST.get('artiest')
5
post.song= request.POST.get('song')
6
post.save()
7
8
return render(request, 'spotifylist/check.html')
9
the models.py
JavaScript
1
10
10
1
class Post(models.Model):
2
artiest = models.CharField(max_length=100)
3
song = models.CharField(max_length=100)
4
naam = models.CharField(max_length=100)
5
link = models.CharField(max_length=100)
6
date_posted = models.DateTimeField(auto_now_add=True)
7
8
def __str__(self):
9
return self.artiest
10
the urls.py:
JavaScript
1
5
1
urlpatterns= [
2
re_path('^home/', views.home, name = 'spotifylist-home'),
3
re_path('help/', views.help, name = 'spotifylist-help'),
4
re_path('check/', views.check, name = 'spotifylist-check'),
5
]
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:
JavaScript
1
6
1
def home(request):
2
context = {
3
'posts' : Post.objects.all()
4
}
5
return render(request,'spotifylist/home.html', context)
6
Thanks Tim! for noting the action="check"
error, though it didn’t fix my problem!
Advertisement
Answer
JavaScript
1
73
73
1
# Model
2
from django.db import models
3
4
# Create your models here.
5
class CoachDetailsModel(models.Model):
6
7
coach_id=models.AutoField(primary_key=True)
8
name=models.CharField(max_length=100,help_text="Enter FullName")
9
email=models.EmailField(max_length=100,help_text="Enter Email id")
10
contact=models.BigIntegerField(help_text="Enter Mobile Number" ,null=True)
11
password=models.CharField(max_length=100,help_text="Enter Password")
12
coach_status=models.CharField(max_length=100,default='pending',help_text="Enter Password")
13
14
def __str__(self):
15
return self.email
16
17
class Meta:
18
db_table="Coach_details"
19
20
# Views
21
def coach_register(request):
22
if request.method == "POST":
23
name= request.POST.get('name')
24
email = request.POST.get('email')
25
contact = request.POST.get('contact')
26
password = request.POST.get('password')
27
28
CoachDetailsModel.objects.create(name=name,email=email,contact=contact,password=password)
29
return render(request,'coach/coach-register.html')
30
31
### url
32
path('coach-register',coachviews.coach_register,name='coach_register'),
33
34
# Html page
35
<form method="POST" id="contactForm" name="contactForm" class="contactForm" enctype="multipart/form-data">
36
{% csrf_token %}
37
<div class="row">
38
39
<div class="col-md-6">
40
<div class="form-group">
41
<label class="label" for="subject">Enter UserName</label>
42
<input type="text" class="form-control" name="name" id="subject" placeholder="UserName">
43
</div>
44
</div>
45
<div class="col-md-6">
46
<div class="form-group">
47
<label class="label" for="subject">Enter Contact</label>
48
<input type="text" class="form-control" name="contact" id="subject" placeholder="Contact">
49
</div>
50
</div>
51
<div class="col-md-6">
52
<div class="form-group">
53
<label class="label" for="subject">EMAIL-ADDRESS</label>
54
<input type="text" class="form-control" name="email" id="subject" placeholder="Email">
55
</div>
56
</div>
57
<div class="col-md-6">
58
<div class="form-group-col-6">
59
<label class="label" for="subject">PASSWORD</label>
60
<input type="text" class="form-control" name="password" id="subject" placeholder="Password">
61
</div>
62
</div>
63
64
65
<div class="col-md-12">
66
<div class="form-group col-9">
67
<input type="submit" value="Register" class="btn btn-primary">
68
<div class="submitting"></div>
69
</div>
70
</div>
71
</div>
72
</form>
73