CSRF verification failed. Request aborted. Django 2.0 - python

def order_view(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('Order Submitted')
else:
form = OrderForm()
return render_to_response('home/order.html', {'form': form})
order_view function in views.py
<form class="form form-table" method="post">
{% csrf_token %}
{{ form|crispy }}
<input class="btn br-green" type="submit" value="Submit"/>
</form>
There is still a CSRF error in it. Have tried most of the solution but they are not working.Have tried adding RequestContext(request) as well.

HttpResponseRedirect takes a url. I don't think 'Order Submitted' is.
Try
def order_view(request):
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
form.save()
else:
form = OrderForm()
return render_to_response('home/order.html', {'form': form})
If this works then you're sorted and use django.messages to provide the message to your user.

Related

Django form does not work or CSRF_token aborted

Hello I am making Django App. I have a problem with my login form. Whenever I want to login,
form does not do anything or it throws csrf token error.
Views:
def loginView(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f"You successfully logged in {username}")
return redirect('home-page')
else:
form = AuthenticationForm()
return render(request, 'shop/login.html', {'form': form})
HTML TEMPLATE:
{% extends 'shop/base.html' %}
{% block content %}
<div class = "form-container">
<form class="form" method="POST">{% csrf_token %}
<label for="username">Email:</label>
{{form.username}}
<label for="password">Passoword:</label>
{{form.password}}
<button type="submit">Login</button>
</form>
</div>
{% endblock content %}
you have to check whether user existed or not in db if yes then login and redirect if not throw error or redirect on other page
def my_login(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
user = authenticate(username=username, password=password)
if user:
login(request, user)
return redirect('path')
else:
return redirect('path')
else:
return redirect('path')
else:
form = AuthenticationForm()
return render(request, "shop/login.html", {'form': form})
still get error add the complete traceback & i will also suggest you to read the full documentation of django https://docs.djangoproject.com/en/3.2/ref/contrib/auth/

Django Form Not Saving the Data

I've imported this from django.contrib.auth.forms import UserCreationForm
used csrf_token in the form but still when I hit submit the page reloads but doesn't save the data to database.
def signup(req):
if req.method == 'POST':
form = UserCreationForm(req.POST)
if form.is_valid():
form.save()
form = UserCreationForm()
reg_con={
'regform': form
}
return render(req, 'signup.html', reg_con)
form
<form action="." method="POST">
{% csrf_token %}
{{ regform.as_ul }}
<input type="submit" value="Sign Up">
</form>
This is normally because something is wrong with your form the problem is however that you each time construct a new form, and you thus can not see what went wrong. You should only create a new form in case it is a GET request, so:
def signup(req):
if req.method == 'POST':
form = UserCreationForm(req.POST)
if form.is_valid():
form.save()
else:
form = UserCreationForm()
reg_con={
'regform': form
}
return render(req, 'signup.html', reg_con)
Try removing the action attribute from your form tag. Also don't forget to redirect after calling form.save()

django form view show error or not

I'm making a little personal project using Django framework and I get one question while making login view with django form.
I was struggled to show form error messages in my template, and I found a cause in my view.
This is view that showing error message
def login_view(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
form.login(request)
return redirect('/')
else:
form = LoginForm()
context = {
'form': form,
}
return render(request, 'member/login.html', context=context)
another view that dosen't showing error message
def login_view(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
form.login(request)
return redirect('/')
form = LoginForm()
context = {
'form': form,
}
return render(request, 'member/login.html', context=context)
and this is my template
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
{{ form.username}}
{{ form.password }}
{{ form.non_field_errors }}
<button id="login-btn" class="btn btn-default" type="submit">login</button>
The difference is just using elsephrase or not in view.
I think whether using elsephrase or not, there two views have logically same result... I don't understand difference of those two views.
Is there any clue to understand the differece of those two views?..
Thanks
You're overwriting the POST form by defining the form at the end. Load the blank form first
def login_view(request):
form = LoginForm()
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
form.login(request)
return redirect('/')
context = {
'form': form,
}
return render(request, 'member/login.html', context=context)

Django form isn't rendering at all

I'm trying to make a non model form that just gets input text for a chat like interface.
views.py
def get_input(request):
if request.method == 'POST':
form = inputForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = inputForm()
return render(request, 'index.html', {'form': form})
def shelley_test(request):
form = inputForm()
return render(request, 'shelley_test.html')
form.py
from django import forms
class inputForm(forms.Form):
input = forms.CharField(label='input field')
shelley_test.html
<form action="/get_input/" method="get">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
please please help. I'm new at django and stumped :(
You're not sending the form to the context in your shelley_test method - see the difference in the render line compared with get_input.
Note though you don't need shelley_test at all: just go straight to /get_input/ to see the empty form.

Django form not visible

Only the button is visible and headings are visible cannot see CharField
forms.py
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from compare.forms import NameForm
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
index.htm
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
You should give the if form.is_valid() part, one more tab.
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from compare.forms import NameForm
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
I'm not yet allowed to add comments to your question, but you did notice, that your view renders name.html while you're providing the code for index.htm.
Basically your code seems valid, while it can be further optimized:
def get_name(request):
form = NameForm(request.POST or None)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
return render(request, 'name.html', {'form': form})
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
This should work by adding editing {{form.as_p}}

Categories