In forms
from django import forms
class DataForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
Create a Django template file
<!DOCTYPE html>
<html>
<head>
<title>Store Data Example</title>
</head>
<body>
<h1>Store Data Example</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="text" name="additional_field" placeholder="Additional Field">
<input type="submit" value="Submit">
</form>
</body>
</html>
Create a Django view in data_app/views.py:
from django.shortcuts import render
from data_app.forms import DataForm
def data_view(request):
if request.method == 'POST':
form = DataForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
additional_field = request.POST.get('additional_field')
# Print all the request data
print('Name:', name)
print('Email:', email)
print('Additional Field:', additional_field)
# You can save the data to a database or perform any other desired actions here
return render(request, 'data_app/success.html')
else:
form = DataForm()
return render(request, 'data_app/data.html', {'form': form})
Create a Django template file
<html>
<head>
<title>Success</title>
</head>
<body>
<h1>Success</h1>
<p>Data submitted successfully!</p>
</body>
</html>
Make sure to adjust the file paths and imports based on your project structure. With this
Another Example
def data_view(request):
if request.method == 'POST':
form = DataForm(request.POST)
if form.is_valid():
data = {
'name': form.cleaned_data['name'],
'email': form.cleaned_data['email'],
'additional_field': request.POST.get('additional_field'),
}
# Print all the request data
print('Request Data:', data)
# You can save the data to a database or perform any other desired actions here
return render(request, 'data_app/success.html')
else:
form = DataForm()
return render(request, 'data_app/data.html', {'form': form})
Another Example
In forms
from django import forms
from .models import Employee
from .models import Category
class ChatForm(forms.Form):
message = forms.CharField(label='Message',widget=forms.Textarea(attrs={'id': 'my-textarea','class': 'centered-textarea',
'style': 'height: 300px; width: 500px; margin-left: 2%;'}))
in html file
<form method="post">
{% csrf_token %}
<div class="row">
<div class="col p-3"></div>
<div class="col p-3"> {{ form.as_p }}
<input type="submit" value="Send"></div>
<div class="col p-3"> <div class="form-group mt-4">
<label for="temperature">Temperature:</label>
<input type="range" min="0" max="1" step="0.1" value="0.0" name="temperature" id="temperature" class="temperature-slider">
<span id="temperature_value">0.0</span>
</div>
<div class="form-group mt-4">
<label for="max_tokens">Max Tokens:</label>
<input type="range" min="1.0" max="4000" step="1" value="0.0" name="max_tokens" id="max_tokens" class="max_tokens-slider">
<span id="max_tokens_value">0.0</span>
</div>
<div class="form-group mt-4">
<label for="top_p">Top P:</label>
<input type="range" min="0" max="1" step="0.1" value="0.0" name="top_p" id="top_p" class="top_p_slider">
<span id="top_p_value">0.0</span>
</div>
<div class="form-group mt-4">
<label for="fre_p">Frequency penalty</label>
<input type="range" min="0" max="1" step="0.1" value="0.0" name="frequency_penalty" id="frequency_penalty" class="frequency-penalty-slider">
<span id="frequency_penalty_value">0.0</span>
</div>
<div class="form-group mt-4">
<label for="pre_p">Presence penalty</label>
<input type="range" min="0" max="1" step="0.1" value="0.0" name="presence_penalty" id="presence_penalty" class="presence-penalty-slider">
<span id="presence_penalty_value">0.0</span>
</div>
</div>
</div>
</form>
in view file
print in array
output
print in array
Top comments (0)