我有一个简单的Web界面,可在我们公司的数据库中显示设备的资产编号。我在数字Deploy
和Return
旁边有一个可点击的链接。我想发生的事情是,当某人单击Deploy
时,该特定设备的资产编号被添加到列表中(最终是用于打印的csv文件),以及其必需的型号和序列号。
如何使可点击链接执行一个函数,该函数将其旁边的那个特定资产编号,在数据库中搜索其型号和序列号并将它们添加到列表中?
我的想法是,我可以单击一堆资产上的Deploy
,并将它们放在适合打印的部署工作表上。我是Django的新手,并且我一直在使用Mozilla教程作为该网站的骨架,但是现在我要取消一点保留了。谢谢!
models.py
:
from django.db import models
from django.urls import reverse #Used to generate URLs by reversing the URL patterns
from django.contrib.auth.models import User
import uuid
from datetime import date
class ModelInstance(models.Model):
asset = models.CharField('Asset Number', max_length = 50, help_text = "Enter Weston or Government I.D. Tag Number")
model = models.ForeignKey('Model', on_delete = models.SET_NULL, null = True, help_text = "Leave blank if no model number exists")
serial = models.CharField('Serial Number', max_length = 50, null = True, blank = True, help_text = "Leave blank if no serial number exists")
location = models.ForeignKey('Location', on_delete = models.SET_NULL, null = True, help_text = "Enter current location or location after manufacturing")
ownership = models.ForeignKey('Ownership', on_delete = models.SET_NULL, null = True, help_text = "Enter current owner of equipment")
borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
STATUS = (
('M', 'Maintenance'), #being worked on, not available
('D', 'Deployed'), #in the field
('A', 'Available'), #ready to go
('R', 'Reserved'), #Being used for classified/other project but not deployed
('K', 'Broken'), #Broken
)
status = models.CharField(
max_length = 1,
choices = STATUS,
blank = True,
default = 'A',
help_text = 'Equipment Availability',
)
note = models.TextField('Notes', max_length = 1000, null = True, blank = True)
date_added = models.DateField("Date Added", default=date.today())
def __str__(self):
return f'Asset Number: {self.asset}..........Serial Number: {self.serial}..........Location: {self.location}..........Ownership: {self.ownership}'
class Category(models.Model):
category = models.CharField('Equipment Type', max_length = 50, help_text = "Enter general category of equipment")
class Meta:
verbose_name_plural = "categories" #default would have shown as "Categorys" on admin page
def __str__(self):
return self.category
category_list.html :
{% extends "base_generic.html" %}
{% block content %}
<div style="margin-left:20px; margin-top:20px">
<h1>Model list</h1>
</div>
{% if category_list %}
{% for category in category_list %}
<ul><strong>{{ category }}</strong></ul>
<ul>
{% for model in category.model_set.all %}
<a href="{{ model.get_absolute_url }}"> {{ model.model_number }}..............{{ model.description }} </a>
<ul> {% for copy in model.modelinstance_set.all %}
<li>{{ copy.asset }} <a href="">Deploy</a> <a href="">Return</a>
<p class="{% if copy.status == 'A' %}text-success{% elif copy.status == 'M' %}text-danger{% else %}text-warning{% endif %}">{{ copy.get_status_display }}</p>
</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
{% endfor %}
{% else %}
<p>There is no equipment in the database</p>
{% endif %}
{% endblock %}
urls.py :
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('books/', views.BookListView.as_view(), name='books'),
path('book/<int:pk>', views.BookDetailView.as_view(), name='book-detail'),
path('models/', views.ModelListView.as_view(), name='models'),
path('model/<int:pk>', views.ModelDetailView.as_view(), name='model-detail'),
path('category/', views.CategoryListView.as_view(), name='categories'),
]
urlpatterns += [
path('mybooks/', views.LoanedBooksByUserListView.as_view(), name='my-borrowed'),
]
urlpatterns += [
path('book/<uuid:pk>/renew', views.renew_book_librarian, name="renew-book-librarian"),
]
views.py :
from django.shortcuts import render #generates HTML fiels using a template and data
#from django.http import HttpResponse
from .models import Book, Author, BookInstance, Genre, Model, ModelInstance, Category, Ownership, Manufacturer, Location #imports model classes to access data in views
from django.views import generic
class BookListView(generic.ListView):
model = Book
class BookDetailView(generic.DetailView):
model = Book
class ModelListView(generic.ListView):
model = Model
class ModelDetailView(generic.DetailView):
model = Model
class CategoryListView(generic.ListView):
model = Category