【问题标题】:Django contact form on homepage主页上的 Django 联系表
【发布时间】:2017-12-16 21:19:02
【问题描述】:

我需要在我的主页底部嵌入一个联系表,这是一个单页应用程序。我遵循了几个关于如何在另一个页面(例如,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 %}

在这种情况下,什么都没有出现。但是,如果我注释掉第二行并从第一行中删除 cmets,则会出现提交按钮。

现在我意识到我假设 django-contact-form 将获取此表单并知道如何处理它,就像在 myportflio.com/contact 场景中可以访问它一样。但是,我知道以前通过urls.py文件有对/contact的引用,但是在嵌入到base.html文件的情况下,就没有引用了。

我可以在 HompePage 视图中以某种方式添加表单,类似于将模型数据添加到上下文中吗?

【问题讨论】:

  • 只需将表单添加到您的主页并使用模态和 jquery/bootstrap *super simple hack 加载它
  • 请告诉我你的views.py以及你在模板中声明表单的HTML部分
  • @hansTheFranz 刚刚添加了它们和一些额外内容。

标签: django django-forms django-templates contact-form django-email


【解决方案1】:

我在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。 无论如何,我已经在项目 repo 中打开了a ticket

【讨论】:

    【解决方案2】:

    您所遵循的教程并不是最新的(它是在 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 中进行这样的验证。无论如何,既然您声明了“表单”变量,它应该在您的模板中可见。

    【讨论】:

    • 两种方法都运行良好。感谢您的帮助。
    • 我对第一个有点害怕,因为我重写了所有内容,而且里面通常有错误。当人们刚开始使用 Django 时,他们无法弄清楚哪里出了问题。很高兴你能让它工作:)
    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多