Added asset edit/save

This commit is contained in:
Dirk Jahnke 2022-03-16 14:13:20 +01:00
parent 111f147cac
commit fa2b83a63a
3 changed files with 31 additions and 3 deletions

View File

@ -1,6 +1,23 @@
{% extends 'base.html' %}
{% extends "base.html" %}
{% load static %}
{% block title %}Edit Asset: {{ asset.named_id }}{% endblock %}
{% block content %}
<h2>Form for Asset</h2>
{% endblock %}
<div class="container">
<form action="{% url 'asset:asset-save' asset.id %}" method="post">
{% csrf_token %}
<div class="row">
<div class="col-sm-12">
<legend><h2>Asset: {{ asset.named_id }}</h2></legend>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<div><input type="text" name="description" id="description" value="{{ asset.description }}" max_length="250" size="80"> <label for="description">Beschreibung</label></div>
<div><input type="number" name="quantity" id="quantity" value="{{ asset.quantity }}" size="6" min="0" max="99999"><label for="quantity">Quantity</label></div>
<div><input type="submit" class="btn btn-primary" value="save"></div>
</div>
</div>
</form>
</div>
{% endblock content %}

View File

@ -1,4 +1,5 @@
from django.urls import path
from . import views
from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetIndexView
app_name = 'asset'
@ -6,6 +7,7 @@ urlpatterns = [
path('', AssetIndexView.as_view(), name='index'),
path('add/', AssetCreateView.as_view(), name='asset-add'),
path('<int:pk>/', AssetUpdateView.as_view(), name='asset-update'),
path('<int:asset_id>/save', views.asset_save, name='asset-save'),
path('<int:pk>/delete/', AssetDeleteView.as_view(), name='asset-delete'),
]

View File

@ -2,6 +2,8 @@ from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views import generic
from asset.models import Asset
from django.shortcuts import get_object_or_404, reverse
from django.http import HttpResponse, HttpResponseRedirect
class AssetCreateView(CreateView):
model = Asset
@ -23,3 +25,10 @@ class AssetIndexView(generic.ListView):
# Return the last 20 created containers
return Asset.objects.order_by('-created_ts')[:20]
def asset_save(request, asset_id):
asset = get_object_or_404(Asset, pk=asset_id)
asset.description = request.POST['description']
asset.quantity = request.POST['quantity']
asset.save();
return HttpResponseRedirect(reverse('asset:index'))