我正在尝试在同一项目中的页面应用index.html
模板上呈现新闻通讯应用表单,但由于某种原因,该表单未显示在首页上。请,有人可以帮忙,因为我是使用Django的新手。
这是我的pages\views.py
:
from django.shortcuts import render
from newsletter.forms import JoinForm
def index(request):
return render(request, 'pages/index.html')
这是我的newsletter\views
:
from django.shortcuts import render
from .models import Join
from .forms import JoinForm
def newsletter_signup(request):
form = JoinForm(request.Post or None)
if form.is_valid():
instance = form.save(commit=false)
if Join.objects.filter(email=instance.email).exists():
print("This email already exists")
else:
instance.save()
context = {'form': form,}
return render(request, 'pages/index.html', context)
这是我的pages\index.html
模板:
<form action="">
{% csrf_token %}
{{ join }}
<button class="waves-effect waves-light btn">Subscribe</button>
</form>
这是我的newsletter\forms.py
:
from django import forms
from .models import Join
class JoinForm(forms.ModelForm):
class Meta:
model = Join
fields = ['email']
def clean_email(self):
email = self.cleaned_data.get('email')
return email
谢谢。