在Django中映射views.py(Python)

时间:2015-07-14 14:48:45

标签: python django

我需要在Django中帮助映射视图。我的应用程序非常简单 - 它只是一个列出我们所有环境及其相应应用程序的状态页面。到目前为止,我所有的env列出如下:

ENV_1
ENV_2
ENV_3

但我想要的是:(其中[app *]将来自我的环境类中的app_list - 见下文)

ENV_1  [app1] [app2] [app3] ...
ENV_2  [app1] [app2] [app3] ...
ENV_3  [app1] [app2] [app3] ...

这是我的models.py(app_list = app1,app2等)

- - 更新 -

from django.db import models

class Environment(models.Model):
    name = models.CharField(max_length=128, unique=True)
    app_list = models.CharField(max_length=128, blank=True)

    def __str__(self):
        return self.name

    def format(self):
        app_list = self.app_list or ""
        return (" ".join(["[%s] " % a for a in app_list.split(',')]))

和我的views.py:

from django.shortcuts import render
from dashboard.models import Environment, Page

def index(request):
    environment_list = Environment.objects.order_by('name')
    context_dict = {'environments': environment_list}

    for environment in environment_list:
        environment.url = environment.name.replace(' ', '_')

    return render(request, 'dashboard/index.html', context_dict)

def environment(request, environment_name_url):
    environment_name = environment_name_url.replace('_', ' ')
    context_dict = {'environment_name': environment_name}

    try:
        environment = Environment.objects.get(name=environment_name)
        context_dict['environment'] = environment
    except Environment.DoesNotExist:
        pass

    return render(request, 'dashboard/environment.html', context_dict)

我的index.html

<body>
  <h1>Title</h1>

  {% if environments %}
    td>
        {% for environment in environments %}
            <li><a href="/dashboard/environment/{{ environment.url }}">{{ environment.name }}</a>{{environment.format_apps_list}}</li>
    </td>
  {% else %}
    <strong>There are no environments present.</strong>
  {% endif %}

</body>

1 个答案:

答案 0 :(得分:1)

要解决您的具体问题,您可以执行以下操作:

class Environment(models.Model):
    name = models.CharField(max_length=128, unique=True)
    app_list = models.CharField(max_length=128, blank=True)

    def __str__(self):
        return self.name

    def format_apps_list(self):
        app_list = self.app_list or "" #if app_list is blank
        return (" ".join(["[%s] " % a for a in app_list.split(',')])

您可以在模板中调用format辅助方法 - 随意将其修改为您的用例。

{% for environment in environments %}
    <li><a href="/dashboard/environment/{{ environment.url }}">{{ environment.name }}</a> {{environment.format_apps_list}}</li>

我在这款应用中看到了一些可以更改的内容。 我还建议使用slugs - 例如此应用很棒(django-autoslug-field)。一种方法是:

from django.db import models


class Environment(models.Model):
    name = models.CharField(max_length=128, unique=True)
    slug = AutoSlugField()

    def __str__(self):
        return self.name

class App(models.Model):
    environment = models.ForeignKey(Environment)
    name = models.CharField(max_length=128)
    slug = AutoSlugField()

这将使您可以灵活地分析环境中的应用程序,同时还可以分析应用程序所属的环境。

您的模板将是

{% for environment in environments %}
    {% for app in environment.app_set.all %}
        {{app.name}}
    {% endfor %}
{% endfor %}

此外,现在您可以通过slug处理网址,而不是名称 - 这将消除所有.replace(..)黑客攻击