python - 新手 - 具有多个应用程序的 Django 项目 - 无法渲染 View

标签 python django python-3.x django-models django-templates

我正在开发一个 Django 项目,该项目有 3 个应用程序,即客户、投资和股票。每个客户可以拥有多项投资和股票,但反之则不然。到目前为止,应用程序的管理端一切运行良好。我能够以管理员身份进行 CRUD,但我的目标是拥有 3 个独立的用户级别 - 客户、顾问和管理员(这工作正常!)。客户只能查看他/她的个人资料、投资和与其相关的股票。顾问可以查看多个客户及其投资组合的信息。我想我可以通过不同的身份验证级别/限制来区分它们。这些是我的文件,

这是我的客户模型,

    from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User


class Customer(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=200)
    cust_number = models.AutoField(max_length=5, primary_key=True)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    zipcode = models.CharField(max_length=10)
    email = models.CharField(max_length=200)
    home_phone = models.CharField(max_length=50)
    cell_phone = models.CharField(max_length=50)
    created_date = models.DateTimeField(
        default=timezone.now)
    updated_date = models.DateTimeField(
        blank=True, null=True)

    def created(self):
        self.created_date = timezone.now()
        self.save()

    def updated(self):
        self.updated_date = timezone.now()
        self.save()

    def __str__(self):
        return self.name

这是我的投资模型,

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User


class Investment(models.Model):
    category = models.CharField(max_length=50)
    description = models.CharField(max_length=200)
    cust_number = models.ForeignKey('customers.Customer')
    acquired_value = models.DecimalField(max_digits=10, decimal_places=2)
    acquired_date = models.DateTimeField(default=timezone.now)
    recent_value = models.DecimalField(max_digits=10, decimal_places=2)
    recent_date = models.DateTimeField(default=timezone.now, blank=True, null=True)

    def created(self):
        self.acquired_date = timezone.now()
        self.save()

    def updated(self):
        self.recent_date = timezone.now()
        self.save()

    def __str__(self):
        return self.category

这是我的股票模型,

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User


class Stock(models.Model):
    symbol = models.CharField(max_length=10)
    name = models.CharField(max_length=50)
    shares = models.CharField(max_length=50)
    cust_number = models.ForeignKey('customers.Customer')
    purchase_price = models.DecimalField(max_digits=10, decimal_places=2)
    recent_date = models.DateTimeField(default=timezone.now, blank=True, null=True)

    def created(self):
        self.recent_date = timezone.now()
        self.save()

    def __str__(self):
        return self.name

这是我在客户应用程序中的views.py,尽管投资和股票有单独的 View ,但我在customers/views.py中定义了相同的类,因此它在单个 View 中呈现。

from django.shortcuts import render
from django.utils import timezone
from .models import Customer
from investments.models import Investment
from stocks.models import Stock


def customer(request):
    customers = Customer.objects.filter(created_date__lte=timezone.now())
    return render(request, 'customers/customer.html', {'customers': customers})


def investment(request):
    investments = Investment.objects.filter(created_date__lte=timezone.now())
    return render(request, 'customers/customer.html', {'investments': investments})


def stock(request):
    stocks = Stock.objects.filter(created_date__lte=timezone.now())
    return render(request, 'customers/customer.html', {'stocks': stocks})

我正在尝试像这样在单个 html 页面上呈现 View ,

这是我的 customer.html 模板

{% load staticfiles %}
<!DOCTYPE html>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">

<html lang="en">

<head>
  <link rel="stylesheet" href="{% static 'css/customers.css' %}">
  <meta charset="UTF-8">
  <title>Eagle Financial Services</title>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-10 col-md-offset-1">
        <div class="panel panel-primary">
          <div class="panel-heading">Welcome!</div>
          <div class="panel-body">
            ABC Financial Services, your Financial Services Partner.
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <h2 style="padding-left: 15Px">Customer Information</h2>
  </div>
  <div>
    <table class="table table-striped table-bordered table-hover">
      <thead>
        <tr class="bg-info">
          <th>Customer ID</th>
          <th>Name</th>
          <th>Address</th>
          <th>City</th>
          <th>State</th>
          <th>Zip</th>
          <th>Primary Email</th>
          <th>Home Phone</th>
          <th>Cell Phone</th>
          <th colspan="3">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for customer in customers %}
        <tr>
          <td>{{ customer.cust_number }}</td>
          <td>{{ customer.name }}</td>
          <td>{{ customer.address }}</td>
          <td>{{ customer.city }}</td>
          <td>{{ customer.state }}</td>
          <td>{{ customer.zipcode }}</td>
          <td>{{ customer.email }}</td>
          <td>{{ customer.home_phone }}</td>
          <td>{{ customer.cell_phone }}</td>
          <td><a href="{{ customers.customer }}" class="btn btn-primary">Read</a></td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>

  <div class="row">
    <h2 style="padding-left: 15Px">Investments Information</h2>
  </div>
  <div>
    <table class="table table-striped table-bordered table-hover">
      <thead>
        <tr class="bg-info">
          <th>Customer ID</th>
          <th>Name</th>
          <th>Category</th>
          <th>Description</th>
          <th>Acquired Value</th>
          <th>Acquired Date</th>
          <th>Recent Value</th>
          <th>Recent Date</th>
          <th colspan="3">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for customer in customers %}
        <tr>
          <td>{{ customer.cust_number }}</td>
          <td>{{ customer.name }}</td>
          {% for investment in investments %}
          <td>{{ investment.category }}</td>
          <td>{{ investment.description }}</td>
          <td>{{ investment.acquired_value }}</td>
          <td>{{ investment.acquired_date }}</td>
          <td>{{ investment.recent_value }}</td>
          <td>{{ investment.recent_date }}</td>
          {% endfor %} {% endfor %}

      </tbody>
    </table>
  </div>

  <div class="row">
    <h2 style="padding-left: 15Px">Stocks Information</h2>
  </div>
  <div>
    <table class="table table-striped table-bordered table-hover">
      <thead>
        <tr class="bg-info">
          <th>Symbol</th>
          <th>Name</th>
          <th>Shares</th>
          <th>Cust_Number</th>
          <th>Purchase Price</th>
          <th>Recent Date</th>
          <th colspan="3">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for stock in stocks %}
        <tr>
          <td>{{ stock.symbol }}</td>
          <td>{{ stock.name }}</td>
          <td>{{ stock.shares }}</td>
          <td>{{ stock.cust_number }}</td>
          <td>{{ stock.purchase_price }}</td>
          <td>{{ stock.recent_date }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>

</body>

</html>

这是项目名称.url

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include('customers.urls')),
    url(r'', include('investments.urls')),
    url(r'', include('stocks.urls')),
]

这是客户.url

`from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.customer, name='customer'),
]`

除了名称之外,投资和股票与 customer.url 类似。

现在在网页上,仅显示客户表中的值,在投资表中仅显示 cust_number 和 name,没有其他内容,在股票中则不显示任何内容。过去三天我几乎尝试了所有的尝试/错误。我很困惑。我哪里弄错了。任何指导都将受到高度赞赏。谢谢。

我使用的是 Python - 3.6,Django - 1.11.1。

最佳答案

您遇到此问题的原因是 views.customer 仅传递客户信息,而不传递股票等其他变量。 所以在你的 customer/views.py 中:

def customer(request):
    customers = Customer.objects.filter(created_date__lte=timezone.now())
    investments = Investment.objects.filter(created_date__lte=timezone.now())
    stocks = Stock.objects.filter(created_date__lte=timezone.now())
    return render(request, 'customers/customer.html', {'customers': customers, 'stocks': stock, 'investments', investments})

关于python - 新手 - 具有多个应用程序的 Django 项目 - 无法渲染 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44487187/

相关文章:

python - 自动将 Json 数据添加到 Django 模型中,且仅一次

python - 如何从 SimpleNamespace 初始化字典?

python - Opencv 3.2 Python视频编写问题: Text on frames gets overlapped

python - 如何将django admin中输入的时间转换为utc?

python - 根据掩码删除日期子字符串

python - 升级后无法加载 GDAL 库

python-3.x - 在另一台计算机上运行卡住的 pyqt 应用程序时不显示图像

python - 当 DateTimeField 等于当前日期和时间时,Django 执行函数

python - 如何打印文本文件中的某些行和行的某些部分

使用复杂规则复制数组中的值的 Pythonic 方式