0
對於我的Ajax請求我得到一個404AJAX請求查看在Django生產404
[06/Feb/2015 06:46:27] "POST /contact/ HTTP/1.1" 404 2149
我是新來的AJAX,但我覺得我的邏輯是做發送POST請求/contact/
網址。這會將請求發送到我的視圖中的contact
函數並打印出一些文本(現在爲了測試目的)。我不確定爲什麼我得到了404。我對Django和web開發人員相當陌生。
JS:
function create_post() {
console.log("create post is working");
var firstName = document.getElementById("firstname");
var lastName = document.getElementById("lastname");
var email = document.getElementById("email");
var phoneNumber = document.getElementById("phonenumber");
var message = document.getElementById("message");
var contactInfo = {
"first_name": firstName.value,
"last_name": lastName.value,
"email": email.value,
"phone_number": phoneNumber.value,
"message": message.value
};
$.ajax({
url: "/contact/",
type: "POST",
data: contactInfo,
success: console.log(contactInfo),
error: console.log("ajax fail")
});
};
// Contact form submit
var contactForm = document.getElementById("contact-form");
$(contactForm).on('submit', function(event) {
event.preventDefault();
console.log("form submitted");
create_post();
});
我猜測它可能是我的網址?
家/ urls.py:
urlpatterns = patterns('',
url(r'^$', load_home_content),
url(r'^contact/$', contact, name="contact"),
)
urls.py:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('home.urls'), name='home'),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
家/ views.py:
def contact(request):
if request == "POST":
print request.POST
form = ContactForm(request.POST)
print form.first_name, form.last_name, form.email, form.phone_number
if form.is_valid():
print "form valid"
else:
print "form invalid"
return render(request, 'home/home.html', {'form': ContactForm})
開發工具輸出:
爲什麼刪除$符號? – 2018-02-14 19:33:50
$表示「字符串結尾」,因此「^ $」表達式僅與空字符串匹配。 – catavaran 2018-02-15 23:39:30