【问题标题】:Django and Bootstrap contact form not workingDjango 和 Bootstrap 联系表不起作用
【发布时间】:2021-10-27 22:08:05
【问题描述】:

我正在尝试使用 Bootstrap 样式制作 Django 联系表单,但它不起作用。我在视图中尝试使用send_mail 而不是EmailMessage,但它仍然不起作用。当我点击“发送”按钮时,页面只是重新加载,没有任何反应,我没有收到任何电子邮件。

请注意,出于安全考虑,我更改了电子邮件地址和密码,但它是一个 Gmail 帐户。

这是我的文件:

home.html

<footer class="bg-dark text-white pt-3" id="contact">
    <div class="row text-center col-lg-12"> 
      <div>
        <h3>Contact</h3>
        <img src="{% static 'home_app/img/contact.png' %}" class="img-fluid">
      </div>
    </div>

    <div class="col-lg-12 mx-auto">
      <form method="post">
        {% csrf_token %}
        <div class="row">
          <div class="col-lg-6 col-sm-3" >
            <input type="text" class="form-control" name="name" placeholder="Name and last name" required>
          </div>
          <div class="col-lg-6 col-sm-3">
            <input type="tel" class="form-control" name="subject" placeholder="Subject" required>
          </div>
          <div class="col-lg-12 col-sm-6">
            <input type="text" class="form-control" name="email" placeholder="email@example.com" required>
          </div>
          <div class="col-lg-12 col-sm-6">
            <textarea class="form-control" name="text" placeholder="Write your message" rows="5"></textarea>  
          </div>
          <div class="col-lg-12">
            <button type="Submit" class="btn btn-primary w-100 fs-5">Send</button>
          </div>
        </div>
      </form>
    </div>
  </footer>

views.py

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.core.mail import EmailMessage
    
def contact(request):
    if request.method == "POST":
        name = request.POST.get('name')
        subject = request.POST.get('subject')
        email = request.POST.get('email')
        message = request.POST.get('text')
        return redirect('contact')
   
        email=EmailMessage("Message from Django",
        "User: {} Subject: {} Address: {} Message:\n\n {}".format(name,subject,email,message),
        "",["email@example.com"],reply_to=[email])
    
        try: 
            email.send()
            return redirect("/?valid")
    
        except:
            return redirect("/?notvalid")   
    
        return render(request,'home_app/home.html',{})

setting.py

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_PORT = 587
EMAIL_HOST_USER = "email@example.com"
EMAIL_HOST_PASSWORD = "password123"

如果有人能帮助我,我将不胜感激

【问题讨论】:

    标签: django django-views bootstrap-5 django-settings


    【解决方案1】:

    return render(request,'home_app/home.html',{}) 没有返回任何上下文。

    你可以考虑我项目的功能之一

    def list_auction(request, id):
        lists = AuctionList.objects.filter(id = id).first()
        bid = AuctionBid.objects.filter(lists = lists)
        comment = AuctionComment.objects.filter(lists = lists)
        highest_bid = lists.starting_bid
        if bid is not None:
            for bids in bid:
                if bids.input_value > highest_bid:
                    highest_bid = bids.input_value
        if request.method == 'POST':
            user = request.user
            lists = AuctionList.objects.filter(id = id).first()
            comment = request.POST.get('comment', None)
            input_value = request.POST.get('Auction_price', None)
            try:
                input_value = float(input_value)
            except:
                input_value = None
            if comment is not None:
                comm = AuctionComment.objects.create(comment = comment, user = user, lists = lists)
                comm.save()
                return HttpResponseRedirect(reverse('lists', args = [id]))
            if input_value is not None:
                if float(input_value) < highest_bid:
                    return HttpResponseRedirect(reverse('lists', args = [id]))
                bid = AuctionBid.objects.create(input_value = float(input_value), user = user, lists =lists)
                bid.save()
                prev_bid = AuctionBid.objects.filter(lists = lists).exclude(input_value = input_value)
                prev_bid.delete()
                return HttpResponseRedirect(reverse('lists', args = [id]))
            if comment is None and input_value is None:
                return render(request, "auctions/error.html", {"message": "Please bid the product or add a comment."})
        context = {"lists": lists, "highest_bid":highest_bid, "min_bid":(highest_bid + 0.50),"comment":comment}
        return render(request, "auctions/lists.html", {"context"=context}  )
            
    

    查看代码的最后一行。

    您可以在渲染时看到我正在向我的 HTML 返回一些我想在该特定 Html 文件中看到的内容。您需要添加诸如上下文之类的内容来查看结果,或者您可以使用 JavaScript 控制任务。在这种情况下,您不需要返回上下文。

    【讨论】:

      【解决方案2】:

      我解决了这个问题。 home.html 有它自己的视图,称为 home,并且联系人部分位于此 html 中,因此无论出于何种原因,当联系人 html 部分位于主视图中时,联系人视图都无法响应。解决方案是为联系人部分创建一个 html,以便联系人视图可以响应

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多