Reputation: 203
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>
Upvotes: 0
Views: 81
Reputation: 99620
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
Upvotes: 1