view option in Django - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: view option in Django (/thread-41810.html) |
view option in Django - Sowmya - Mar-22-2024 Hi all I am creating a django project . I have 3 html pages Index.html - it contains form result.html - it will display a report when i click on submit in index.html table.html - It contains a database data from index.html . I have delete & view option in my table when click on view i want to display result.html page for different entries. what type of code should i use can anyone please explain. RE: view option in Django - SULMAN - Mar-29-2024 (Mar-22-2024, 10:54 AM)Sowmya Wrote: Hi allTo achieve the described functionality in your Django project, you would need to implement the following: 1-URL Configuration: Define URL patterns in your Django project's urls.py file to map the URLs to corresponding views. 2-Views: Create view functions in your Django application's views.py file to handle the logic for rendering each HTML page and processing form submissions. 3-Templates: Develop HTML templates for each page (index.html, result.html, and table.html) in the appropriate directory within your Django application's templates folder. 4-Forms: Define a Django form class in your application's forms.py file to handle form validation and processing. Here's a brief overview of how you can structure your code: urls.py: python Copy code from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), path('result/', views.result, name='result'), path('table/', views.table, name='table'), # Add other URL patterns as needed ] views.py: python Copy code from django.shortcuts import render, redirect from .forms import YourFormClass def index(request): if request.method == 'POST': form = YourFormClass(request.POST) if form.is_valid(): # Process form data # Redirect to result page return redirect('result') else: form = YourFormClass() return render(request, 'index.html', {'form': form}) def result(request): # Logic to generate report data return render(request, 'result.html', {'report_data': report_data}) def table(request): # Logic to retrieve and display data from the database return render(request, 'table.html', {'database_data': database_data}) forms.py: python Copy code from django import forms class YourFormClass(forms.Form): # Define form fields here In your table.html, you would need to include buttons or links to view and delete entries, and when clicking on "view," you can navigate to the result.html page with the corresponding data. Ensure to replace placeholders like YourFormClass, report_data, and database_data with your actual form class, report data, and database data respectively. Also, customize the views and templates according to your specific requirements. |