I am trying to save a form to update an existing user, but I am unable to get it to work. The error occurs when I try to save the form. The console shows the error occurs in the view (internal server error).
Form:
class updateFirstName(forms.ModelForm):
class Meta:
model = User
fields = ('first_name',)
def __init__(self, *args, **kwargs):
super(updateInfoForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-updateFirst'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.form_action = '/login/userInfoChange/'
self.helper.add_input(Submit('submit', 'Submit'))
view:
#json_view
#csrf_exempt
def userInfoChange(request):
context = RequestContext(request)
if request.method == 'POST':
user = User.objects.get(username=request.user.username)
user_form = updateFirstName(request.POST)
print user_form
if user_form.is_valid():
user_form.save()
print "Valid Form"
return {'success': True}
html:
<form id="updateUsername">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-success" type="button" id="submit-username">Change</button>
</span>
<input type="text" class="form-control" id="id_firstname" placeholder="First Name: {{user.first_name}}">
<script type="text/javascript">
$('#submit-username').click(function(){
$.ajax({//begin
type: 'POST',
url: '/login/userInfoChange/',
data: $('#updateUsername').serialize(),
success: function(result){
console.log('updateUsername');
}
});
});
</script>
</div><br></form>
Model:
class UserProfile(models.Model):
user = models.OneToOneField(User)
confirmation_code = models.CharField(max_length=128)
reset_code = models.CharField(max_length=128)
address_lineOne = models.CharField(max_length=128)
address_lineTwo = models.CharField(max_length=128)
city = models.CharField(max_length=128)
State = models.CharField(max_length=128)
zipCode = models.CharField(max_length=10)
def __unicode__(self):
return self.user.username
Solution:
first_name = request.POST.get('first_name')
user.first_name = first_name
user.save()
Thanks ejey for resolving my csrf ajax issue.
If you are intending on posting using ajax you ought to use have a view method such as:
def some_ajax_view(request):
data = request.POST.copy()
if request.is_ajax() and request.POST:
...
...
You can get more information on how to process your ajax request without having to compromise on the csrf https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
Firstly, you didn't answer my question about the error message. This is a vital step in debugging, and it's very little use coming onto SO and asking questions without first finding the actual error and doing what you can to fix it yourself.
That said, you do have a huge obvious bug in your code. If the request is not a POST, and/or the form is not valid, what are you expecting to happen? Currently, your code just stops; so in either of those cases it will return None, which is an error: all views must return an HttpResponse. If you did look at the error message, that is probably what it would say.
You should ensure that your view at least returns an empty HttpResponse in either of those circumstances. More useful, though, would be a response that actually contains the errors from the form validation.
I have a class based view that displays a page and handles POST requests, like so:
class citywall (View):
def get(self, request, *args, **kwargs):
template_name = 'citywall.html'
city_slug = kwargs['city_slug']
context = self.get_context_data(city_slug)
return render_to_response(template_name, context, RequestContext(request))
def post(self, request, *args, **kwargs):
if request.is_ajax:
if request.POST.has_key('comment'):
#process comment
...
if request.POST.has_key('vote'):
#process vote
...
The problem is when I try to POST the form with AJAX, two requests are being sent. The Ajax request and then a regular POST request.
This is my comment form in html:
<form class="comment_form" data-id="{{post.id}}" method="POST" >{% csrf_token %}
<textarea rows = "1" name="comment" class="form-control" placeholder="Write a comment..." ></textarea>
<button type="submit" class="btn btn-info">Go!</button>
</form>
This is the jQuery code:
var comment_form = $('.comment_form')
comment_form.submit(function(){
var post_id = $(this).data('id');
var comment = $(this).find('textarea[name="comment"]').val();
$.ajax({
url: "",
dataType:"json",
type:"POST",
data:{
comment: comment,
post_id: post_id,
csrfmiddlewaretoken:'{{csrf_token}}',
},
success:function(json)
{
alert(json);
},
});
});
When I submit the form this is happening:
AJAX POST is made with its json data(post_id, comment, csrf).
Response for the AJAX post is received back in browser.
POST is made with html form data(comment and csrf).
why is a second POST being made?
I have several types of forms on the page for example. comment form, voting form, etc and I want to make all of them AJAX. Is it a good idea to implement all of them with the above mentioned method?
The second POST with your form data is sent because your submit event handler doesn't return False. If you want to prevent the form from submitting when clicking the submit button, you have to return False in the event handler. The event handler can also be told to prevent form submission by calling e.preventDefault() on the event object that is being passed to the submit handler.
The reason for this behaviour is that, by default, the event fired when a submit button is clicked submits the forms at the very end of the event handling chain. So what happens is that in your event handler you are send a AJAX POST call (which is asynchronous), and the very next moment the handler returns without prevent the event from form submission. This results in both AJAX and form being sent.
e.preventDefault() tells the event e to avoid doing the default action specific to this event, in this case form submission. With this added, when $.ajax finishes and all the handlers are done handling the event the default handler is checked if it's allowed to be run. Since you have prevented it, nothing happens.
The very same method can be used to, e.g. prevent the webpage following a link <a> when clicked.
I am trying to use the generic login view provided by Django. i want the registration and login form on the same page. Here is my urls.py
from django.conf.urls import patterns, include, url
from myApp.forms import UsersForm
urlpatterns = patterns('',
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'templates/login.html', 'authentication_form':UsersForm}),
)
and this is my login.html
<html>
<body>
<form method="post" action="">{% csrf_token %}
{{ authentication_form.first_name }} {{authentication_form.last_name }} <br>
{{ authentication_form.username }} {{ authentication_form.password }} <br>
<input type="submit" value="Register"/>
</form>
{% for field, error in form.errors.items %}
{% if forloop.counter == 1 %}
{{ error | striptags }}
{% endif %}
{% endfor %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">{% csrf_token%}
{{authentication_form.username.label_tag}}
{{authentication_form.username}}
{{authentication_form.password.label_tag}}
{{authentication_form.password}}
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</body>
</html>
When I run the server and go to 127.0.0.1, only the login and register Buttons show up, the actual fields do not (I can't type anything in anywhere on the page, there are just two buttons, one which says 'login' and another which says 'register'. There is just a blank white space where the actual fields should be. How come the box for username, password, first name and last name aren't showing up?
EDIT: when I change authentication_form.username to just form.username, it gives a template error saying
'WSGIRequest' object has no attribute 'get'
and the traceback to this is
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
140. response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
82. content = template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
140. return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
830. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render
87. output = force_text(output)
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text
99. s = s.__unicode__()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in __str__
411. return self.as_widget()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in as_widget
458. return widget.render(name, self.value(), attrs=attrs)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in value
494. self.data, self.form.initial.get(self.name, self.field.initial)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _data
480. return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in value_from_datadict
209. return data.get(name, None)
Exception Type: AttributeError at /
Exception Value: 'WSGIRequest' object has no attribute 'get'
My generic login view is just this
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.REQUEST.get(redirect_field_name, '')
if request.method == "POST":
form = authentication_form(data=request.POST)
if form.is_valid():
# Ensure the user-originating redirection url is safe.
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
# Okay, security check complete. Log the user in.
auth_login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect(redirect_to)
else:
form = authentication_form(request)
request.session.set_test_cookie()
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)
return TemplateResponse(request, template_name, context,
current_app=current_app)
I didn't make any changes to template_name (although I did specify template_name in my urls.py which can be seen above) or current_app. My form class in forms.py is this
class UsersForm(forms.ModelForm):
class Meta:
model = Users
widgets = {'password':forms.PasswordInput()}
def __init__(self, *args, **kwargs):
super( UsersForm, self ).__init__(*args, **kwargs)
self.fields[ 'first_name' ].widget.attrs[ 'placeholder' ]="First Name"
self.fields[ 'last_name' ].widget.attrs[ 'placeholder' ]="Last Name"
self.fields[ 'username' ].widget.attrs[ 'placeholder' ]="Username"
self.fields[ 'password' ].widget.attrs[ 'placeholder' ]="Password"
self.fields['first_name'].error_messages = {'required': 'Please enter your First Name.', 'max_length': 'Your First Name must be shorter than 50 characters.'}
self.fields['last_name'].error_messages = {'required': 'Please enter your Last Name.', 'max_length': 'Your last Name must be shorter than 50 characters.'}
self.fields['password'].error_messages = {'required': 'Please enter a Password.', 'max_length': 'Your Password must be shorter than 50 characters.', 'invalid': 'Your Password can only contain letters, numbers, underscores, and hyphens.'}
self.fields['username'].error_messages = {'required': 'Please enter a Username.', 'max_length': 'Your Username must be shorter than 50 characters.', 'invalid': 'Your Username can only contain letters, numbers, underscores, and hyphens.',}
def clean_date_of_birth_month(self):
data = self.cleaned_data.get('date_of_birth_month')
if data == 'Month':
raise forms.ValidationError('Please enter a correct Month for your date of birth.')
return data
def clean_date_of_birth_day(self):
data = self.cleaned_data.get('date_of_birth_day')
if data == 'Day':
raise forms.ValidationError('Please enter a correct Day for your date of birth.')
return data
def clean_date_of_birth_year(self):
data = self.cleaned_data.get('date_of_birth_year')
if data == 'Year':
raise forms.ValidationError('Please enter a correct Year for your date of birth.')
return data
It is a form created from an existing model, which is this
class Users(models.Model):
months = (
('Month','Month'), ('January', 'January'), ('February','February'), ('March','March'), ('April','April'), ('May','May'), ('June','June'),
('July','July'), ('August','August'), ('September','September'), ('October','October'), ('November','November'), ('December','December'),
)
days = (
('Day', 'Day'), ('1','1'), ('2','2'), ('3','3'), ('4','4'), ('5','5'), ('6','6'), ('7','7'), ('8','8'), ('9','9'), ('10','10'), ('11','11'),
('12','12'), ('13','13'), ('14','14'), ('15','15'), ('16','16'), ('17','17'), ('18','18'), ('19','19'), ('20','20'), ('21','21'), ('22','22'),
('23','23'), ('24','24'), ('25','25'), ('26','26'), ('27','27'), ('28','28'), ('29','29'), ('30','30'),('31','31'),
)
years = (
('Year','Year'), ('2013','2013'), ('2012','2012'), ('2011','2011'), ('2010','2010'), ('2009','2009'), ('2008','2008'),
)
alpha_field = RegexValidator(regex=r'^[a-zA-Z]+$', message='Name can only contain letters.')
user_id = models.AutoField(unique=True, primary_key=True)
first_name = models.CharField(max_length=50, validators=[alpha_field])
last_name = models.CharField(max_length=50, validators=[alpha_field])
username = models.SlugField(max_length=50, unique=True, error_messages={'unique': u'A user with that Username already exists. Please choose a different Username.'})
password = models.SlugField(max_length=50)
date_of_birth_month = models.CharField(verbose_name='', max_length=9, choices=months, default='Month')
date_of_birth_day = models.CharField(verbose_name='', max_length=3, choices=days, default='Day')
date_of_birth_year = models.CharField(verbose_name='', max_length=4, choices=years, default='Year')
Try with
{{form.username}}
because generic view names it that way:
form = authentication_form(request)
I don`t speak english well? but i have problem in Django.
I have models:
class Model1(models.Model):
model2 = models.ManyToManyField(Model2)
#...
class Model2(models.Model):
model3 = models.ForeignKey(Model3)
#...
class Model3(models.Model):
custom = models.CharField()
have view
def simple(request, simple_id):
if request.method == 'POST':
if request.is_ajax():
if 'delete' in request.POST:
id3 = request.POST.get('delete', '')
Model1.objects.get(id = simple_id).model2.filter(model3__id = id3).delete()
That is, when submitting a form with name = "delete" Ajax have removed all the objects belonging to Model2 with the same value of the field "model3"
Here's a piece of template:
<form action="" method="post" id="simple">{% csrf_token %}
<input type="submit" name="delete" id="simple_delete" value="">
</form>
the value passed from js:
$('.deletebutton').click(function(){
id = $(this).attr('data-id');
$('#simple_delete').attr('value', id);
$('#simple').ajaxForm();
$('#simple_delete').click();
});
Well, respectively plugin jquery.form.js also connected
The problem is this - if submission without ajax all is normal, it works ... and if with Ajax is an error such as incorrect int value ... How to make it work via Ajax?
try this
$('.deletebutton').click(function(){
id = $(this).attr('data-id');
$.ajax(function(){
type:"POST",
url :"/your_url/",
data:{
'id'=id,
}
}).done(function(result){
alert('your json object result render by view :'+result)
})
i think it work,
and i didnt get wat you are doing in i.e $('#simple_delete').click();
can you please describe about that
in view
obj = Model1.objects.get(id = simple_id)
model2.objects.filter(model3__id = id3).delete()
i just split single line query into two lines and if not working
use .select_related()
Is it possible in Lift web framework to create forms (and links) that react via AJAX, but also work without Javascript support? If so, how?
When I build the form using <lift:form.ajax>, the form's action is set to javascript:// so that it no longer submits without JS. If I build the form without explicit AJAX support, I don't know how to insert the AJAX functionality.
I suppose I could build a RESTful interface (we'll have to build that anyway) and write custom Javascript to submit the form through that. I would like to avoid code duplication, though: if it is possible to handle all three inputs (RESTful, traditional HTTP POST, AJAX) with the same code, that would be best.
Take a look at http://demo.liftweb.net/form_ajax
FormWithAjax.scala
class FormWithAjax extends StatefulSnippet {
private var firstName = ""
private var lastName = ""
private val from = S.referer openOr "/"
def dispatch = {
case _ => render _
}
def render(xhtml: NodeSeq): NodeSeq =
{
def validate() {
(firstName.length, lastName.length) match {
case (f, n) if f < 2 && n < 2 => S.error("First and last names too short")
case (f, _) if f < 2 => S.error("First name too short")
case (_, n) if n < 2 => S.error("Last name too short")
case _ => S.notice("Thanks!"); S.redirectTo(from)
}
}
bind( "form", xhtml,
"first" -> textAjaxTest(firstName, s => firstName = s, s => {S.notice("First name "+s); Noop}),
"last" -> textAjaxTest(lastName, s => lastName = s, s => {S.notice("Last name "+s); Noop}),
"submit" -> submit("Send", validate _)
)
}
form_ajax.html
<lift:surround with="default" at="content">
Enter your first and last name:<br>
<form class="lift:FormWithAjax?form=post">
First Name: <form:first></form:first>
Last Name: <form:last></form:last>
<form:submit></form:submit>
</form>
</lift:surround>
And this will work without javascript:
<form action="/form_ajax" method="post">
<input name="F1069091373793VHXH01" type="hidden" value="true">
First Name: <input value="" type="text" name="F1069091373788OVAAWQ" onblur="liftAjax.lift_ajaxHandler('F1069091373789N2AO0C=' + encodeURIComponent(this.value), null, null, null)">
Last Name: <input value="" type="text" name="F1069091373790VANYVT" onblur="liftAjax.lift_ajaxHandler('F1069091373791CJMQDY=' + encodeURIComponent(this.value), null, null, null)">
<input name="F1069091383792JGBYWE" type="submit" value="Send">
</form>
I dont know a lot about Lift so my answer focuses on alternate way to do it.
This is jQuery based and will do with AJAX when Javascript is usable and traditional POST if there is no Javascript support enabled.
Form:
<form id="ajaxform" action="formhandler.php" method="post" enctype="multipart/form-data" >
<input name="firstname" type="text" />
<input name="email" type="email" />
<input name="accept" type="submit" value="Send" />
</form>
<div id="result"></div>
JS:
note: jQuery $.ajax() sends as application/x-www-form-urlencoded by default, it may be good to set form enctype="application/x-www-form-urlencoded" too.
$("#ajaxform").submit(function(e){
// Alternative way to prevent default action:
e.preventDefault();
$.ajax({
type: 'POST',
url: 'formhandler.php',
// Add method=ajax so in server side we can check if ajax is used instead of traditional post:
data: $("#ajaxform").serialize()+"&method=ajax",
success: function(data){ // formhandler.php returned some data:
// Place returned data <div id="result">here</div>
$("#result").html(data);
}
});
// Prevent default action (reposting form without ajax):
return false;
});
Server side (PHP)
<?php
if (isset($_POST['method']) && $_POST['method'] == 'ajax') {
// AJAX is used this time, only #result div is updating in this case.
} else {
// Traditional POST is used to send data, whole page is reloading. Maybe send <html><head>... etc.
}
?>
What About REST then?
This is something you should decide to use or to not use, it is not something to support as alternate to other methods (ajax, traditional) but more something integrate within other methods.
Of course you can always enable or disable REST feature.
You can always make form method="POST/GET/PUT/DELETE" and ajax call RESTful:
...
$.ajax({
type: 'PUT',
url: 'formhandler.php',
...
...
$.ajax({
type: 'DELETE',
url: 'formhandler.php',
...
But REST asks us to use XML, JSON, ... for requests too
Well, that is not well supported by browsers (without Javascript) but $.ajax() uses application/x-www-form-urlencoded as default encoding.
Ofcourse, with Javascript one can always convert data container to XML or JSON ...
Here's how it can be done with jQuery, JSON object:
/* This is function that converts elements to JSON object,
* $.fn. is used to add new jQuery plugin serializeObject() */
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
But I want one AJAX call that does everything:
You are right, computers should do our work. It's what they are designed for.
So, another thing that needs to be done is to check what http method our original html form wants to use and adapt it to send ajax requests with same method that would be used without javascript support.
This is modified version from under JS: heading used earlier:
...
// Alternative way to prevent default action:
e.preventDefault();
// Find out what is method that form wants to use and clone it:
var restmethod = $('#ajaxform').attr('method');
// Put form data inside JSON object:
var data = $('#orderform').serializeObject();
// Add method=ajax so in server side we can check if ajax is used instead of traditional post:
data.method = 'ajax';
$.ajax({
type: restmethod, // Use method="delete" for ajax if so defined in <form ...>
url: 'formhandler.php',
data: data, // data is already serialized as JSON object
...
Now, our AJAX handler sends data as JSON object using method (post|get|put|delete) that is defined at <form method="put" ...>, if form method changes then our ajax handler will adapt changes too.
That's all, some code tested and is actually in use, some is not tested at all but should work.