Django forms Forbidden (403) error - django-forms

I am getting the below error when i click on submit button of my dummy form
Forbidden (403)
CSRF verification failed. Request aborted
My views.py(have done the required imports above) looks like this:
def search(request):
errors=[]
if request.method=="POST":
if not request.POST['name']:
errors.append('Name field is empty')
if not request.POST['subject']:
errors.append('Subject field is empty')
if not request.POST['age']:
errors.append('Age field is empty')
if not errors:
thank()
return render_to_response('search.html',{'errors':errors},context_instance=RequestContext(request))
def thank(search):
return HttpResponse('<html><p>Post done successfully</p></html>')
My search.html is :
<form method="post" action='/search/'>
<p>Name: <input type="text" name="name"/></p>
<p>Subject <input type="text" name="subject"/></p>
<p>Age: <input type="text" name="age"/></p>
<input type="submit" value="Hit Me!!"/>
</form>
</body>
</html>
Someone please let me know,how can i overcome this error?

Well,
1 . Add 'django.core.context_processors.csrf' to TEMPLATE_CONTEXT_PROCESSORS settings in settings.py.
2. Modify your forms like this,
<form method="post" action='/search/'>
{% csrf_token %}
<p>Name: <input type="text" name="name"/></p>
<p>Subject <input type="text" name="subject"/></p>
<p>Age: <input type="text" name="age"/></p>
<input type="submit" value="Hit Me!!"/>
</form>

I was going to say, I don't see a {% csrf_token %} between your <form></form> tags. That will cause CSRF verification to fail. The above poster beat me to it.

Related

MJML converts links in email clients to mjt.lu

Laravel-mix-mjml plugin converts href links. All links work fine except one link which is a form with post request. It doesn't pass the parameters. In the browser the links are not converted. How can I fix this? Thanks
<mj-text>
<form action="https://www.carsale.com/login" method="post" target="_blank">
<input type="hidden" name="customerId" value="{{ $inquiry->customer->customer_id ?? '' }}">
<input type="hidden" name="authToken" value="{{ $inquiry->customer->auth_token ?? '' }}">
<button type="submit" name="submit">Lets go</button>
</form>
Lets go
</mj-text>

Laravel same URL with 2 method

I am using same URL (Get and Post ) but on submit button it using the get method not the post .
my is below :
<form action="{{route('document.update', $document->id)}}" method="POST"
enctype="multipart/form-data" class="form-action">
<input type="hidden" name="flag" class="flag" value="0">
#csrf
#method('PATCH')
<button type="submit" class="btn btn-md btn-primary mr-1 btn-submit"
data-text="{{ __('save-all-changes') }}" name="post_action"
role="button" value="post_comment">{{ __('post-a-comment') }}
I need to take the post not the get , any idea?
I found the problem , since i disabled all input using
$("input").prop('disabled', true);
this has disable the _token and _method input , Change the Jquery sentence to be :
$("input").not( ':input[type=hidden]').prop('disabled', true);
this fix the problem .

can't get old() to work in laravel

in my blade i have 2 radio inputs
<form class="" action="{{route('admin.album.search')}}" method="post">
<input id="slug" type="radio" name="search" value="slug" >
<input id="id" type="radio" name="search" value="id">
in the same blade file I test to see old value of radio input
{{ old('search')}}
When I select first radio button and submit the form and page reloads I expect that old will be selected radio button value but nothing comes out. What could I be doing wrong?
Try this:
<form class="" action="{{route('admin.album.search')}}" method="post">
{{ csrf_field() }}
<input id="slug"
type="radio"
name="search"
value="slug"
{{ (old('search') == 'slug') ? 'checked': '' }}>
<input id="id"
type="radio"
name="search"
value="id"
{{ (old('search') == 'id') ? 'checked': '' }}>
N.T. If you submit post form request you must be specify csrf_field(). Otherwise it gives you token mismatch exception.

laravel post request results in an error that method not allowed

<form id="login-form" action="brand-dashboard" method="post">
<span class"email">email</span>
<input type="email" name="email">
<span class"email">password</span>
<input type="password" name="password"><br><br>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit" value="login">
</form>
this is in my view page..then in my route..
Route::get('/brand-dashboard','BrandsController#auth_brand_admin');
in my Brands controller..i use the method like
public function auth_brand_admin()
{
return ('sample text');
}
but i got error when submiting the form ..the error is..
MethodNotAllowedHttpException in RouteCollection.php
Change your code to this
Route::post('/brand-dashboard','BrandsController#auth_brand_admin');
It's because you register route with GET method but send POST request.

POST method not working in laravel 5

**THIS is my code for form creation and action is set to post but it is showing error "tokenmismatchexception in verifycsrftoken.php line 53 laravel 5.1";
<html>
<body>
<form action="/abc" method="post">
<b style="padding-right:110px;">Patient's Name:</b>
<input type="text" name="fullname" required><br><br>
<b style="padding-right:50px;">Required Blood Group:</b>
<option>A+</option>
<select>
<option>B+</option>
<option>AB+</option>
<option>O+</option>
<option>A-</option>
<option>B-</option>
<option>AB-</option>
<option>O-</option>
</select><br><br>
<input
type="submit"
style="position:absolute;right:170px;"
name="submit"
value="Request">
</form>
</body>
</html>
You need to include the CSRF token in your form data. Add:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
right after your opening form tag.

Resources