0

I need some help mapping views in Django. My app is pretty simple -- it's just a status page listing all of our environments and their corresponding apps. So far I have all the env's listed like so:

ENV_1
ENV_2
ENV_3

But what I want is: (where [app*] would be from app_list in my Environment class -- see below)

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

Here's my models.py (app_list = app1, app2, etc.)

- - UPDATE - -

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(',')]))

And my 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)

And my 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>
2
  • I'm not sure what your question is. Commented Jul 14, 2015 at 14:52
  • I want to loop through app_list and display them next to each env Commented Jul 14, 2015 at 14:54

1 Answer 1

1

To fix your specific issue, you can do something like this:

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(',')])

and you can call the format helper method in the template - feel free to modify it to your usecase.

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

I see a few things that could be changed in this app. I would also recommend using slugs - example this app is great (django-autoslug-field). One approach for this would be:

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()

This would give you the flexibility to analyze which apps are in an environment, and at the same time, what environments does an app belong to.

Your template would be

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

Also, now you can process the url by slug, instead of the name - which would eliminate all the .replace(..) hacks

Sign up to request clarification or add additional context in comments.

8 Comments

Does AutoSlugField() generate slug for each field? I normally use models.SlugField().
That should be sufficient, but AutoSlugField does a little more - you can set it up for updating the slug when name changes, etc..
Thanks karthikr! Please forgive me for prying, but how could I implement the first part? Maybe add this to my views? application_list = format(app_list) and update: context_dict = {'environments': environment_list, 'applications': application_list}
Check the edit. You can do it in the template directly.
Ugh I can't get {{environment.format.apps_list}} to return anything. I've even tried just returning self.name from that def and I still don't get anything. Thanks again... appreciate your patience.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.