sqlite 我应该如何将html输入数据插入到Django数据库

5vf7fwbs  于 2022-11-15  发布在  SQLite
关注(0)|答案(2)|浏览(185)

我想不出该怎么解决这个问题。我正在尝试将一些来自html表单的数据插入到一个小的简单的Django数据库中;如果我是对的,则是SQLite。
我试着跟随教程,在网上做了很多搜索,但似乎我已经进入了教程的地狱。
我的问题是:如何实现将html文件上的文本输入字段中的数据放入Django数据库?
以下是我目前掌握的情况:
超文本标记语言:

<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>

那就是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')

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

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'),

]
因此发生的情况是:当我提交时,页面会刷新,并且不会添加数据。它被添加到主页,其中包含在views.py中:

def home(request):
context = {
    'posts' : Post.objects.all()
}
return render(request,'spotifylist/home.html', context)

谢谢蒂姆!注意到action="check"错误,尽管它没有解决我的问题!

ijnw1ujt

ijnw1ujt1#

# 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>
ncecgwcz

ncecgwcz2#

多亏了纳赛尔·法扎尔·汗,这个问题才得以解决。
我的html表格在主页上。
所以我只是简单地放弃了我的“检查”方法。

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)

到我的主视图方法:

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)

并且我在方法上添加了额外的一行

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

这就是当你盲目地遵循教程时会发生的事情!感谢大家所做的努力。

相关问题