简体   繁体   中英

How to configure Bootstrap3 form for use with Django form

I posted a similar question previously, but the answers did not lead me to a solution. I believe the solution now lies within my template, which many answers did not seem to address.

Anyways, I believe views.py, forms.py and models.py to be correct and my issue seems to be calling the form within the Bootstrap3 modal and I'm not quite sure how to do this.

This issue is that when the form is filled out validation does not occur, nor does the email get sent to the database. However, on form submit you are sent to the thanyou page, so at least I know that is working properly...

Any direction would be helpful.

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

forms.py

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

EDIT : I didn't see last 2 lines of your views. You send your form to thanks/ , so it directly calls

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

without putting anything in your database

Try replacing in your template :

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

If it doesn't work :

Try to print the form itself and the values of it's fields to see if it contains the email adress.

Also, try changing your text input to an email input, maybe Django form requires it to be email input when you call the EmailForm cast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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