如何为我的联系页面制作成功网址

Posted

技术标签:

【中文标题】如何为我的联系页面制作成功网址【英文标题】:How to make a success ulr for my contact page 【发布时间】:2021-06-05 04:16:11 【问题描述】:

我希望将客户端重定向到另一个 html 页面,作为我联系表单中成功发送的消息。但我不知道这里到底出了什么问题。我无法重定向到我的成功页面,但表单有效。 #我的主应用的 url.py

urlpatterns = [
path('^contact/', include('contactus.urls')),
]

#我的联系人应用 url.py

from __future__ import unicode_literals
from django.conf.urls import url
from django.views.generic import TemplateView

from .views import ContactUsView

urlpatterns = [
url(r'^$', ContactUsView.as_view(), , 'contactus'),
url(r'^success/$', TemplateView.as_view(),
    , 'contactus-success'),
]

#contact app view.py

from __future__ import unicode_literals

from django.conf import settings
from django.core.mail import EmailMessage
from django.template import loader
from django.views.generic.edit import FormView

from contactus.forms import ContactUsForm


class ContactUsView(FormView):
    template_name = 'contactus/contact.html'
    email_template_name = 'contactus/contact_notification_email.txt'
    form_class = ContactUsForm
    success_url = "/contact/success/"
    subject = "Contact Us Request"

def get_initial(self):
    initial = super(ContactUsView, self).get_initial()
    if not self.request.user.is_anonymous:
        initial['name'] = self.request.user.get_full_name()
        initial['email'] = self.request.user.email

    return initial

   def form_valid(self, form):
        form_data = form.cleaned_data

    if not self.request.user.is_anonymous:
        form_data['username'] = self.request.user.username

    # POST to the support email
    sender = settings.SERVER_EMAIL
    recipients = (getattr(settings, 'CONTACT_US_EMAIL'),)

    reply_to = form_data.get('email') or sender

    tmpl = loader.get_template(self.email_template_name)
    email = EmailMessage(
        self.subject,
        tmpl.render(form_data),
        sender,
        recipients,
        reply_to=[reply_to],
    )
    email.send()

    return super(ContactUsView, self).form_valid(form)

#contact app forms.py

from __future__ import unicode_literals
from django import forms


class ContactUsForm(forms.Form):
    name = forms.CharField(required=True, max_length=512)
    email = forms.EmailField(required=True)
    subject = forms.CharField(required=True, max_length=512)
    description = forms.CharField(
        widget=forms.Textarea(attrs='class': "form-control"),
        required=True)

def clean(self):
    cleaned_data = super(ContactUsForm, self).clean()

    return cleaned_data

#Contact.html

% extends 'website/base.html' %
% load static %

% block main %

<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
  <div class="container">

    <div class="d-flex justify-content-between align-items-center">
      <h2>Contact</h2>
      <ol>
        <li><a href="home.html">Home</a></li>
        <li>Contact</li>
      </ol>
    </div>

  </div>
  </section><!-- End Breadcrumbs -->


 <section id="contact" class="contact">
  <div class="container">
    <div class="row mt-5 justify-content-center" data-aos="fade-up">
      <div class="col-lg-10">
    <form action="." method="post" role="form" class="php-email-form">% csrf_token %
           <div class="row">
            <div class="col-md-6 form-group % if form.name.errors %has-error% endif %">
                <input type="text" name="name" class="form-control"
                   data-rule="minlen:4" data-msg="Please enter at least 4 chars"
                       id="id_name" placeholder="Your Name"
                       value="% if form.data.name %form.data.name% else%                                 form.initial.name% endif %" />                                                
                     <div class="validate"></div>
            </div>
            <div class="col-md-6 form-group mt-3 mt-md-0 % if form.email.errors %has-error% endif %">
                <input class="form-control" type="email" name="email"
                       id="email" data-rule="email" data-msg="Please enter a valid email"
                       id="id_email" placeholder="Your Email"
                       value="% if form.data.email %form.data.email% else     %form.initial.email% endif %" />
                <div class="validate"></div>
            </div>
          </div>
         <div class="form-group mt-3 % if form.subject.errors %has-error% endif %">
                    <input type="text" class="form-control" name="subject" id="subject"     placeholder="Subject"
                           data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject"      />
           <div class="validate"></div>
            <div class="validate"></div>
          </div>
           <div class="form-group mt-3 % if form.description.errors %has-error% endif %">
               <textarea class="form-control" name="description" rows="5" data-rule="required"
                         data-msg="Please write something for us" placeholder="Message"></textarea>

            <div class="validate"></div>
          </div>
            <div class="mb-3">
          </div>
          <div class="text-center"><button type="submit">Send         Message</button></div>
    
    </form>
      </div>

    </div>

  </div>
</section><!-- End Contact Section -->
% endblock %

【问题讨论】:

【参考方案1】:

错误可能来自视图。

尝试:

success_url = "/success/"

或者你可以这样离开你的视图并将 urls.py 中的 url 更改为:

url(r'^contact/success/$',......),

【讨论】:

以上是关于如何为我的联系页面制作成功网址的主要内容,如果未能解决你的问题,请参考以下文章

如何为所有页面制作一个 CustomScrollView

如何为也可以包含视频的 wordpress 帖子制作滑块?

如何为 djangocms-blog 设置自定义模板?

如何为每个帖子自动制作带有新 URL 的子页面 [NodeJS]

如何为所有模式制作一个脚本?

如何为我的引导加载程序制作内核?