主页上的 Django 联系表
Django contact form on homepage
我需要在主页底部嵌入一个联系表单,这是一个单页应用程序。我遵循了几个关于如何在另一个页面(例如 myportfolio.com/contact)上创建自己的联系表单的教程,这些教程都成功了。不幸的是,我无法调整这些结果或找到其他教程,了解如何在同一页面上显示该联系表单并使其发挥作用,我的所有其他模型数据都已显示(例如,myportfolio.com)。
我遵循了本教程:https://atsoftware.de/2015/02/django-contact-form-full-tutorial-custom-example-in-django-1-7。我只创建了一个应用程序。
我觉得这应该是一件很想做的事情,这让我觉得我忽略了一些非常明显的事情。
views.py 申请中
from django.views.generic import TemplateView
from portfolio.models import Experience
class HomePage(TemplateView):
template_name = 'portfolio/base.html'
def get_context_data(self, *args, **kwargs):
context = super(HomePage, self).get_context_data(*args, **kwargs)
context['experience_list'] = Experience.objects.order_by('-start_date')
return context
contact_form.html
{% extends 'portfolio/base.html' %}
{% block contact %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
现在 contact_form.html 像这样进入 portfolio/base.html:
base.html
{# {% include "contact_form/contact_form.html" %}#}
{% block contact %}{% endblock %}
在这种情况下,没有任何显示。但是,如果我注释掉第二行并删除第一行的注释,就会出现提交按钮。
现在我意识到我假设 django-contact-form 将选择此表单并知道如何处理它,就像在 myportflio.com/contact 场景中访问它时一样。但是,我知道以前曾经通过urls.py文件引用/contact,但是在嵌入到base.html文件中的情况下,不再有引用。
我能否在 HompePage 视图中以某种方式添加表单,类似于将模型数据添加到上下文中?
您正在学习的教程并不是最新的(它是用 1.7 编写的)。所以你通常在 views.py 中做的(以及你的函数中缺少的)是声明表单。
我会告诉你我会怎么做,所有这些都与你的代码有点不同,但在我看来更容易理解,这就像标准方法。
from appName.forms import formName #dont forget to import the form
from django.views.generic import TemplateView
from portfolio.models import Experience
def HomePage(request):
form = formName(request.POST or None) #here you tell django what form you are talking about
if form.is_valid(): #checking if the form input is valid
....
form.save()
#messages.success(request, 'form was posted') #this is optional but good for the user
context = {
'form': form, #here you are passing the variable "form" to the template so you can use it like "{{form.as_p}}"
'experience_list' = Experience.objects.order_by('-start_date')
#other context variables
}
return render(request, "appName/MainPage.html", context)
您的 urls.py 在应用程序中看起来像:
urlpatterns = [
url(r'mainPage',views.HomePage, name='HomePage'),
]
如果你想保持一切原样,试试这个:
def get_context_data(self, *args, **kwargs):
context = super(HomePage, self).get_context_data(*args, **kwargs)
context['experience_list'] = Experience.objects.order_by('-start_date') #maybe you have to put a "," here but I'm not sure
context['form'] = formName
return context
但我不确定如何在通用 TemplateView 中进行这样的验证。无论如何,既然你声明了 "form" 变量,它应该在你的模板中可见。
我在 view.py
中解决这个问题的方法是:
from contact_form.forms import ContactForm
def index(request):
if request.method == 'POST':
form = ContactForm(request.POST or None, request=request)
if form.is_valid(): # checking if the form input is valid
form.save()
return render(request, 'myapp/index.html')
它适用于我的 django-contact-form 1.8.2 和 django 3.0.8。
不管怎样,我已经在项目仓库中打开了a ticket。
我需要在主页底部嵌入一个联系表单,这是一个单页应用程序。我遵循了几个关于如何在另一个页面(例如 myportfolio.com/contact)上创建自己的联系表单的教程,这些教程都成功了。不幸的是,我无法调整这些结果或找到其他教程,了解如何在同一页面上显示该联系表单并使其发挥作用,我的所有其他模型数据都已显示(例如,myportfolio.com)。
我遵循了本教程:https://atsoftware.de/2015/02/django-contact-form-full-tutorial-custom-example-in-django-1-7。我只创建了一个应用程序。
我觉得这应该是一件很想做的事情,这让我觉得我忽略了一些非常明显的事情。
views.py 申请中
from django.views.generic import TemplateView
from portfolio.models import Experience
class HomePage(TemplateView):
template_name = 'portfolio/base.html'
def get_context_data(self, *args, **kwargs):
context = super(HomePage, self).get_context_data(*args, **kwargs)
context['experience_list'] = Experience.objects.order_by('-start_date')
return context
contact_form.html
{% extends 'portfolio/base.html' %}
{% block contact %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
现在 contact_form.html 像这样进入 portfolio/base.html:
base.html
{# {% include "contact_form/contact_form.html" %}#}
{% block contact %}{% endblock %}
在这种情况下,没有任何显示。但是,如果我注释掉第二行并删除第一行的注释,就会出现提交按钮。
现在我意识到我假设 django-contact-form 将选择此表单并知道如何处理它,就像在 myportflio.com/contact 场景中访问它时一样。但是,我知道以前曾经通过urls.py文件引用/contact,但是在嵌入到base.html文件中的情况下,不再有引用。
我能否在 HompePage 视图中以某种方式添加表单,类似于将模型数据添加到上下文中?
您正在学习的教程并不是最新的(它是用 1.7 编写的)。所以你通常在 views.py 中做的(以及你的函数中缺少的)是声明表单。
我会告诉你我会怎么做,所有这些都与你的代码有点不同,但在我看来更容易理解,这就像标准方法。
from appName.forms import formName #dont forget to import the form
from django.views.generic import TemplateView
from portfolio.models import Experience
def HomePage(request):
form = formName(request.POST or None) #here you tell django what form you are talking about
if form.is_valid(): #checking if the form input is valid
....
form.save()
#messages.success(request, 'form was posted') #this is optional but good for the user
context = {
'form': form, #here you are passing the variable "form" to the template so you can use it like "{{form.as_p}}"
'experience_list' = Experience.objects.order_by('-start_date')
#other context variables
}
return render(request, "appName/MainPage.html", context)
您的 urls.py 在应用程序中看起来像:
urlpatterns = [
url(r'mainPage',views.HomePage, name='HomePage'),
]
如果你想保持一切原样,试试这个:
def get_context_data(self, *args, **kwargs):
context = super(HomePage, self).get_context_data(*args, **kwargs)
context['experience_list'] = Experience.objects.order_by('-start_date') #maybe you have to put a "," here but I'm not sure
context['form'] = formName
return context
但我不确定如何在通用 TemplateView 中进行这样的验证。无论如何,既然你声明了 "form" 变量,它应该在你的模板中可见。
我在 view.py
中解决这个问题的方法是:
from contact_form.forms import ContactForm
def index(request):
if request.method == 'POST':
form = ContactForm(request.POST or None, request=request)
if form.is_valid(): # checking if the form input is valid
form.save()
return render(request, 'myapp/index.html')
它适用于我的 django-contact-form 1.8.2 和 django 3.0.8。 不管怎样,我已经在项目仓库中打开了a ticket。