# models.py
from django.db import models
class YourModel(models.Model):
field1 = models.CharField(max_length=100)
field2 = models.IntegerField()
# Add other fields as needed
# forms.py
from django import forms
from .models import YourModel
class YourModelForm(forms.ModelForm):
class Meta:
model = YourModel
fields = '__all__' # Use specific fields if needed
# views.py
from django.shortcuts import render
from .models import YourModel
from .forms import YourModelForm
def your_view(request):
if request.method == 'POST':
form = YourModelForm(request.POST)
if form.is_valid():
form.save()
# Add any further processing or redirection as needed
else:
form = YourModelForm()
# Retrieve data from the database
data_from_database = YourModel.objects.all()
return render(request, 'your_template.html', {'form': form, 'data_from_database': data_from_database})
<!-- your_template.html -->
<form method="post" action="{% url 'your_view_name' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
<!-- Display data from the database -->
{% if data_from_database %}
<ul>
{% for item in data_from_database %}
<li>{{ item.field1 }} - {{ item.field2 }}</li>
<!-- Add other fields as needed -->
{% endfor %}
</ul>
{% else %}
<p>No data available.</p>
{% endif %}
# urls.py
from django.urls import path
from .views import your_view
urlpatterns = [
path('your_url/', your_view, name='your_view_name'),
# Add other URLs as needed
]