0

I'm new to Django and am having a bit of trouble with forms. I'm trying to display a single text input so that users can enter their phone number and it will be sent to my email. I'm actually going to have it stored in a postgre database but want to get the basics down first. The submit button is being displayed but the text input field isn't. I tried putting the forms.py inside of the views.py to see if the PhoneForm() function and file wasn't importing but that didn't do anything.

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail

from .forms import PhoneForm

# Create your views here.

def index(request):
    # Render the index.html template with a context dictionary
    return render(request, "index.html")

def get_number(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        #create a form instance
        form = PhoneForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['phone_form'],
                ['siteadmin@example.com'],
            )
            return HttpResponseRedirect('/thanks/')
    else:
        form = PhoneForm()
    return render(request, 'index.html', {'form': form})

index.html

<form action="" method="post">
  {% csrf_token %}
  {{ form }}
  <input type="submit" value="Submit" />
</form>

forms.py

from django import forms

class PhoneForm(forms.Form):
    phone_form = forms.CharField(widget=forms.TextInput())

EDIT: Adding urls.py (the one in the app)

from django.conf.urls import include, url
from django.contrib import admin

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^$', views.get_number, name='get_number'),
]
TheFlyingProgrammer
  • 358
  • 1
  • 6
  • 19

2 Answers2

0

Change from {{form}} to {{form.phone_form}} in index.html

Bob Ezuba
  • 470
  • 5
  • 19
  • No change :C Interestingly, just being on the page, when I try to refresh, I get a google chrome error saying "confirm form resubmission. The page that you're looking for used information you entered" even though there is no visible form – TheFlyingProgrammer Jan 21 '16 at 01:21
0

Both urls in urls.py have the same expression r'^$' and django looks for them in order, therefore the first one (index) will always be selected. This links to the index view not the get_number view. This means the form is not passed to the template and the form does not show up.

To solve this move url(r'^$', get_number), to the top of 'urlpatterns'.

ijames55
  • 146
  • 1
  • 3
  • 15
  • this worked! Thank you! I'm confused on one thing... does this mean that every page can only have 1 "view"? How do you implement multiple forms then in a single page? I always thought that "views" were just a certain part of the page but it looks like it's the entire thing... – TheFlyingProgrammer Jan 21 '16 at 01:35
  • Yeah views are the entire page. To implement more than 1 form set 2 forms `form1 = PhoneForm()` and `form2 = PhoneForm()` in the view then call them in the template `{{ form1 }}` and `{{ form2 }}` inside different HTML forms. Then put `name=form1submit` in the `` button for first form (and do same for second) and then in view `if request.method == 'POST'` and `if 'form1submit' in request.post` perform actions for the first form `elif 'form2submit in request.post` perform actions for second form. More details can be found here: http://stackoverflow.com/questions/1395807/ – ijames55 Jan 21 '16 at 10:39