Django 和 Bootstrap 联系表不工作

Django and Bootstrap contact form not working

我正在尝试制作具有 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"

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

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

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

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}  )
        

查看代码的最后一行。

渲染时你可以看到我正在 return 向我的 HTML 发送一些我想在那个特定的 Html 文件中看到的东西。您需要添加诸如上下文之类的内容才能查看结果,或者您可以使用 JavaScript 控制任务。在这种情况下,您不需要 return 上下文。

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