Django Form using Ajax and jQuery! Can't work out how to pass control from Ajax to Python in URL -
how use python file (project/controller/functions/email/sendemail.py) process sending of email (or other task) ajax using jquery? or going wrong?
i don't want redirect page django tuts teach, want refresh form , show success message only. (i have messages , refresh of form working fine not handling of url in ajax).
i have searched high , low on , have come trumps without useful. advice or links examples appreciated.
my files similar below, except using jquery validate bit different same principal , form laid out using bootstrap 3 on version.
index.html
<form method="post" action="sendemail" id="sendemail"> <input name="name" type="text" /> <input name="email" type="email" /> <input name="submit" type="submit" value="send" /> </form>
main.js
$('form#sendemail').on('submit', function() { $.ajax({ url: $(this).attr('action'), #(targeting action form. working can console.log correct action sendemail), type: type, data: data }); });
i tried redirecting urls target file use url ajax target file below.
urls.py
from django.conf.urls import include, url django.contrib import admin . import views .controller.functions.email import sendemail urlpatterns = [ url(r'^$', views.home, name='home'), url(r'^sendemail', sendemail, name='sendemail'), url(r'^admin/', include(admin.site.urls)), ]
i 500 server error in console this.
sendemail.py
from django.core.mail import send_mail def sendemail(request): if (send_mail('subject', 'here message.', 'from@example.com', ['to@example.com'], fail_silently=false)): print(1) #success else: print(99) #fail
views.py
from django.shortcuts import render django.views.decorators.csrf import csrf_protect .forms import contactform @csrf_protect def home(request): tpl = 'main/index.html' contactnotify = "" contactform = request.post if request.method == 'post': contactform = contactform(request.post) if contactform.is_valid(): return render(request, tpl, context) else: contactform = contactform() context = { 'contactform' : contactform } return render(request, tpl, context)
in php use echo return jquery presume print it's equivalent in python return value rather return.
on send following log in console: post http://localhost:8888/sendemail 500 (internal server error)
django works differently
<form method="post" action="{% 'sendemail' %}" id="sendemail">
urls.py
from . import views .controller.functions.email import sendemail urlpatterns = [ url(r'^$', views.home, name='home'), url(r'^sendemail$/', views.sendemail_view, name='sendemail'), url(r'^admin/', include(admin.site.urls)), ]
views.py
from .helpers import sendemail def sendemail_view(request): # here sendemail() # , return httpresponse
and remember,
- after urlconf match, django calls function views. how django designed, views == controllers frameworks
- django views function names should lowercase always.
i dont know why listening post requests in home views.. think there more things fix work, please try suggestions
Comments
Post a Comment