Django主页上的联系方式
我需要在我的主页底部嵌入一个联系人表单,这是一个单页应用程序。我学习了一些关于如何在另一个页面(例如myportfolio.com/contact)上创建自己的联系人表单的教程,这些教程都很成功。不幸的是,我无法调整这些结果,也无法在同一页面上找到关于如何使联系人表单可见和功能的其他教程。我的所有其他模型数据都会显示出来(例如,myportfolio.com) 我遵循了以下教程:。我只创建了一个应用程序 我觉得这应该是一件很平常的事情,这让我觉得我忽略了一些显而易见的事情 应用程序中的views.pyDjango主页上的联系方式,django,django-forms,django-templates,contact-form,django-email,Django,Django Forms,Django Templates,Contact Form,Django Email,我需要在我的主页底部嵌入一个联系人表单,这是一个单页应用程序。我学习了一些关于如何在另一个页面(例如myportfolio.com/contact)上创建自己的联系人表单的教程,这些教程都很成功。不幸的是,我无法调整这些结果,也无法在同一页面上找到关于如何使联系人表单可见和功能的其他教程。我的所有其他模型数据都会显示出来(例如,myportfolio.com) 我遵循了以下教程:。我只创建了一个应用程序 我觉得这应该是一件很平常的事情,这让我觉得我忽略了一些显而易见的事情 应用程序中的views
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
联系表单.html
{% extends 'portfolio/base.html' %}
{% block contact %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
{# {% include "contact_form/contact_form.html" %}#}
{% block contact %}{% endblock %}
在这个场景中,没有显示任何内容。但是,如果我注释掉第二行并删除第一行中的注释,则会出现submit按钮
现在我意识到,我假设django联系人表单将获取此表单,并知道如何处理它,就像在myportflio.com/contact场景中可以访问它一样。但是,我知道以前通过url.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)
应用程序中的URL.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”变量,它应该在您的模板中可见。您所遵循的教程实际上不是最新的(它是在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)
应用程序中的URL.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”变量,它应该在模板中可见。我在
视图.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联系表1.8.2和django 3.0.8。
无论如何,我已经在项目回购中打开了它。我在视图.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联系表1.8.2和django 3.0.8。
不管怎样,我已在project repo中打开。只需将表单添加到主页,并加载它w/a modal和jquery/bootstrap*super simple hack请向我展示您的views.py以及您在template@hansTheFranz只是添加了它们和一些额外的内容。只需将表单添加到主页中,并加载它,其中包含模态和jquery/bootstrap*超级简单hack请向我展示您的views.py以及在template@hansTheFranz只是添加了它们和一些额外的东西。两种方法都非常有效。谢谢你的帮助。第一次我有点害怕,因为我重写了所有的东西,通常里面都有bug。当人们刚开始使用Django时,他们无法判断出哪里出了问题。很高兴你能成功:)这两种方法都很有效。谢谢你的帮助。第一次我有点害怕,因为我重写了所有的东西,通常里面都有bug。当人们刚开始使用Django时,他们无法判断出哪里出了问题。很高兴你能成功:)