繁体   English   中英

如何配置Bootstrap3表单以与Django表单一起使用

[英]How to configure Bootstrap3 form for use with Django form

我之前曾发布过类似的问题,但是答案并没有导致我找到解决方案。 我认为解决方案现在就在我的模板之内,许多答案似乎都没有解决。

无论如何,我相信views.py,forms.py和models.py是正确的,而且我的问题似乎是在Bootstrap3模态中调用表单,而我不确定如何执行此操作。

问题是填写表单时不会发生验证,也不会将电子邮件发送到数据库。 但是,在表单提交时,您将被发送到thanyou页面,因此至少我知道这可以正常工作...

任何方向都会有所帮助。

index.html

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Sign up for the Beta</h4>
            </div>

            <div class="modal-body">

                <form class="form-horizontal well" data-async data-target="#myModal" action="thanks/" method="POST">
                    {% csrf_token %}

                    <fieldset>
                        <div>Your email address:
                            <span>

                                <input type="text" id="modalInput"></input>

                            </span>
                        </div>
                    </fieldset>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
</div>

views.py

from django.shortcuts import render
from temp.models import Email
from temp.forms import EmailForm

def index(request):
    if request.method == 'POST':
        # POST, get the data from the form
        form = EmailForm(request.POST)
        if form.is_valid():
            # if is valid, just save, and return a new page with thanks
            form.save()
            return render(request, 'temp/thanks.html')
        else:
            # Form has errors, re-render with data and error messages
            return render(request, 'temp/index.html', {'form': form,})
    else:
        # GET, render new empty form
        form = EmailForm()
        return render(request, 'temp/index.html', {'form': form,})

def thanks(request):
    return render(request, 'temp/thanks.html')

表格

from django.forms import ModelForm
from temp.models import Email

class EmailForm(ModelForm):
    class Meta:
        model = Email

models.py

from django.db import models

class Email(models.Model):
    email = models.EmailField(max_length=200)

    def __unicode__(self):  # Python 3: def __str__(self):
        return self.email

编辑:我没有看到您意见的最后2行。 您将表格发送给thanks/ ,因此直接调用

def thanks(request):
    return render(request, 'temp/thanks.html')

无需在数据库中放置任何内容

尝试替换您的模板:

<form class="form-horizontal well" data-async data-target="#myModal" action="index/" method="POST"> <!-- Or whatever the url is for index -->

如果不起作用:

尝试打印表单本身及其字段的值,以查看它是否包含电子邮件地址。

另外,请尝试将文本输入更改为电子邮件输入,也许在调用EmailForm类型转换时Django表单要求将其作为电子邮件输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM