Django 联系电子邮件表单未在模板 home.html 中呈现/显示

Posted

技术标签:

【中文标题】Django 联系电子邮件表单未在模板 home.html 中呈现/显示【英文标题】:Django contact e-mail form does not render/show up in template home.html 【发布时间】:2021-04-23 17:59:32 【问题描述】:

Django 项目结构

父目录:personal_portfolio_project 主子目录(包含设置):personal_portfolio 应用:作品集、博客

整个项目及其所有应用程序的目录结构(通过tree 在控制台中打印出来):

├── personal_portfolio_project
│   ├── blog
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-39.pyc
│   │   │   ├── admin.cpython-39.pyc
│   │   │   ├── models.cpython-39.pyc
│   │   │   ├── urls.cpython-39.pyc
│   │   │   └── views.cpython-39.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0002_auto_20210117_1306.py
│   │   │   ├── 0003_auto_20210117_1309.py
│   │   │   ├── 0004_auto_20210117_1432.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-39.pyc
│   │   │       ├── 0002_auto_20210117_1306.cpython-39.pyc
│   │   │       ├── 0003_auto_20210117_1309.cpython-39.pyc
│   │   │       ├── 0004_auto_20210117_1432.cpython-39.pyc
│   │   │       └── __init__.cpython-39.pyc
│   │   ├── models.py
│   │   ├── templates
│   │   │   └── blog
│   │   │       ├── all_blogs.html
│   │   │       └── detail.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── db.sqlite3
│   ├── manage.py
│   ├── media
│   │   └── portfolio
│   │       └── images
│   │           ├── DSC_0004a.jpg
│   │           └── DSC_0010a.jpg
│   ├── personal_portfolio
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-39.pyc
│   │   │   ├── settings.cpython-39.pyc
│   │   │   ├── urls.cpython-39.pyc
│   │   │   └── wsgi.cpython-39.pyc
│   │   ├── asgi.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── portfolio
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-39.pyc
│   │   │   ├── admin.cpython-39.pyc
│   │   │   ├── forms.cpython-39.pyc
│   │   │   ├── models.cpython-39.pyc
│   │   │   └── views.cpython-39.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── forms.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-39.pyc
│   │   │       └── __init__.cpython-39.pyc
│   │   ├── models.py
│   │   ├── static
│   │   │   └── portfolio
│   │   │       ├── CV_Andreas_Luckert.pdf
│   │   │       ├── DSC_0010a.jpg
│   │   │       ├── Logo_Andreas_Luckert.png
│   │   │       ├── Logo_ZappyCode_DjangoCourse.png
│   │   │       ├── custom.css
│   │   │       └── main.js
│   │   ├── templates
│   │   │   └── portfolio
│   │   │       ├── about.html
│   │   │       ├── base.html
│   │   │       └── home.html
│   │   ├── tests.py
│   │   └── views.py
│   └── test_sendgrid.py

故事线

我有一个portfolio/forms.py - “投资组合”中的文件 - 包含以下内容的应用程序:

from django import forms

class ContactForm(forms.Form):
    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=True)
    message = forms.CharField(widget=forms.Textarea, required=True)
    # From docs: https://docs.djangoproject.com/en/3.1/topics/forms/#more-on-fields
    cc_myself = forms.BooleanField(required=False)

portfolio/views.py我有以下相关部分:

from .forms import ContactForm  # import module from same parent folder as this script
import General.Misc.general_tools as tools  # custom module to import special-print function

def contactView(request, admin_email='blaa@blaaa.com'):

    if request.method == 'GET':
        sendemail_form = ContactForm()
    else:
        sendemail_form = ContactForm(request.POST)

        if sendemail_form.is_valid():
            # * Retrieve data and set up e-mail (subject, sender, message)
            subject = sendemail_form.cleaned_data['subject']
            from_email = sendemail_form.cleaned_data['from_email']
            message = sendemail_form.cleaned_data['message']
            # From docs: https://docs.djangoproject.com/en/3.1/topics/forms/#more-on-fields
            cc_myself = sendemail_form.cleaned_data['cc_myself']

            recipients = [admin_email]
            if cc_myself:
                recipients.append(from_email)

            # * Send email
            try:
                # General Django docs: https://docs.djangoproject.com/en/3.1/topics/email/#send-mail
                # NOTE on passed list as 4th parameter: recipient-list

                ## i) How to set up an email contact form - docs: https://learndjango.com/tutorials/django-email-contact-form
                send_mail(subject,
                          message,
                          from_email,
                          recipients,
                          fail_silently=False)

            # * Exceptions * #
            # NOTE on scope: security
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            # General exception to be printed out
            except Exception as e:
                tools.except_print(f"ERROR:\ne")

            # Return success (if this code is reached)
            return redirect('success')

    # Send the completed (filled-in) form to the homepage - HTML - document
    return render(request, "portfolio/home.html",
                  'sendemail_form': sendemail_form)


def successView(request):
    return HttpResponse('Success! Thank you for your message.')

这是我在portfolio/templates/portfolio/home.html 末尾包含的联系表格(HTML 的其余部分工作得非常好,通常Django 在显示localhost 时不会在控制台中显示任何错误-网页):

<!-- ** E-Mail contact form **
Docs: https://learndjango.com/tutorials/django-email-contact-form-->

% if sendemail_form %
    <h2 class="mt-5">Contact Me</h2>
    <hr>
    <form method="post">
        % csrf_token %
        <!-- NOTE on ".as_p": https://docs.djangoproject.com/en/3.1/topics/forms/#form-rendering-options
        Possible attributes: as_table, as_p, as_ul -->
         sendemail_form.as_p 
        <div class="form-actions">
            <button type="submit">Send</button>
        </div>
    </form>
% endif %

personal_portfolio/urls.py的内容如下:

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings  # get info from the settings.py - file
from portfolio import views  # other views could also be imported (e.g. from the blog-app)

urlpatterns = [
    path("", views.home, name="home"),

    # Docs: https://learndjango.com/tutorials/django-email-contact-form
    path("contact/", views.contactView, name='contact'),
    path("success/", views.successView, name='success'),

    # Add "About me" for additonal info about the creator of the webpage
    path("about/", views.about, name="about"),

    # Standard admin page
    path('admin/', admin.site.urls),

    # NOTE on include: forwarding all /blog/ - related requests to the blog-app
    path('blog/', include('blog.urls')),
]

# * Add a static link to the MEDIA - files * #
# NOTE on processing: import certain variables defined in the settings.py of this project
# Docs: google django images (or media (folder)) etc.
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我尝试在portfolio/admin.py 中注册表单,就像在portfolio/models.py 中定义的类一样,但首先我不知道这是否有必要,其次由于TypeError: 'DeclarativeFieldsMetaclass' object is not iterable,它甚至无法正常工作,所以我后来从portfolio/admin.py 中删除了以下代码:

from django.contrib import admin
# !!! CAUTON: TypeError: 'DeclarativeFieldsMetaclass' object is not iterable
from .forms import ContactForm

# NOTE on registering form: doesn't work due to the following error:
# TypeError: 'DeclarativeFieldsMetaclass' object is not iterable
admin.site.register(ContactForm)

总的来说,我对如何使表单最终出现在我的portfolio/templates/portfolio/home.html 上感到困惑。 当% if sendemail_form % 扫描要传递的non-None 表单时,这表明该表单没有正确传递。


测试以检查调用联系人表单的views.py 中的函数contactView() 是否已到达并带有打印输出:

$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 19, 2021 - 14:16:37
Django version 3.1.5, using settings 'personal_portfolio.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[19/Jan/2021 14:16:42] "GET / HTTP/1.1" 200 31617
[19/Jan/2021 14:16:42] "GET /static/portfolio/custom.css HTTP/1.1" 304 0
[19/Jan/2021 14:16:42] "GET /static/portfolio/main.js HTTP/1.1" 304 0
[19/Jan/2021 14:16:43] "GET /static/portfolio/Logo_ZappyCode_DjangoCourse.png HTTP/1.1" 200 442438

--> 根据控制台没有到达,因为没有出现打印输出。

【问题讨论】:

您是否在home 视图中传递了您的联系表格? 我是 django 新手,不完全确定您的意思,@Benbb96,但希望目录树能帮助理解这里发生了什么。 【参考方案1】:

您需要在呈现主页的主页视图中创建ContactForm。我们需要这样做,因为表单出现在主页模板中;如果我们不在此视图中添加表单,那么模板的联系表单部分将永远无法工作,因为它接收到None

def home(request):
    ... your code here ...

    # send empty contact form to template
    sendemail_form = ContactForm()

    return render(
        request,
        "portfolio/home.html",
        'sendemail_form': sendemail_form)

那么contactView 不应该渲染模板,只是重定向。 contactView 的工作是获取 POST 数据并对其进行处理,然后重定向到不同的视图;它不应该渲染模板。

def contactView(request, admin_email='blaa@blaaa.com'):
    if request.method == 'POST':
        sendemail_form = ContactForm(request.POST)

        if sendemail_form.is_valid():
            subject = sendemail_form.cleaned_data['subject']
            from_email = sendemail_form.cleaned_data['from_email']
            message = sendemail_form.cleaned_data['message']
            cc_myself = sendemail_form.cleaned_data['cc_myself']

            recipients = [admin_email]
            if cc_myself:
                recipients.append(from_email)

            try:
                send_mail(subject,
                          message,
                          from_email,
                          recipients,
                          fail_silently=False)
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            except Exception as e:
                tools.except_print(f"ERROR:\ne")
                return HttpResponse('errors.')

            # Return success (if this code is reached)
            return redirect('success')

    # return to home page
    return redirect('home')

另外,将您的联系表单(位于主页模板中)指向正确的 URL(这是联系 URL,而不是主页 URL);你可以改变:

% if sendemail_form %
    ...
    <form method="post">
    ...
% endif %

% if sendemail_form %
    ...
    <form method="post" action="% url 'contact' %">
    ...
% endif %

【讨论】:

现在终于可以用了,非常感谢!出于某种原因,网页learndjango.com/tutorials/django-email-contact-form 的过程是按照我最初的方式提出的。生成的联系表单在样式方面看起来非常基本,但这是我将尝试通过谷歌解决它的另一个主题:P【参考方案2】:

我认为您需要将正确的 URL 添加到联系人 HTML 表单中。

你说HTML表单位于首页,但是HTML表单没有指定不同的action(目标URL),所以会post(发送HTTP POST请求)到首页URL ,它使用视图views.home,这不是你需要的。

通过在表单中​​指定不同的 HTML 操作属性,页面会将数据发送到不同的 URL(应为 contact),然后该 URL 应使用您的视图 views.contactView

尝试从

更改联系表单(在主页模板中)的 HTML 代码
% if sendemail_form %
    ...
    <form method="post">
    ...
% endif %

% if sendemail_form %
    ...
    <form method="post" action="% url 'contact' %">
    ...
% endif %

另外,在您的主页视图中添加ContactForm()

【讨论】:

有道理,谢谢解释。我刚刚实现了,但是表单仍然没有显示出来。 @AndreasL。您确定您的contactView 正在被使用吗?您可以尝试通过在视图中放置打印语句来查看到达视图的哪些部分。 其实我已经试过了,但我会重复这个意图。我确定它没有被使用,否则它会返回表单。也许整个 django 项目的目录树(见我帖子末尾的注释)将有助于解决问题。 @AndreasL。您还需要将sendemail_form = ContactForm() 添加到您的主视图中。你有吗?或者您可以将您的主视图代码添加到问题中吗? 这是我在上面发布的portfolio/views.py 的一部分:if request.method == 'GET': sendemail_form = ContactForm() else: sendemail_form = ContactForm(request.POST)。但显然,这个功能永远无法实现,我插入了一个没有发送到控制台的打印输出。至于目录结构,我是根据 udemy.com 做的——当然,所以我对什么是我的“家”目录感到困惑。主目录是settings.py 所在的目录吗?那么,这将是personal_portfolio,但没有views.py,因为这不是应用程序。

以上是关于Django 联系电子邮件表单未在模板 home.html 中呈现/显示的主要内容,如果未能解决你的问题,请参考以下文章

重定向后表单未在模板中呈现(django)

基本模板未在 Django 中加载

Django python联系电子邮件表单错误帖子不允许405

Python Django 电子邮件表单示例 [关闭]

仅从模板中的 django 表单中获取一个字段

Django为表单中的两个提交按钮呈现不同的模板