Django – handling form without the form model
Question:
I wish to handle a form I created with HTML in django, but I can’t find the correct way to get the value from the inputs.
This is my HTML form:
<form action="" class="form-inline" method="post">
{% csrf_token %}
<!-- malfunction description -->
<label class="requiredField" for="malfunctionDescription">תאור
התקלה</label>
<br/>
<textarea class="form-control" style="width: 100%; margin-bottom: 3px"
id="malfunctionDescription"
name="malfunctionDescription"
rows="5">
</textarea>
</form>
And this is my view.py which is unfortunately empty:
def index(request):
error = ''
if request.method == 'POST':
status = request.POST['status']
rank = request.POST['rank']
opener = request.POST['MalfunctionOpener']
handler = request.POST['malfunctionHandler']
system = request.POST['system']
unit = request.POST['unit']
opening_date = request.POST['openingdate']
closing_date = request.POST['closingdate']
description = request.POST['malfunctionDescription']
solution = request.POST['malfunctionSolution']
summary = request.POST['malfunctionSummary']
find_description = Malfunction.objects.filter(description=description)
if find_description:
error = 'This malfunction is already stored in the data base'
else:
Malfunction.objects.create(status=status, rank=rank, opener=opener, handler=handler, system=system,
unit=unit, openingDate=opening_date, closingDate=closing_date, solution=solution,
summary=summary, description=description)
else:
error = 'Something went wrong'
return render(request, 'resources-management/home.html')
The main goal for me is to get this information from the form and create a new object that will push it to the data base.
If I understand correctly to create this object I need to do something like:
Object_name.object.create(information=information)
But I don’t know how to get the information, I need something like request.form[‘name’] in Flask
Thanks!
—- EDIT 1 – url.py —-
This is the main url.py
urlpatterns = [
url(r'^', include('elbit_ils.urls')),
url(r'^admin/', admin.site.urls),
url(r'^resources-management/', include('resources_management.urls')),
url(r'^registration-login/', include('registration_login.urls')),
url(r'^contact/', include('contact.urls')),
]
This is the application url.py
urlpatterns = [
url(r'^$', IndexView.as_view(), name="my_list"),
url(r'^(?P<pk>d+)$', DetailView.as_view(model=Malfunction, template_name="resources-management/malfunction.html")),
]
— edit 2 : indexview —
class IndexView(CreateView):
context_object_name = 'my_list'
template_name = 'resources-management/home.html'
queryset = Malfunction.objects.all()
fields = '__all__'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['contacts'] = Contact.objects.all().order_by("firstname")
context['malfunctions'] = Malfunction.objects.all().order_by("-openingDate")
context['systems'] = System.objects.all().order_by("systemname")
context['units'] = Unit.objects.all().order_by("unitname")
# And so on for more models
return context
Answers:
If you have a form like
<form action="" method="post">
<input type="text" name="username" />
<input type="submit" />
</form>
Note the name
attribute in input
element. Then
In django
, you will get data in request.POST
.So you can do
request.POST['username']
You should be able to access the data in the following way
def index(request):
# All the post data is stored in `request.POST`
desc = request.POST["malfunctionDescription"]
You can look at how to use Forms in the docs, to learn the best way to validate the data you receive.
For Django version 4.0.1(Basically secured versions) you have to use csrf_token.
Example:
In my_app/templates/my_app/index.html
<form action="" method="POST">
{% csrf_token %}
<input type="text" name="x" placeholder="x values" title="Enter x values">
<input type="submit" value="Enter">
</form>
In my_app/views.py
def index(request):
if request.method == 'POST':
x_val = request.POST['x']
return HttpResponse(x_val)
return render(request,'plots/index.html')
I wish to handle a form I created with HTML in django, but I can’t find the correct way to get the value from the inputs.
This is my HTML form:
<form action="" class="form-inline" method="post">
{% csrf_token %}
<!-- malfunction description -->
<label class="requiredField" for="malfunctionDescription">תאור
התקלה</label>
<br/>
<textarea class="form-control" style="width: 100%; margin-bottom: 3px"
id="malfunctionDescription"
name="malfunctionDescription"
rows="5">
</textarea>
</form>
And this is my view.py which is unfortunately empty:
def index(request):
error = ''
if request.method == 'POST':
status = request.POST['status']
rank = request.POST['rank']
opener = request.POST['MalfunctionOpener']
handler = request.POST['malfunctionHandler']
system = request.POST['system']
unit = request.POST['unit']
opening_date = request.POST['openingdate']
closing_date = request.POST['closingdate']
description = request.POST['malfunctionDescription']
solution = request.POST['malfunctionSolution']
summary = request.POST['malfunctionSummary']
find_description = Malfunction.objects.filter(description=description)
if find_description:
error = 'This malfunction is already stored in the data base'
else:
Malfunction.objects.create(status=status, rank=rank, opener=opener, handler=handler, system=system,
unit=unit, openingDate=opening_date, closingDate=closing_date, solution=solution,
summary=summary, description=description)
else:
error = 'Something went wrong'
return render(request, 'resources-management/home.html')
The main goal for me is to get this information from the form and create a new object that will push it to the data base.
If I understand correctly to create this object I need to do something like:
Object_name.object.create(information=information)
But I don’t know how to get the information, I need something like request.form[‘name’] in Flask
Thanks!
—- EDIT 1 – url.py —-
This is the main url.py
urlpatterns = [
url(r'^', include('elbit_ils.urls')),
url(r'^admin/', admin.site.urls),
url(r'^resources-management/', include('resources_management.urls')),
url(r'^registration-login/', include('registration_login.urls')),
url(r'^contact/', include('contact.urls')),
]
This is the application url.py
urlpatterns = [
url(r'^$', IndexView.as_view(), name="my_list"),
url(r'^(?P<pk>d+)$', DetailView.as_view(model=Malfunction, template_name="resources-management/malfunction.html")),
]
— edit 2 : indexview —
class IndexView(CreateView):
context_object_name = 'my_list'
template_name = 'resources-management/home.html'
queryset = Malfunction.objects.all()
fields = '__all__'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['contacts'] = Contact.objects.all().order_by("firstname")
context['malfunctions'] = Malfunction.objects.all().order_by("-openingDate")
context['systems'] = System.objects.all().order_by("systemname")
context['units'] = Unit.objects.all().order_by("unitname")
# And so on for more models
return context
If you have a form like
<form action="" method="post">
<input type="text" name="username" />
<input type="submit" />
</form>
Note the name
attribute in input
element. Then
In django
, you will get data in request.POST
.So you can do
request.POST['username']
You should be able to access the data in the following way
def index(request):
# All the post data is stored in `request.POST`
desc = request.POST["malfunctionDescription"]
You can look at how to use Forms in the docs, to learn the best way to validate the data you receive.
For Django version 4.0.1(Basically secured versions) you have to use csrf_token.
Example:
In my_app/templates/my_app/index.html
<form action="" method="POST">
{% csrf_token %}
<input type="text" name="x" placeholder="x values" title="Enter x values">
<input type="submit" value="Enter">
</form>
In my_app/views.py
def index(request):
if request.method == 'POST':
x_val = request.POST['x']
return HttpResponse(x_val)
return render(request,'plots/index.html')