我是Django的新手,並且在表單中遇到了一些問題。我試圖顯示一個文本輸入,以便用戶可以輸入他們的電話號碼,並將其發送到我的電子郵件。我實際上會將它存儲在一個postgre數據庫中,但希望首先獲取基本信息。正在顯示提交按鈕,但文本輸入字段不是。我嘗試將views.py中的forms.py放在裏面,看看PhoneForm()函數和文件是否沒有導入,但是沒有做任何事情。表單字段不在模板中顯示
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'],
['[email protected]'],
)
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())
編輯:添加urls.py(一個在應用程序)
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'),
]
你能顯示urls.py嗎? – vadimchin
@vadimchin ahhh我忘了那些..補充。我想知道是不是有什麼問題 – TheFlyingProgrammer