Django Project Name: DICTIONARY_FORMS
Application Name: App_01
Python Interpreter Version: 3.8.1
Django Version: 3.0.2
Python & Django Environment: Virtual Environment
Database Client Version: MySQL Client 1.4.6
1. How to configure HTML Form Labels as Dictionary Keys that are defined in forms.py through views.py?
2. How to assign the end-user responses provided in the HTML Form Fields into the respective Dictionary Values in forms.py?
3. How to control the form defined in the forms.py through views.py in this particular regard?
4. Ultimately, how to store them in my database after form validation, but as of now i did not customize any validations so forth?
Note: Upto now, i did not define any models in my models.py, because of the above raised issue.
# Sample Code of My requirement is as follows:-
# forms.py
from django import forms
class MyForm(forms.Form):
member_1 = {'key_1': forms.CharField(max_length=10, required=True),
'key_2': {'A': forms.CharField(max_length=10, required=True), 'B': forms.CharField(max_length=10, required=True)},
'key_3': forms.IntegerField(required=True)}
# forms.py ended
# views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .forms import MyForm
# Configuring MyForm in forms.py as HTML Form to display on the User Interface.
def form_view():
if request.POST == True:
form = MyForm(request.POST)
form.save(commit=True)
return HttpResponseRedirect('form/')
form = MyForm()
return render(request, 'form1.html', {'form': form})
# views.py ended
<!-- form1.html -->
{% extends base.html %}
<!-- base.html is as per generated by django standards in addition to that i extend it with <form> tag with submit reset buttons in the body section-->
<!--<a href='/form1'>form1</a> is also added in base.html-->
{% block content %}
{% block form %}
{% csrf_token %}
{{ form.as_div }}
{% endblock %}
{% endblock %}
<!--form1.html ended-->
# urls.py
from . import views
# extending the standard urlpatterns generated by Django with the following:-
urlpatterns = [path('form1', views.form_view), ]
# urls.py ended
# models.py
# yet, I doesn't write code to store the values entered by the end-user/client.
# models.py ended
Kindly respond me depends on the information provided in the attachment.
-- Thanks and Regards
V.K. Vanama
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1f51d69b-6d2d-4604-abc7-5036995dce45%40googlegroups.com.
No comments:
Post a Comment