У меня есть база данных активов с номером актива, номером модели, серийным номером и т. Д. На моем веб-сайте я хотел бы нажать кнопку «Развернуть» рядом с номером актива и добавить в него этот номер актива (свсе остальные поля, связанные) с фреймом данных панд.Я новичок в Django, пандах и питоне, поэтому я немного застрял в том, как поступить.У меня есть базовый скелет, составленный из различных уроков.
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 ModelListView(generic.ListView):
model = Model
class ModelDetailView(generic.DetailView):
model = Model
class CategoryListView(generic.ListView):
model = Category
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
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'),
]
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 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
class Manufacturer(models.Model):
manufacturer = models.CharField(max_length = 50, help_text = "Original manufacturer or supplier")
def __str__(self):
return self.manufacturer
class Model(models.Model):
model_number = models.CharField('Model Number', max_length = 50)
manufacturer = models.ForeignKey('Manufacturer', on_delete = models.SET_NULL, null = True)
category = models.ForeignKey('Category', on_delete = models.SET_NULL, null = True)
description = models.TextField(max_length = 1000, help_text = "Enter brief description of product", null = True) #TextField for longer descriptions
def __str__(self):
return f'{self.model_number}..........{self.description}'
def get_absolute_url(self):
#Returns the url to access a particular location instance
return reverse('model-detail', args=[str(self.id)])
class Ownership(models.Model):
ownership = models.CharField('Ownership', max_length = 50)
class Meta:
verbose_name_plural = "Owners"
def __str__(self):
return self.ownership
def get_absolute_url(self):
return reverse('ownership-detail', args=[str(self.id)])
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}'
base_generic.html
<!DOCTYPE html>
<html lang="en">
<head>
{% block title %}<title>Local Library</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
{% block sidebar %}
<ul class="sidebar-nav", style="list-style-type:none">
<br></br>
<li><a href="{% url 'index' %}">Home</a></li>
<li><a href="{% url 'books' %}">All books</a></li>
<li><a href="{% url 'categories' %}">Search & Deploy</a></li>
<li><a href="">Recent Deployments</a></li>
<li><a href="">Disabled List</a></li>
<br></br>
<!--Authentication in sidebar-->
{% if user.is_authenticated %}
<li>User: {{ user.get_username }}</li>
<li><a href="{% url 'my-borrowed' %}">My Borrowed Books</a></li>
<li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
{% else %}
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}
</ul>
{% endblock %}
</div>
<div class="col-sm-10 ">
{% block content %}{% endblock %}
</div>
</div>
</div>
</body>
</html>
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 %}
model_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 %}
<li><strong>{{ category }}</strong></li>
<ul>
{% for model in category.model_set %}
<li>
<a href="{{ model.get_absolute_url }}"> {{ model.model_number }}..........{{ model.description }} </a>
</li>
{% endfor %}
</ul>
{% endfor %}
{% else %}
<p>There is no equipment in the database</p>
{% endif %}
{% endblock %}
Пожалуйста, дайте мне знать, если вам нужно что-нибудь еще, спасибо!