Python Forum

Full Version: context Django
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all
I have 3 html pages
index.html - It contains form
result.html - when you click on submit in index.html it will display result.html page in this page we have context i.e {{result}}
I want to show this context in database
when we click on submit in index.html the data will store in database for this i created si class in models.py
Please anyone can help me.
yes can you show me your code please?
(Mar-22-2024, 10:47 AM)Sowmya Wrote: [ -> ]Hi all
I have 3 html pages
index.html - It contains form
result.html - when you click on submit in index.html it will display result.html page in this page we have context i.e {{result}}
I want to show this context in database
when we click on submit in index.html the data will store in database for this i created si class in models.py
Please anyone can help me.


Hey! I think, first, make sure you have a form in your index.html that sends data to your backend. In your Django views, you’ll handle the form submission and save the data to your database.

You should have a form like this:
<form action="{% url 'save_data' %}" method="POST">
    {% csrf_token %}
    <input type="text" name="data" placeholder="Enter data">
    <button type="submit">Submit</button>
</form>
In your models.py, create a model to store the data:
from django.db import models

class DataModel(models.Model):
    data = models.CharField(max_length=255)
In your views.py, handle the form submission and save the data.
from django.shortcuts import render, redirect
from .models import DataModel

def save_data(request):
    if request.method == "POST":
        data = request.POST['data']
        DataModel.objects.create(data=data)
        return redirect('result', result=data)
    return render(request, 'index.html')

def result(request, result):
    return render(request, 'result.html', {'result': result})
In your urls.py, set up the URLs for your views.
from django.urls import path
from . import views

urlpatterns = [
    path('', views.save_data, name='save_data'),
    path('result/<str:result>/', views.result, name='result'),
]
Finally, in your result.html, display the result:
<h1>Result: {{ result }}</h1>
When you submit the form in index.html, it should hit the save_data view, save the data to the database, and redirect to result.html with the saved data. Hope this helps!
- Create a Django Model
In your models.py file, create a model to store the form data:
This model has two fields: result to store the form data, and created_at to store the timestamp when the data was saved.
- Create a Django View
In your views.py file, create a view to handle the form submission and save the data to the database
- Update your URLs
In your urls.py file, add the routes for the index and result views:
- Update your HTML templates
In your index.html template, add a form that submits the result value
In your result.html template, display the result value:
Now, when a user submits the form on the index.html page, the form data will be saved to the database, and the result.html page will display the latest saved value.
When will admin answer these questions? Doh