Selaa lähdekoodia

Opciones de editar y borrar documentos

Celestino Rey 5 kuukautta sitten
vanhempi
sitoutus
4aa08455cc

+ 16 - 0
templates/textdocs/document_confirm_delete.html

@@ -0,0 +1,16 @@
+{% extends 'textdocs/base.html' %}
+{% block content %}
+    <div class="card">
+        <div class="card-body">
+            <h2 class="card-title">Confirm Deletion</h2>
+            <p>Are you sure you want to delete the document titled "<strong>{{ document.title }}</strong>"?</p>
+            <p>This action cannot be undone.</p>
+            
+            <form method="post">
+                {% csrf_token %}
+                <button type="submit" class="btn btn-danger">Yes, Delete</button>
+                <a href="{{ document.get_absolute_url }}" class="btn btn-secondary">Cancel</a>
+            </form>
+        </div>
+    </div>
+{% endblock %}

+ 3 - 0
templates/textdocs/document_detail.html

@@ -3,6 +3,9 @@
     <div class="card">
         <div class="card-header d-flex justify-content-between align-items-center">
             <h2>{{ document.title }}</h2>
+            <a href="{% url 'document_update' pk=document.pk %}" class="btn btn-secondary">Edit</a>
+            <a href="{% url 'document_delete' pk=document.pk %}" class="btn btn-danger">Delete</a>
+
             <a href="{% url 'generate_pdf' pk=document.pk %}" class="btn btn-info">Export to PDF</a>
         </div>
         <div class="card-body">

+ 2 - 1
templates/textdocs/document_form.html

@@ -1,9 +1,10 @@
 {% extends 'textdocs/base.html' %}
 {% block content %}
-    <h2>Create a New Document</h2>
+    <h2>{% if form.instance.pk %}Edit Document{% else %}Create a New Document{% endif %}</h2>
     <form method="post">
         {% csrf_token %}
         {{ form.as_p }}
         <button type="submit" class="btn btn-success">Save Document</button>
+        <a href="{{ document.get_absolute_url }}" class="btn btn-secondary">Cancel</a>
     </form>
 {% endblock %}

+ 4 - 0
textdocs/urls.py

@@ -7,4 +7,8 @@ urlpatterns = [
     path('document/new/', views.document_create, name='document_create'),
     path('document/<int:pk>/', views.document_detail, name='document_detail'),
     path('document/<int:pk>/pdf/', views.generate_pdf, name='generate_pdf'),
+    
+    # --- NEW URLS for Edit and Delete ---
+    path('document/<int:pk>/edit/', views.document_update, name='document_update'),
+    path('document/<int:pk>/delete/', views.document_delete, name='document_delete'),
 ]

+ 29 - 6
textdocs/views.py

@@ -1,5 +1,7 @@
 # textdocs/views.py
+
 from django.shortcuts import render, get_object_or_404, redirect
+from django.urls import reverse_lazy # NEW: Useful for delete success URL
 from django.http import HttpResponse
 from django.template.loader import render_to_string
 from weasyprint import HTML
@@ -28,18 +30,39 @@ def document_create(request):
         form = DocumentForm()
     return render(request, 'textdocs/document_form.html', {'form': form})
 
+# --- NEW: View for updating a document ---
+def document_update(request, pk):
+    """Handles editing and saving an existing document."""
+    document = get_object_or_404(Document, pk=pk)
+    if request.method == 'POST':
+        # Pass the instance to the form to update it
+        form = DocumentForm(request.POST, instance=document)
+        if form.is_valid():
+            form.save()
+            return redirect('document_detail', pk=document.pk)
+    else:
+        # Pre-populate the form with the existing document's data
+        form = DocumentForm(instance=document)
+    return render(request, 'textdocs/document_form.html', {'form': form})
+
+# --- NEW: View for deleting a document ---
+def document_delete(request, pk):
+    """Handles the deletion of a document after confirmation."""
+    document = get_object_or_404(Document, pk=pk)
+    if request.method == 'POST':
+        # This is after the user confirms the deletion
+        document.delete()
+        return redirect('document_list') # Redirect to the list after deletion
+    
+    # If it's a GET request, show the confirmation page
+    return render(request, 'textdocs/document_confirm_delete.html', {'document': document})
+
 def generate_pdf(request, pk):
     """Generates a PDF for a specific document."""
     document = get_object_or_404(Document, pk=pk)
-
-    # Render the HTML template with the document context
     html_string = render_to_string('textdocs/document_pdf.html', {'document': document})
-
-    # Use WeasyPrint to create the PDF
     html = HTML(string=html_string, base_url=request.build_absolute_uri())
     pdf = html.write_pdf()
-
-    # Create an HTTP response with the PDF
     response = HttpResponse(pdf, content_type='application/pdf')
     response['Content-Disposition'] = f'attachment; filename="{document.title}.pdf"'
     return response