【发布时间】:2021-04-03 01:48:38
【问题描述】:
我正在使用 Django 开发博客,当我想获取联系信息时,发布请求似乎根本不起作用。在联系人视图中,我并没有做太多事情,我只是想确保发布请求正常工作,但它返回一个 Get 请求,并且 get 方法工作得非常好
javascript 文件
//CONTACT FORM
$('#contactform').submit(function(){
var action = $(this).attr('action');
$("#message").slideUp(750,function() {
$('#message').hide();
$('#submit').attr('disabled','disabled');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
comments: $('#comments').val()
},
function(data){
document.getElementById('message').innerHTML = data;
$('#message').slideDown('slow');
$('#submit').removeAttr('disabled');
if(data.match('success') != null) $('#contactform').slideUp('slow');
$(window).trigger('resize');
}
);
});
return false;
});
view.py
from django.http.response import HttpResponse
from contact.forms import ContactForm
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
# Create your views here.
def contacts(request):
if request.method == "POST" and request.is_ajax:
print('hello!!')
return render(request, 'contact/contact.html')
forms.py
from django import forms
from .models import Contact
class ContactForm(forms.Form):
class Meta:
model = Contact
fields = ['cont_name', 'cont_email', 'cont_message', 'cont_date']
models.py
from django.db import models
from datetime import datetime
class Contact(models.Model):
cont_name = models.CharField( max_length = 10)
cont_email = models.EmailField()
cont_message = models.TextField(blank=False)
cont_date = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.cont_name
contact.html
<div class="container">
<div class="row">
<div class="col-md-6">
<p class="lead">Sold old ten are quit lose deal his sent. You correct how sex several far distant believe journey parties. We shyness enquire uncivil affixed it carried to. </p>
<p>End sitting shewing who saw besides son musical adapted. Contrasted interested eat alteration pianoforte sympathize was. He families believed if no elegance interest surprise an. It abode wrong miles an so delay plate.</p>
</div>
<div class="col-md-6">
<div id="message"></div>
<form method="post" action="{% url 'contacts' %}" id="ContactForm" class="main-contact-form wow">
{% csrf_token %}
<input type="text" class="form-control col-md-4" name="name" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name." />
<input type="text" class="form-control col-md-4" name="email" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address." />
<textarea name="comments" class="form-control" id="comments" placeholder="Your Message *" required data-validation-required-message="Please enter a message."></textarea>
<input class="btn btn-primary btn-theme" type="button" name="submit" value="Submit" />
</form>
</div>
</div>
</div>
urls.py
from django.urls import path, reverse
from . import views
urlpatterns = [
path('', views.contacts, name= 'contacts'),
]
【问题讨论】:
-
欢迎来到 Stack Overflow。具体来说,“似乎根本不起作用”是什么意思?你得到什么 HTTP 代码?它是否带有错误消息?您在日志中看到了什么?请阅读How to Ask。
标签: javascript python html django ajax