2

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!

2
  • Maybe first use print() (and print(type(...)), print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called "print debuging" and it helps to see what code is really doing. Commented Sep 6, 2022 at 23:37
  • 1
    did you run it in console to see error messages in console? Maybe it has some problem and it display error. Commented Sep 6, 2022 at 23:40

2 Answers 2

3
# Model
    from django.db import models

    # Create your models here.
    class CoachDetailsModel(models.Model):

         coach_id=models.AutoField(primary_key=True)
         name=models.CharField(max_length=100,help_text="Enter FullName")
         email=models.EmailField(max_length=100,help_text="Enter Email id")
         contact=models.BigIntegerField(help_text="Enter Mobile Number" ,null=True)
         password=models.CharField(max_length=100,help_text="Enter Password")
         coach_status=models.CharField(max_length=100,default='pending',help_text="Enter Password")

         def __str__(self):
             return self.email

         class Meta:
             db_table="Coach_details"

# Views
    def coach_register(request):
      if request.method == "POST":
            name= request.POST.get('name')
            email = request.POST.get('email')
            contact = request.POST.get('contact')
            password = request.POST.get('password')

            CoachDetailsModel.objects.create(name=name,email=email,contact=contact,password=password)
      return render(request,'coach/coach-register.html')

      ### url
    path('coach-register',coachviews.coach_register,name='coach_register'),

# Html page
                                      <form method="POST" id="contactForm" name="contactForm" class="contactForm" enctype="multipart/form-data">
                                        {% csrf_token %}
                                        <div class="row">
                                        
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label class="label" for="subject">Enter UserName</label>
                                                    <input type="text" class="form-control" name="name" id="subject" placeholder="UserName">
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label class="label" for="subject">Enter Contact</label>
                                                    <input type="text" class="form-control" name="contact" id="subject" placeholder="Contact">
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label class="label" for="subject">EMAIL-ADDRESS</label>
                                                    <input type="text" class="form-control" name="email" id="subject" placeholder="Email">
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group-col-6">
                                                    <label class="label" for="subject">PASSWORD</label>
                                                    <input type="text" class="form-control" name="password" id="subject" placeholder="Password">
                                                </div>
                                            </div>
                                        
                                        
                                            <div class="col-md-12">
                                                <div class="form-group col-9">
                                                    <input type="submit" value="Register" class="btn btn-primary">
                                                    <div class="submitting"></div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
Sign up to request clarification or add additional context in comments.

1 Comment

in my case, i need "action="{% url 'yoururl' %}"" into form tag
0

It was solved thanks to Naser Fazal khan.

my html form was in on home page.

so i simply just moved from my 'check' method.

    if request.method == "POST":
    artiest= request.POST.get('artiest')
    song = request.POST.get('song')
    naam = request.POST.get('naam')
    link = request.POST.get('link')

    Post.objects.create(artiest=artiest,song=song,naam=naam,link=link)
return render(request,'spotifylist/home.html', context)

to my home views method:

def home(request):
context = {
    'posts' : Post.objects.all()
}
if request.method == "POST":
    artiest= request.POST.get('artiest')
    song = request.POST.get('song')
    naam = request.POST.get('naam')
    link = request.POST.get('link')

    Post.objects.create(artiest=artiest,song=song,naam=naam,link=link)
return render(request,'spotifylist/home.html', context)

and i've added an extra line on the method

Post.objects.create(artiest=artiest,song=song,naam=naam,link=link)

and this is what happens when you just blindly follow tutorials! thanks everyone for the effort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.