I don't know why my create function worked before, but now is not working. I see only "It's not POST, it's GET". I have tried to delete something, for example: from def index(request): email = str(request.GET.get('autorization_email'))
and from def create(request)
type_values = Type.objects.all()
model_values = Model.objects.all()
equipment_values = Equipment.objects.all()
It didn't helped
url.py
urlpatterns = [
path('', views.autorization, name='autorization'),
path('home/', views.index, name='home'),
path('return_equpment/', views.return_equpment, name='return_equpment'),
path('create/', views.create, name='create'),
]
create.html
{% block content %}
<form method="post" action="{% url 'create' %}">
{% csrf_token %}
...
<button type="submit" >Save</button><br>
<span>{{ error }}</span><br>
</form>
{% endblock%}
views.py
from django.shortcuts import render, redirect
from .models import Booking, Type, Model, Equipment
from .forms import BookingForm
error = ''
email = ''
def index(request):
global email
global error
email = str(request.GET.get('autorization_email'))
context = {
'title': 'Main page',
'error': error,
'index_email': email
}
return render(request, 'main/index.html', context)
def create(request):
type_values = Type.objects.all()
model_values = Model.objects.all()
equipment_values = Equipment.objects.all()
form2 = BookingForm()
global error
global email
context = {
'form': form2,
'error': error,
'type_values': type_values,
'model_values': model_values,
'equipment_values': equipment_values,
'create_email': email
}
if request.method == 'POST':
form2 = BookingForm(request.POST)
form2.email = email
form2.type = request.POST.get('type')
form2.model = request.POST.get('model')
form2.number = request.POST.get('number')
if form2.is_valid():
form2.save()
return redirect('autorization')
else:
error = 'Form incorrect'
else:
error = 'It's not POST, it's ' request.method
return render(request, 'main/create.html', context)
CodePudding user response:
I’m not sure but I think
if request.method == 'POST':
Should be in the first of function
def create(request):
if request.method == 'POST':
Because Django by default make a function as get request and you make some operations before you specific the request type
CodePudding user response:
from django.shortcuts import render, redirect
from .models import Booking, Type, Model, Equipment
from .forms import BookingForm
error = ''
email = ''
def index(request):
global email
global error
email = str(request.GET.get('autorization_email'))
context = {
'title': 'Main page',
'error': error,
'index_email': email
}
return render(request, 'main/index.html', context)
def create(request):
if request.method == 'POST':
form2 = BookingForm(request.POST)
form2.email = email
form2.type = request.POST.get('type')
form2.model = request.POST.get('model')
form2.number = request.POST.get('number')
if form2.is_valid():
form2.save()
return redirect('autorization')
else:
error = 'Form incorrect'
else:
type_values = Type.objects.all()
model_values = Model.objects.all()
equipment_values = Equipment.objects.all()
form2 = BookingForm()
global error
global email
context = {
'form': form2,
'error': error,
'type_values': type_values,
'model_values': model_values,
'equipment_values': equipment_values,
'create_email': email
}
error = 'It's not POST, it's ' request.method
return render(request, 'main/create.html', context)